dbot/modules/poll.js

85 lines
3.0 KiB
JavaScript
Raw Normal View History

2012-06-13 22:11:00 +02:00
var poll = function(dbot) {
2012-06-15 17:18:36 +02:00
if(!dbot.db.hasOwnProperty('polls')) {
dbot.db.polls = {};
}
2012-06-13 22:11:00 +02:00
var polls = dbot.db.polls;
var commands = {
'~newpoll': function(event) {
var name = event.input[1];
2012-06-15 00:32:16 +02:00
var options = event.input[2].split(',');
var description = event.input[3];
2012-06-13 22:11:00 +02:00
if(name === undefined || name === 'help') {
event.reply(dbot.t('newpoll_usage'));
} else {
if(polls.hasOwnProperty(name)) {
2012-06-15 01:07:15 +02:00
event.reply(dbot.t('poll_exists', {'name': name}));
2012-06-13 22:11:00 +02:00
} else {
polls[name] = {
'name': name,
2012-06-15 00:32:16 +02:00
'description': description,
2012-06-13 22:16:29 +02:00
'owner': event.user,
2012-06-15 00:32:16 +02:00
'votes': {},
2012-06-15 18:20:05 +02:00
'votees': {}
2012-06-13 22:11:00 +02:00
};
for(var i=0;i<options.length;i++) {
polls[name]['votes'][options[i]] = 0;
}
2012-06-15 00:32:16 +02:00
2012-06-15 01:07:15 +02:00
event.reply(dbot.t('poll_created', {'name': name, 'description': description}));
2012-06-13 22:11:00 +02:00
}
}
},
'~vote': function(event) {
2012-06-15 00:32:16 +02:00
var name = event.input[1];
var vote = event.input[2];
2012-06-13 22:11:00 +02:00
2012-06-15 00:32:16 +02:00
if(polls.hasOwnProperty(name)) {
2012-06-15 18:30:54 +02:00
if(!polls[name].votes.hasOwnProperty(vote)) {
2012-06-15 18:20:05 +02:00
if(polls[name].votees.hasOwnProperty(event.user)) {
var oldVote = polls[name].votees[event.user];
polls[name].votes[oldVote]--;
2012-06-15 00:32:16 +02:00
polls[name].votes[vote]++;
2012-06-15 18:20:05 +02:00
polls[name].votees[event.user] = vote;
event.reply(dbot.t('changed_voted', {'vote': vote, 'poll': name,
'count': polls[name].votes[vote]}));
2012-06-15 00:32:16 +02:00
} else {
2012-06-15 18:20:05 +02:00
polls[name].votes[vote]++;
polls[name].votees[event.user] = vote;
event.reply(dbot.t('voted', {'vote': vote, 'poll': name,
'count': polls[name].votes[vote]}));
2012-06-15 00:32:16 +02:00
}
2012-06-15 18:20:05 +02:00
} else {
event.reply(dbot.t('invalid_vote', {'vote': vote}));
2012-06-15 00:32:16 +02:00
}
} else {
2012-06-15 01:07:15 +02:00
event.reply(dbot.t('poll_unexistent', {'name': name}));
2012-06-15 00:32:16 +02:00
}
2012-06-13 22:11:00 +02:00
},
2012-06-15 00:57:07 +02:00
'~viewpoll': function(event) {
var name = event.input[1];
if(polls.hasOwnProperty(name)) {
2012-06-13 22:11:00 +02:00
2012-06-15 00:57:07 +02:00
} else {
2012-06-15 01:07:15 +02:00
event.reply(dbot.t('poll_unexistent', {'name': name}));
2012-06-15 00:57:07 +02:00
}
2012-06-13 22:11:00 +02:00
}
};
2012-06-15 00:36:52 +02:00
commands['~newpoll'].regex = [/~newpoll ([^ ]+) \[options=([^ ]+)\] (.+)/, 4];
2012-06-15 00:42:06 +02:00
commands['~vote'].regex = [/~vote ([^ ]+) ([^ ]+)/, 3];
2012-06-15 00:57:07 +02:00
commands['~viewpoll'].regex = [/~viewpoll ([^ ]+)/, 2];
2012-06-13 22:11:00 +02:00
return {
'name': 'poll',
'ignorable': true,
'commands': commands
};
};
exports.fetch = function(dbot) {
return poll(dbot);
}