Added basic poll functionality

This commit is contained in:
plane000
2018-05-21 21:55:33 +01:00
parent 427db61faf
commit 0867cc0dba
4 changed files with 114 additions and 10 deletions

View File

@@ -11,7 +11,6 @@ module.exports.stop = async function(message, msg, args, discordclient) {
message.channel.send(':white_check_mark: \`Disconnected...\`');
Logger.failed('Disconnected');
discordclient.destroy();
//discordclient.disconnect();
}
module.exports.reload = async function(message, msg, args, discordclient) {

View File

@@ -6,6 +6,7 @@ const Helper = require('../helper.js');
/*message object, messaage full, message args, discord client*/
/*rule commands*/
module.exports.rules = async function(message, msg, args, discordclient) {
var serverName = message.guild.name;
var serverID = message.guild.id;
@@ -104,7 +105,7 @@ module.exports.editrule = async function(message, msg, args, discordclient) {
serversConfig[serverID].rules = Rules;
Config.writeToFile();
} catch (e) {
message.channel.send(`:no_entry_sign: \`That is not a valid rule\``)
message.channel.send(`:no_entry_sign: \`That is not a valid rule\``);
return;
}
var em = new Discord.RichEmbed();
@@ -113,3 +114,103 @@ module.exports.editrule = async function(message, msg, args, discordclient) {
em.addField(`Rule ${args[1]}:`, newRule);
message.channel.send(em);
}
/*poll commands*/
var polls = {};
module.exports.poll = async function(message, msg, args, discordclient) {
if (args[1] == 'start') {
if (args[2]) {
if (!polls[message.guild.id]) {
await startPoll(message, args);
var options = '';
for (i in polls[message.guild.id].options) {
options += polls[message.guild.id].options[i] + ', ';
}
options = options.substring(0, options.length - 2);
var em = new Discord.RichEmbed();
em.setTitle('Poll started!');
em.setColor('BLUE');
em.addField('With the options:', options)
em.setFooter('Type \'vote [option]\' to vote for that option');
message.channel.send(em);
return;
} else {
message.channel.send(':no_entry_sign: \`There is allready a poll running, you can type \'poll stop\' to stop it and find its results\`');
return;
}
} else {
message.channel.send(':no_entry_sign: \`No options given, try \'help poll\' for usage\`');
return;
}
} else if (args[1] == 'stop') {
Math.max(polls[message.guild.id].votes)
} else {
message.channel.send(':no_entry_sign: \`Incorrect arguments given, try \'help poll\' for usage\`');
return;
}
}
module.exports.vote = async function(message, msg, args, discordclient) {
if (polls[message.guild.id]) {
var poll = polls[message.guild.id];
var hasVoted = false;
for (var i = 0; i < poll.voted.length; i++) {
if (poll.voted[i] == message.author.id) {
hasVoted = true;
}
}
if (!hasVoted) {
var option = args[1].toLowerCase();
var index;
for (var i = 0; i < poll.options.length; i++) {
if (poll.options[i] == option) {
index = i
poll.votes[i]++;
break;
}
}
if (index == undefined) {
message.channel.send(':no_entry_sign: \`That is not an option!\`');
return;
}
poll.voted.push(message.author.id);
polls[message.guild.id] = poll;
message.channel.send(`${message.author} voted for ${option}!`);
console.log(poll);
} else {
message.channel.send(':no_entry_sign: \`You have allready voted\`');
}
} else {
message.channel.send(':no_entry_sign: \`There are no polls running at the moment, use \'poll start\' to start one\`');
}
}
async function startPoll(messageObj, args) {
var parsedOptions = await parseOptions(args);
var parsedVotes = await getVotes(parsedOptions)
polls[messageObj.guild.id] = {
options: parsedOptions,
votes: parsedVotes,
voted: []
}
}
async function parseOptions(args) {
return args[2].toLowerCase().split('/');
}
async function getVotes(options) {
var votes = [];
for (i in options) {
votes.push('0');
}
return votes;
}