diff --git a/commandmanager.js b/commandmanager.js index 04dba47..018b339 100644 --- a/commandmanager.js +++ b/commandmanager.js @@ -43,7 +43,7 @@ module.exports.loadCommands = function() { addCommand('EditRule', 'editrule', undefined, 'editrule [rule number] [new rule]', 'Edits the corresponding rule for the server the command was issued on', true, MultiServerCommands.editrule); //vote commands - addCommand('Poll', 'poll', undefined, 'poll start [option1/option2...] or poll stop', 'Starts/stops an automated poll with the options given as a third argument to \'poll start\' and are seperated with a \'/\' with no spaces', false, MultiServerCommands.poll); + addCommand('Poll', 'poll', undefined, 'poll start [option1/option2...] or poll stop', 'Starts/stops an automated poll with the options given as a third argument to \'poll start\' and are seperated with a \'/\' with no spaces, stopping a poll will produce the results', false, MultiServerCommands.poll); addCommand('Vote', 'vote', undefined, 'vote [option]', 'Places your vote on the option you chose, based on the running poll in the server the \'poll start\' command was issued in', false, MultiServerCommands.vote); //admin commands diff --git a/commands/multiservercommands.js b/commands/multiservercommands.js index cc3a625..e49050e 100644 --- a/commands/multiservercommands.js +++ b/commands/multiservercommands.js @@ -145,8 +145,41 @@ module.exports.poll = async function(message, msg, args, discordclient) { return; } } else if (args[1] == 'stop') { - Math.max(polls[message.guild.id].votes) + if (polls[message.guild.id]) { + var results = polls[message.guild.id].votes + .map((x, i) => + { + return { + count: x, + value: polls[message.guild.id].options[i] + }; + } + ) + .sort((a, b) => b.count - a.count) + .filter((x, i, arr) => x.count == arr[0].count) + //.map(x => x.value) + + var winners = ''; + var votesForWinners = ''; + + for (var i = 0; i < results.length; i++) { + winners += results[i].value + ' and '; + votesForWinners = results[i].count; + } + winners = winners.substring(0, winners.length - 5); + + var em = new Discord.RichEmbed(); + em.setTitle('Poll Results'); + em.setColor('BLUE'); + em.addField(winners + ' wins!', 'with ' + votesForWinners + ' vote(s)'); + message.channel.send(em); + delete polls[message.guild.id]; + return; + } else { + message.channel.send(':no_entry_sign: \`There are no polls running, you can type \'poll start\' to start a new poll\`'); + return; + } } else { message.channel.send(':no_entry_sign: \`Incorrect arguments given, try \'help poll\' for usage\`'); return; @@ -163,7 +196,7 @@ module.exports.vote = async function(message, msg, args, discordclient) { } } if (!hasVoted) { - var option = args[1].toLowerCase(); + var option = args.slice(1).join(" "); var index; for (var i = 0; i < poll.options.length; i++) { @@ -183,8 +216,7 @@ module.exports.vote = async function(message, msg, args, discordclient) { polls[message.guild.id] = poll; message.channel.send(`${message.author} voted for ${option}!`); - - console.log(poll); + //console.log(polls); } else { message.channel.send(':no_entry_sign: \`You have allready voted\`'); } @@ -204,7 +236,7 @@ async function startPoll(messageObj, args) { } async function parseOptions(args) { - return args[2].toLowerCase().split('/'); + return args.slice(2).join(" ").match(/(\\.|[^/])+/g) } async function getVotes(options) {