~addoption and ~rmoption

This commit is contained in:
Luke Slater 2012-06-17 12:51:25 +01:00
parent d1daf51c67
commit 8837cd7b98
2 changed files with 59 additions and 0 deletions

View File

@ -2,6 +2,7 @@ var poll = function(dbot) {
if(!dbot.db.hasOwnProperty('polls')) { if(!dbot.db.hasOwnProperty('polls')) {
dbot.db.polls = {}; dbot.db.polls = {};
} }
var polls = dbot.db.polls; var polls = dbot.db.polls;
var commands = { var commands = {
'~newpoll': function(event) { '~newpoll': function(event) {
@ -33,6 +34,50 @@ var poll = function(dbot) {
} }
}, },
'~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;
event.reply(dbot.t('option_added'), {'user': event.user,
'name': name, 'option': option});
} else {
event.reply(dbot.t('option_exists'), {'option': option,
'name': name, 'user': event.user});
}
} 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];
event.reply(dbot.t('option_removed'), {'user': event.user,
'name': name, 'option': option});
} else {
event.reply(dbot.t('invalid_vote'), {'vote': option});
}
} else {
event.reply(dbot.t('not_poll_owner', {'name': name}));
}
} else {
event.reply(dbot.t('poll_unexistent', {'name': name}));
}
},
'~vote': function(event) { '~vote': function(event) {
var name = event.input[1]; var name = event.input[1];
var vote = event.input[2]; var vote = event.input[2];
@ -70,6 +115,8 @@ var poll = function(dbot) {
} }
}; };
commands['~newpoll'].regex = [/~newpoll ([^ ]+) \[options=([^ ]+)\] (.+)/, 4]; commands['~newpoll'].regex = [/~newpoll ([^ ]+) \[options=([^ ]+)\] (.+)/, 4];
commands['~addoption'].regex = [/~addoption ([^ ]+)/, 2];
commands['~rmoption'].regex = [/~rmoption ([^ ]+)/, 2];
commands['~vote'].regex = [/~vote ([^ ]+) ([^ ]+)/, 3]; commands['~vote'].regex = [/~vote ([^ ]+) ([^ ]+)/, 3];
commands['~pdesc'].regex = [/~pdesc ([^ ]+)/, 2]; commands['~pdesc'].regex = [/~pdesc ([^ ]+)/, 2];

View File

@ -229,5 +229,17 @@
"poll_unexistent": { "poll_unexistent": {
"english": "Poll '{name}' doesn't exist.", "english": "Poll '{name}' doesn't exist.",
"spanish" : "Votación '{name}' no existe." "spanish" : "Votación '{name}' no existe."
},
"option_added": {
"english": "{user}: '{option}' added to '{name}'"
},
"option_exists": {
"english": "{user}: '{option}' already exists in '{name}'"
},
"not_poll_owner": {
"english": "{user}: You don't own the '{name}' poll"
},
"option_removed": {
"english": "{user}: '{option}' removed from '{name}'"
} }
} }