Finished voteing system

This commit is contained in:
plane000
2018-05-22 11:15:23 +01:00
parent 0867cc0dba
commit c0529d5dab
2 changed files with 38 additions and 6 deletions

View File

@@ -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); 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 //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); 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 //admin commands

View File

@@ -145,8 +145,41 @@ module.exports.poll = async function(message, msg, args, discordclient) {
return; return;
} }
} else if (args[1] == 'stop') { } 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 { } else {
message.channel.send(':no_entry_sign: \`Incorrect arguments given, try \'help poll\' for usage\`'); message.channel.send(':no_entry_sign: \`Incorrect arguments given, try \'help poll\' for usage\`');
return; return;
@@ -163,7 +196,7 @@ module.exports.vote = async function(message, msg, args, discordclient) {
} }
} }
if (!hasVoted) { if (!hasVoted) {
var option = args[1].toLowerCase(); var option = args.slice(1).join(" ");
var index; var index;
for (var i = 0; i < poll.options.length; i++) { 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; polls[message.guild.id] = poll;
message.channel.send(`${message.author} voted for ${option}!`); message.channel.send(`${message.author} voted for ${option}!`);
//console.log(polls);
console.log(poll);
} else { } else {
message.channel.send(':no_entry_sign: \`You have allready voted\`'); message.channel.send(':no_entry_sign: \`You have allready voted\`');
} }
@@ -204,7 +236,7 @@ async function startPoll(messageObj, args) {
} }
async function parseOptions(args) { async function parseOptions(args) {
return args[2].toLowerCase().split('/'); return args.slice(2).join(" ").match(/(\\.|[^/])+/g)
} }
async function getVotes(options) { async function getVotes(options) {