dbot/modules/poll.js

79 lines
2.6 KiB
JavaScript
Raw Normal View History

2012-06-13 22:11:00 +02:00
var poll = function(dbot) {
2012-06-15 17:15:02 +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': {},
'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 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 {
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);
}