From 8837cd7b986b2e28134d0bddae067d05a18b5c1d Mon Sep 17 00:00:00 2001 From: Luke Slater Date: Sun, 17 Jun 2012 12:51:25 +0100 Subject: [PATCH] ~addoption and ~rmoption --- modules/poll.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ strings.json | 12 ++++++++++++ 2 files changed, 59 insertions(+) diff --git a/modules/poll.js b/modules/poll.js index cebeed9..edd376c 100644 --- a/modules/poll.js +++ b/modules/poll.js @@ -2,6 +2,7 @@ var poll = function(dbot) { if(!dbot.db.hasOwnProperty('polls')) { dbot.db.polls = {}; } + var polls = dbot.db.polls; var commands = { '~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) { var name = event.input[1]; var vote = event.input[2]; @@ -70,6 +115,8 @@ var poll = function(dbot) { } }; commands['~newpoll'].regex = [/~newpoll ([^ ]+) \[options=([^ ]+)\] (.+)/, 4]; + commands['~addoption'].regex = [/~addoption ([^ ]+)/, 2]; + commands['~rmoption'].regex = [/~rmoption ([^ ]+)/, 2]; commands['~vote'].regex = [/~vote ([^ ]+) ([^ ]+)/, 3]; commands['~pdesc'].regex = [/~pdesc ([^ ]+)/, 2]; diff --git a/strings.json b/strings.json index ec682bd..a5d7394 100644 --- a/strings.json +++ b/strings.json @@ -229,5 +229,17 @@ "poll_unexistent": { "english": "Poll '{name}' doesn't exist.", "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}'" } }