2013-03-16 01:02:35 +01:00
|
|
|
var _ = require('underscore')._,
|
|
|
|
databank = require('databank'),
|
2013-03-16 14:45:58 +01:00
|
|
|
AlreadyExistsError = databank.AlreadyExistsError,
|
2013-03-16 01:02:35 +01:00
|
|
|
NoSuchThingError = databank.NoSuchThingError;
|
2013-01-15 17:21:43 +01:00
|
|
|
|
|
|
|
var commands = function(dbot) {
|
|
|
|
var commands = {
|
|
|
|
'~newpoll': function(event) {
|
2013-01-19 14:34:42 +01:00
|
|
|
var name = event.input[1].toLowerCase(),
|
|
|
|
options = event.input[2].toLowerCase().split(','),
|
2013-01-15 17:21:43 +01:00
|
|
|
description = event.input[3];
|
2013-05-06 20:12:25 +02:00
|
|
|
|
|
|
|
_.each(options, function(item, index, list) { list[index] = [ item, 0 ]; })
|
|
|
|
votes = _.object(options);
|
2013-02-16 18:37:23 +01:00
|
|
|
|
2013-05-06 20:12:25 +02:00
|
|
|
dbot.api.users.resolveUser(event.server, event.user, function(user) {
|
|
|
|
this.db.create('poll', name, {
|
|
|
|
'name': name,
|
|
|
|
'description': description,
|
|
|
|
'owner': user.id,
|
|
|
|
'votes': votes,
|
|
|
|
'votees': {}
|
|
|
|
}, function(err, value) {
|
|
|
|
if(!err) {
|
|
|
|
event.reply(dbot.t('poll_created', {
|
|
|
|
'name': name,
|
|
|
|
'description': description,
|
|
|
|
'url': dbot.api.web.getUrl('poll/' + name)
|
|
|
|
}));
|
|
|
|
} else if(err instanceof AlreadyExistsError) {
|
|
|
|
event.reply(dbot.t('poll_exists', { 'name': name }));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}.bind(this));
|
2013-01-15 17:21:43 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
'~addoption': function(event) {
|
2013-01-19 14:34:42 +01:00
|
|
|
var name = event.input[1].toLowerCase(),
|
2013-05-06 20:12:25 +02:00
|
|
|
option = event.input[2].toLowerCase();
|
2013-01-15 17:21:43 +01:00
|
|
|
|
2013-05-06 20:12:25 +02:00
|
|
|
dbot.api.users.resolveUser(event.server, event.user, function(user) {
|
|
|
|
this.db.read('poll', name, function(err, poll) {
|
|
|
|
if(!err) {
|
|
|
|
if(poll.owner === user.id) {
|
|
|
|
if(!_.has(poll.votes, option)) {
|
|
|
|
poll.votes[option] = 0;
|
|
|
|
this.db.save('poll', name, poll, function(err) {
|
|
|
|
event.reply(dbot.t('option_added', {
|
|
|
|
'user': event.user,
|
|
|
|
'name': name,
|
|
|
|
'option': option
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('option_exists', {
|
|
|
|
'option': option,
|
2013-03-16 01:02:35 +01:00
|
|
|
'name': name,
|
2013-05-06 20:12:25 +02:00
|
|
|
'user': event.user
|
2013-03-16 01:02:35 +01:00
|
|
|
}));
|
2013-05-06 20:12:25 +02:00
|
|
|
}
|
2013-03-16 01:02:35 +01:00
|
|
|
} else {
|
2013-05-06 20:12:25 +02:00
|
|
|
event.reply(dbot.t('not_poll_owner', {
|
|
|
|
'user': event.user,
|
|
|
|
'name': name
|
2013-03-16 01:02:35 +01:00
|
|
|
}));
|
|
|
|
}
|
2013-01-15 17:21:43 +01:00
|
|
|
} else {
|
2013-05-06 20:12:25 +02:00
|
|
|
if(err instanceof NoSuchThingError) {
|
|
|
|
event.reply(dbot.t('poll_unexistent', { 'name': name }));
|
|
|
|
}
|
2013-03-16 01:02:35 +01:00
|
|
|
}
|
2013-05-06 20:12:25 +02:00
|
|
|
}.bind(this));
|
|
|
|
});
|
2013-01-15 17:21:43 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
'~rmoption': function(event) {
|
2013-01-19 14:34:42 +01:00
|
|
|
var name = event.input[1].toLowerCase(),
|
2013-01-20 20:12:34 +01:00
|
|
|
option = event.input[2].toLowerCase(),
|
|
|
|
user = dbot.api.users.resolveUser(event.server, event.user);
|
2013-01-15 17:21:43 +01:00
|
|
|
|
2013-03-16 01:02:35 +01:00
|
|
|
this.db.read('poll', name, function(err, poll) {
|
|
|
|
if(!err) {
|
|
|
|
if(poll.owner === user) {
|
|
|
|
if(_.has(poll.votes, option)) {
|
|
|
|
delete poll.votes[option];
|
|
|
|
|
|
|
|
this.db.save('poll', name, poll, function(err) {
|
|
|
|
event.reply(dbot.t('option_removed', {
|
|
|
|
'user': event.user,
|
|
|
|
'name': name,
|
|
|
|
'option': option
|
|
|
|
}));
|
|
|
|
}.bind(this));
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('invalid_vote', { 'vote': option }));
|
|
|
|
}
|
2013-01-15 17:21:43 +01:00
|
|
|
} else {
|
2013-03-16 01:02:35 +01:00
|
|
|
event.reply(dbot.t('not_poll_owner', { 'name': name }));
|
2013-01-15 17:21:43 +01:00
|
|
|
}
|
|
|
|
} else {
|
2013-03-16 01:02:35 +01:00
|
|
|
if(err instanceof NoSuchThingError) {
|
|
|
|
event.reply(dbot.t('poll_unexistent', { 'name': name }));
|
|
|
|
}
|
2013-01-15 17:21:43 +01:00
|
|
|
}
|
2013-03-16 01:02:35 +01:00
|
|
|
}.bind(this));
|
2013-01-15 17:21:43 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
'~vote': function(event) {
|
2013-01-19 14:34:42 +01:00
|
|
|
var name = event.input[1].toLowerCase(),
|
2013-05-06 20:12:25 +02:00
|
|
|
vote = event.input[2].toLowerCase();
|
|
|
|
|
|
|
|
dbot.api.users.resolveUser(event.server, event.user, function(user) {
|
|
|
|
this.db.read('poll', name, function(err, poll) {
|
|
|
|
if(!err) {
|
|
|
|
if(_.has(poll.votes, vote)) {
|
|
|
|
if(_.has(poll.votees, user.id)) {
|
|
|
|
var oldVote = poll.votees[user.id];
|
|
|
|
poll.votes[oldVote]--;
|
|
|
|
poll.votes[vote]++;
|
|
|
|
poll.votees[user.id] = vote;
|
|
|
|
} else {
|
|
|
|
poll.votes[vote]++;
|
|
|
|
poll.votees[user.id] = vote;
|
|
|
|
}
|
2013-01-15 17:21:43 +01:00
|
|
|
|
2013-05-06 20:12:25 +02:00
|
|
|
this.db.save('poll', name, poll, function(err) {
|
|
|
|
event.reply(dbot.t('voted', {
|
|
|
|
'vote': vote,
|
|
|
|
'poll': name,
|
|
|
|
'count': poll.votes[vote],
|
|
|
|
'user': event.user
|
|
|
|
}));
|
|
|
|
}.bind(this));
|
2013-03-16 01:02:35 +01:00
|
|
|
} else {
|
2013-05-06 20:12:25 +02:00
|
|
|
event.reply(dbot.t('invalid_vote', { 'vote': vote }));
|
2013-03-16 01:02:35 +01:00
|
|
|
}
|
2013-01-15 17:21:43 +01:00
|
|
|
} else {
|
2013-05-06 20:12:25 +02:00
|
|
|
if(err instanceof NoSuchThingError) {
|
|
|
|
event.reply(dbot.t('poll_unexistent', { 'name': name }));
|
|
|
|
}
|
2013-03-16 01:02:35 +01:00
|
|
|
}
|
2013-05-06 20:12:25 +02:00
|
|
|
}.bind(this));
|
2013-03-16 01:02:35 +01:00
|
|
|
}.bind(this));
|
2013-01-15 17:21:43 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
'~pdesc': function(event) {
|
2013-01-19 14:34:42 +01:00
|
|
|
var name = event.input[1].toLowerCase();
|
2013-03-16 01:02:35 +01:00
|
|
|
this.db.read('poll', name, function(err, poll) {
|
|
|
|
if(!err) {
|
|
|
|
var options = _.keys(poll.votes);
|
|
|
|
var optionString = " Choices: ";
|
|
|
|
for(var i=0;i<options.length;i++) {
|
|
|
|
optionString += options[i] + ', ';
|
|
|
|
}
|
|
|
|
optionString = optionString.slice(0, -2) + '.';
|
2013-01-15 17:21:43 +01:00
|
|
|
|
2013-03-16 01:02:35 +01:00
|
|
|
event.reply(dbot.t('poll_describe', {
|
|
|
|
'name': name,
|
|
|
|
'description': poll.description,
|
2013-05-06 20:12:25 +02:00
|
|
|
'url': dbot.api.web.getUrl('poll/' + name)
|
2013-03-16 01:02:35 +01:00
|
|
|
}) + optionString);
|
|
|
|
} else {
|
|
|
|
if(err instanceof NoSuchThingError) {
|
2013-05-06 20:12:25 +02:00
|
|
|
event.reply(dbot.t('poll_unexistent', { 'name': name }));
|
2013-03-16 01:02:35 +01:00
|
|
|
}
|
2013-01-21 20:30:06 +01:00
|
|
|
}
|
2013-03-16 01:02:35 +01:00
|
|
|
});
|
2013-01-15 17:21:43 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
'~count': function(event) {
|
2013-01-19 14:34:42 +01:00
|
|
|
var name = event.input[1].toLowerCase();
|
2013-03-16 01:02:35 +01:00
|
|
|
this.db.read('poll', name, function(err, poll) {
|
|
|
|
if(!err) {
|
|
|
|
var order;
|
|
|
|
var votesArr = [];
|
2013-01-15 17:21:43 +01:00
|
|
|
|
2013-03-16 01:02:35 +01:00
|
|
|
var order = _.chain(poll.votes)
|
|
|
|
.pairs()
|
2013-05-06 20:12:25 +02:00
|
|
|
.sortBy(function(option) { return option[1]; })
|
2013-03-16 01:02:35 +01:00
|
|
|
.reverse()
|
|
|
|
.value();
|
2013-01-15 17:21:43 +01:00
|
|
|
|
2013-03-16 01:02:35 +01:00
|
|
|
var orderString = "";
|
|
|
|
for(var i=0;i<order.length;i++) {
|
|
|
|
orderString += order[i][0] +
|
|
|
|
" (" + order[i][1] + "), ";
|
|
|
|
}
|
|
|
|
orderString = orderString.slice(0, -2);
|
2013-01-15 17:21:43 +01:00
|
|
|
|
2013-03-16 01:02:35 +01:00
|
|
|
event.reply(dbot.t('count', {
|
|
|
|
'poll': name,
|
|
|
|
'description': poll.description,
|
|
|
|
'places': orderString
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
if(err instanceof NoSuchThingError) {
|
|
|
|
event.reply(dbot.t('poll_unexistent', {'name': name}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-01-15 17:21:43 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
commands['~newpoll'].regex = [/~newpoll ([^ ]+) options=([^ ]+) (.+)/, 4];
|
|
|
|
commands['~addoption'].regex = [/~addoption ([^ ]+) ([^ ]+)/, 3];
|
|
|
|
commands['~rmoption'].regex = [/~rmoption ([^ ]+) ([^ ]+)/, 3];
|
|
|
|
commands['~vote'].regex = [/~vote ([^ ]+) ([^ ]+)/, 3];
|
|
|
|
commands['~pdesc'].regex = [/~pdesc ([^ ]+)/, 2];
|
|
|
|
commands['~count'].regex = [/~count ([^ ]+)/, 2];
|
|
|
|
|
|
|
|
return commands;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return commands(dbot);
|
|
|
|
}
|