vylbot-app/commands/poll.js

137 lines
6 KiB
JavaScript
Raw Normal View History

// Required components
2020-11-04 17:00:23 +00:00
const { command } = require('vylbot-core');
const { MessageEmbed } = require('discord.js');
// Command variables
2020-11-04 17:00:23 +00:00
const embedColor = "0x3050ba";
// Command class
2020-11-04 17:00:23 +00:00
class poll extends command {
constructor() {
// Set the command's run method, description, category, and example usage
2020-11-04 17:00:23 +00:00
super("poll");
super.description = "Generates a poll with reaction numbers";
super.category = "General";
super.usage = "<title>;<option 1>;<option 2>...";
}
// Run method
2020-11-04 17:00:23 +00:00
poll(context) {
// Get the command's arguments, and split them by a semicolon rather than a space
// This allows the variables to be able to use spaces in them
2020-11-04 17:00:23 +00:00
let args = context.arguments;
let argsJoined = args.join(' ');
args = argsJoined.split(';');
// If the argument has 3 or more arguments and less than 11 arguments
// This allows the title and 2-9 options
2020-11-04 17:00:23 +00:00
if (args.length >= 3 && args.length < 11) {
// Set the title to the first argument
2020-11-04 17:00:23 +00:00
let title = args[0];
let optionString = "";
// Array used to get the numbers as their words
// arrayOfNumbers[n] = "n written in full words"
2020-11-04 17:00:23 +00:00
let arrayOfNumbers = [
'zero',
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine'
];
// Loop through all the arguments after the title
// Add them to the optionString, with their index turned into a number emoji
// Example: :one: Option 1
2020-11-04 17:00:23 +00:00
for (let i = 1; i < args.length; i++) {
optionString += `:${arrayOfNumbers[i]}: ${args[i]}\n`;
}
// Create the embed with the title at the top of the description with the options below
2020-11-04 17:00:23 +00:00
let embed = new MessageEmbed()
.setColor(embedColor)
.setDescription(`**${title}**\n\n${optionString}`);
// Send the embed and then react with the numbers for users to react with,
// the bot will determine how many to react with for the amount of options inputted
2020-11-04 17:00:23 +00:00
context.message.channel.send(embed).then(message => {
if (args.length == 2) {
message.react("1⃣");
} else if (args.length == 3) {
message.react("1⃣")
.then(() => message.react("2⃣"));
} else if (args.length == 4) {
message.react("1⃣")
.then(() => message.react("2⃣"))
.then(() => message.react("3⃣"));
} else if (args.length == 5) {
message.react("1⃣")
.then(() => message.react("2⃣"))
.then(() => message.react("3⃣"))
.then(() => message.react("4⃣"));
} else if (args.length == 6) {
message.react("1⃣")
.then(() => message.react("2⃣"))
.then(() => message.react("3⃣"))
.then(() => message.react("4⃣"))
.then(() => message.react("5⃣"));
} else if (args.length == 7) {
message.react("1⃣")
.then(() => message.react("2⃣"))
.then(() => message.react("3⃣"))
.then(() => message.react("4⃣"))
.then(() => message.react("5⃣"))
.then(() => message.react("6⃣"));
} else if (args.length == 8) {
message.react("1⃣")
.then(() => message.react("2⃣"))
.then(() => message.react("3⃣"))
.then(() => message.react("4⃣"))
.then(() => message.react("5⃣"))
.then(() => message.react("6⃣"))
.then(() => message.react("7⃣"));
} else if (args.length == 9) {
message.react("1⃣")
.then(() => message.react("2⃣"))
.then(() => message.react("3⃣"))
.then(() => message.react("4⃣"))
.then(() => message.react("5⃣"))
.then(() => message.react("6⃣"))
.then(() => message.react("7⃣"))
.then(() => message.react("8⃣"));
} else if (args.length == 10) {
message.react("1⃣")
.then(() => message.react("2⃣"))
.then(() => message.react("3⃣"))
.then(() => message.react("4⃣"))
.then(() => message.react("5⃣"))
.then(() => message.react("6⃣"))
.then(() => message.react("7⃣"))
.then(() => message.react("8⃣"))
.then(() => message.react("9⃣"));
}
}).catch(console.error);
// Delete the message
2020-11-04 17:00:23 +00:00
context.message.delete();
} else if (args.length >= 11) { // If the user inputted more than 9 options
2020-11-04 17:00:23 +00:00
let errorEmbed = new MessageEmbed()
.setDescription("The poll command can only accept up to 9 options");
context.message.channel.send(errorEmbed);
} else { // If the user didn't give enough data
2020-11-04 17:00:23 +00:00
let errorEmbed = new MessageEmbed()
.setDescription("Please use the correct usage: <title>;<option 1>;<option 2>... (separate options with semicolons)");
context.message.channel.send(errorEmbed);
}
}
}
module.exports = poll;