dbot/modules/poll.js

76 lines
2.4 KiB
JavaScript
Raw Normal View History

2012-06-13 22:11:00 +02:00
var poll = function(dbot) {
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)) {
event.reply(dbot.t('poll_exists'));
} 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': {},
'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
event.reply(dbot.t('poll_created', {'name': name, 'options': options.join(' ')}));
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 00:57:07 +02:00
if(polls[name].votees.include(event.user)) {
2012-06-15 00:32:16 +02:00
event.reply(dbot.t('alread_voted'));
} else {
if(polls[name].votes.hasOwnProperty(vote)) {
polls[name].votes[vote]++;
polls[name].votees.push(event.user);
event.reply(dbot.t('voted', {'vote': vote}));
} else {
event.reply(dbot.t('invalid_vote', {'vote': vote}));
}
}
} else {
event.reply(dbot.t('poll_unexistent'));
}
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 {
event.reply(dbot.t('poll_unexistent'));
}
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);
}