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-17 13:51:25 +02:00
|
|
|
|
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 18:51:45 +02:00
|
|
|
event.reply(dbot.t('poll_created', {'name': name, 'description': description}) +
|
|
|
|
' - http://nc.no.de:443/polls/' + name);
|
2012-06-13 22:11:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-06-17 13:51:25 +02:00
|
|
|
'~addoption': function(event) {
|
|
|
|
var name = event.input[1];
|
|
|
|
var option = event.input[2];
|
|
|
|
|
|
|
|
if(polls.hasOwnProperty(name)) {
|
|
|
|
if(polls[name].owner === event.user) {
|
|
|
|
if(!polls[name].votes.hasOwnProperty(name)) {
|
|
|
|
polls[name]['votes'][option] = 0;
|
2012-06-17 13:56:37 +02:00
|
|
|
event.reply(dbot.t('option_added', {'user': event.user,
|
|
|
|
'name': name, 'option': option}));
|
2012-06-17 13:51:25 +02:00
|
|
|
} else {
|
2012-06-17 13:56:37 +02:00
|
|
|
event.reply(dbot.t('option_exists', {'option': option,
|
|
|
|
'name': name, 'user': event.user}));
|
2012-06-17 13:51:25 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('not_poll_owner', {'user': event.user,
|
|
|
|
'name': name}));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('poll_unexistent', {'name': name}));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'~rmoption': function(event) {
|
|
|
|
var name = event.input[1];
|
|
|
|
var option = event.input[2];
|
|
|
|
|
|
|
|
if(polls.hasOwnProperty(name)) {
|
|
|
|
if(polls[name].owner === event.user) {
|
|
|
|
if(polls[name].votes.hasOwnProperty(option)) {
|
|
|
|
delete polls[name]['votes'][option];
|
2012-06-17 13:56:37 +02:00
|
|
|
event.reply(dbot.t('option_removed', {'user': event.user,
|
|
|
|
'name': name, 'option': option}));
|
2012-06-17 13:51:25 +02:00
|
|
|
} else {
|
2012-06-17 13:56:37 +02:00
|
|
|
event.reply(dbot.t('invalid_vote', {'vote': option}));
|
2012-06-17 13:51:25 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('not_poll_owner', {'name': name}));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('poll_unexistent', {'name': name}));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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:32:02 +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;
|
2012-06-15 18:39:26 +02:00
|
|
|
event.reply(dbot.t('changed_vote', {'vote': vote, 'poll': name,
|
2012-06-15 18:46:14 +02:00
|
|
|
'count': polls[name].votes[vote], 'user': event.user}));
|
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,
|
2012-06-15 18:46:14 +02:00
|
|
|
'count': polls[name].votes[vote], 'user': event.user}));
|
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 18:51:45 +02:00
|
|
|
'~pdesc': function(event) {
|
2012-06-15 00:57:07 +02:00
|
|
|
var name = event.input[1];
|
|
|
|
if(polls.hasOwnProperty(name)) {
|
2012-06-15 18:51:45 +02:00
|
|
|
event.reply(name + ': ' + polls[name].description + ' - http://nc.no.de:443/polls/' + name);
|
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-17 13:53:52 +02:00
|
|
|
commands['~addoption'].regex = [/~addoption ([^ ]+) ([^ ]+)/, 3];
|
|
|
|
commands['~rmoption'].regex = [/~rmoption ([^ ]+) ([^ ]+)/, 3];
|
2012-06-15 00:42:06 +02:00
|
|
|
commands['~vote'].regex = [/~vote ([^ ]+) ([^ ]+)/, 3];
|
2012-06-15 18:51:45 +02:00
|
|
|
commands['~pdesc'].regex = [/~pdesc ([^ ]+)/, 2];
|
2012-06-13 22:11:00 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
'name': 'poll',
|
|
|
|
'ignorable': true,
|
|
|
|
'commands': commands
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return poll(dbot);
|
|
|
|
}
|