Added basic poll functionality
This commit is contained in:
@@ -4,7 +4,7 @@ const fs = require('fs');
|
|||||||
const Logger = require('./logger');
|
const Logger = require('./logger');
|
||||||
const Config = require('./config');
|
const Config = require('./config');
|
||||||
const Commands = require('./commands/commands')
|
const Commands = require('./commands/commands')
|
||||||
const RuleCommands = require('./commands/rulecommands')
|
const MultiServerCommands = require('./commands/multiservercommands')
|
||||||
const AdminCommands = require('./commands/admincommands')
|
const AdminCommands = require('./commands/admincommands')
|
||||||
const CommandManager = require('./main');
|
const CommandManager = require('./main');
|
||||||
|
|
||||||
@@ -30,17 +30,21 @@ module.exports.loadCommands = function() {
|
|||||||
addCommand('Help', 'help', undefined, 'help [command] or [help]', 'Returns a usage chart for the command requested, or returns a link to a command list', false, Commands.help);
|
addCommand('Help', 'help', undefined, 'help [command] or [help]', 'Returns a usage chart for the command requested, or returns a link to a command list', false, Commands.help);
|
||||||
addCommand('Say', 'say', undefined, 'say [input]', 'Repeats the input', false, Commands.say);
|
addCommand('Say', 'say', undefined, 'say [input]', 'Repeats the input', false, Commands.say);
|
||||||
addCommand('Version', 'version', undefined, 'version', 'Returns the version', false, Commands.version);
|
addCommand('Version', 'version', undefined, 'version', 'Returns the version', false, Commands.version);
|
||||||
addCommand('Ping', 'ping', undefined, 'ping', 'Returns round trip to discords servers', false, Commands.ping);
|
addCommand('Ping', 'ping', undefined, 'ping', 'Returns round trip time to discords servers', false, Commands.ping);
|
||||||
addCommand('Cat', 'cat', undefined, 'cat', 'Returns a picture of a cat', false, Commands.cat);
|
addCommand('Cat', 'cat', undefined, 'cat', 'Returns a picture of a cat', false, Commands.cat);
|
||||||
addCommand('Dog', 'dog', undefined, 'dog', 'Returns a picture of a dog', false, Commands.dog);
|
addCommand('Dog', 'dog', undefined, 'dog', 'Returns a picture of a dog', false, Commands.dog);
|
||||||
addCommand('Undo', 'undo', undefined, 'undo', 'Deletes last message sent by Jeffery from the channel the command was issued in', false, Commands.undo);
|
addCommand('Undo', 'undo', undefined, 'undo', 'Deletes last message sent by Jeffery from the channel the command was issued in', false, Commands.undo);
|
||||||
|
|
||||||
//rule commands
|
//rule commands
|
||||||
addCommand('Rules', 'rules', undefined, 'rules', 'Returns all the rules for the server the command was issued on', false, RuleCommands.rules);
|
addCommand('Rules', 'rules', undefined, 'rules', 'Returns all the rules for the server the command was issued on', false, MultiServerCommands.rules);
|
||||||
addCommand('Rule', 'rule', undefined, 'rule [rule number]', 'Returns corresponding rule for the server the command was issued on', false, RuleCommands.rule);
|
addCommand('Rule', 'rule', undefined, 'rule [rule number]', 'Returns corresponding rule for the server the command was issued on', false, MultiServerCommands.rule);
|
||||||
addCommand('AddRule', 'addrule', undefined, 'addrule [new rule]', 'Adds a rule to the rule list for the server the command was issued on', true, RuleCommands.addrule);
|
addCommand('Add Rule', 'addrule', undefined, 'addrule [new rule]', 'Adds a rule to the rule list for the server the command was issued on', true, MultiServerCommands.addrule);
|
||||||
addCommand('DelRule', 'delrule', undefined, 'delrule [rule number]', 'Deletes the corresponding rule for the server the command was issued on', true, RuleCommands.delrule);
|
addCommand('Delete Rule', 'delrule', undefined, 'delrule [rule number]', 'Deletes the corresponding rule for the server the command was issued on', true, MultiServerCommands.delrule);
|
||||||
addCommand('EditRule', 'editrule', undefined, 'editrule [rule number] [new rule]', 'Edits the corresponding rule for the server the command was issued on', true, RuleCommands.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
|
||||||
|
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('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
|
||||||
addCommand('Shutdown', 'stop', undefined, 'stop', 'Shutsdown JefferyBot', true, AdminCommands.stop);
|
addCommand('Shutdown', 'stop', undefined, 'stop', 'Shutsdown JefferyBot', true, AdminCommands.stop);
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ module.exports.stop = async function(message, msg, args, discordclient) {
|
|||||||
message.channel.send(':white_check_mark: \`Disconnected...\`');
|
message.channel.send(':white_check_mark: \`Disconnected...\`');
|
||||||
Logger.failed('Disconnected');
|
Logger.failed('Disconnected');
|
||||||
discordclient.destroy();
|
discordclient.destroy();
|
||||||
//discordclient.disconnect();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.reload = async function(message, msg, args, discordclient) {
|
module.exports.reload = async function(message, msg, args, discordclient) {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ const Helper = require('../helper.js');
|
|||||||
|
|
||||||
/*message object, messaage full, message args, discord client*/
|
/*message object, messaage full, message args, discord client*/
|
||||||
|
|
||||||
|
/*rule commands*/
|
||||||
module.exports.rules = async function(message, msg, args, discordclient) {
|
module.exports.rules = async function(message, msg, args, discordclient) {
|
||||||
var serverName = message.guild.name;
|
var serverName = message.guild.name;
|
||||||
var serverID = message.guild.id;
|
var serverID = message.guild.id;
|
||||||
@@ -104,7 +105,7 @@ module.exports.editrule = async function(message, msg, args, discordclient) {
|
|||||||
serversConfig[serverID].rules = Rules;
|
serversConfig[serverID].rules = Rules;
|
||||||
Config.writeToFile();
|
Config.writeToFile();
|
||||||
} catch (e) {
|
} 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;
|
return;
|
||||||
}
|
}
|
||||||
var em = new Discord.RichEmbed();
|
var em = new Discord.RichEmbed();
|
||||||
@@ -113,3 +114,103 @@ module.exports.editrule = async function(message, msg, args, discordclient) {
|
|||||||
em.addField(`Rule ${args[1]}:`, newRule);
|
em.addField(`Rule ${args[1]}:`, newRule);
|
||||||
message.channel.send(em);
|
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;
|
||||||
|
}
|
||||||
2
main.js
2
main.js
@@ -89,7 +89,7 @@ client.on('message', async (message) => {
|
|||||||
if (message.content.startsWith(Config.getconfig().Prefix)) {
|
if (message.content.startsWith(Config.getconfig().Prefix)) {
|
||||||
Logger.logMSG(message);
|
Logger.logMSG(message);
|
||||||
var msg = message.content.toLowerCase().substring(Config.getconfig().Prefix.length);
|
var msg = message.content.toLowerCase().substring(Config.getconfig().Prefix.length);
|
||||||
var args = message.content.substring(Config.getconfig().Prefix.length).split(" ");
|
var args = message.content.substring(Config.getconfig().Prefix.length).split(' ');
|
||||||
args[0] = args[0].toLowerCase();
|
args[0] = args[0].toLowerCase();
|
||||||
|
|
||||||
/*command manager checks if command exists*/
|
/*command manager checks if command exists*/
|
||||||
|
|||||||
Reference in New Issue
Block a user