2020-11-07 15:16:54 +00:00
|
|
|
|
// Required components
|
2020-11-04 17:00:23 +00:00
|
|
|
|
const { command } = require('vylbot-core');
|
|
|
|
|
const { MessageEmbed } = require('discord.js');
|
2021-02-17 18:12:45 +00:00
|
|
|
|
const emojiRegex = require('emoji-regex/RGI_Emoji');
|
2020-11-04 17:00:23 +00:00
|
|
|
|
|
2020-11-07 15:16:54 +00:00
|
|
|
|
// Command variables
|
2020-11-04 17:00:23 +00:00
|
|
|
|
const embedColor = "0x3050ba";
|
|
|
|
|
|
2020-11-07 15:16:54 +00:00
|
|
|
|
// Command class
|
2020-11-04 17:00:23 +00:00
|
|
|
|
class poll extends command {
|
|
|
|
|
constructor() {
|
2020-11-07 15:16:54 +00:00
|
|
|
|
// 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>...";
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-07 15:16:54 +00:00
|
|
|
|
// Run method
|
2020-11-04 17:00:23 +00:00
|
|
|
|
poll(context) {
|
2020-11-07 15:16:54 +00:00
|
|
|
|
// 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;
|
2021-02-17 18:12:45 +00:00
|
|
|
|
const argsJoined = args.join(' ');
|
2020-11-04 17:00:23 +00:00
|
|
|
|
args = argsJoined.split(';');
|
|
|
|
|
|
2020-11-07 15:16:54 +00:00
|
|
|
|
// 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) {
|
2020-11-07 15:16:54 +00:00
|
|
|
|
// Set the title to the first argument
|
2021-02-17 18:12:45 +00:00
|
|
|
|
const title = args[0];
|
2020-11-04 17:00:23 +00:00
|
|
|
|
let optionString = "";
|
|
|
|
|
|
2020-11-07 15:16:54 +00:00
|
|
|
|
// Array used to get the numbers as their words
|
|
|
|
|
// arrayOfNumbers[n] = "n written in full words"
|
2021-02-17 18:12:45 +00:00
|
|
|
|
const arrayOfNumbers = [
|
|
|
|
|
':zero:',
|
|
|
|
|
':one:',
|
|
|
|
|
':two:',
|
|
|
|
|
':three:',
|
|
|
|
|
':four:',
|
|
|
|
|
':five:',
|
|
|
|
|
':six:',
|
|
|
|
|
':seven:',
|
|
|
|
|
':eight:',
|
|
|
|
|
':nine:'
|
2020-11-04 17:00:23 +00:00
|
|
|
|
];
|
|
|
|
|
|
2021-02-17 18:12:45 +00:00
|
|
|
|
// Array containing the numbers as their emoji
|
|
|
|
|
const reactionEmojis = ["0️⃣", "1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "8️⃣", "9️⃣"];
|
|
|
|
|
|
2020-11-07 15:16:54 +00:00
|
|
|
|
// 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++) {
|
2021-02-17 18:12:45 +00:00
|
|
|
|
// If the option contains an emoji, replace the emoji with it
|
|
|
|
|
const regex = emojiRegex();
|
|
|
|
|
const match = regex.exec(args[i]);
|
|
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
|
const emoji = match[0];
|
|
|
|
|
reactionEmojis[i] = emoji;
|
|
|
|
|
arrayOfNumbers[i] = '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
optionString += `${arrayOfNumbers[i]} ${args[i]}\n`;
|
2020-11-04 17:00:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-07 15:16:54 +00:00
|
|
|
|
// Create the embed with the title at the top of the description with the options below
|
2021-02-17 18:12:45 +00:00
|
|
|
|
const embed = new MessageEmbed()
|
2020-11-04 17:00:23 +00:00
|
|
|
|
.setColor(embedColor)
|
|
|
|
|
.setDescription(`**${title}**\n\n${optionString}`);
|
|
|
|
|
|
2020-11-07 15:16:54 +00:00
|
|
|
|
// 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) {
|
2021-02-17 18:12:45 +00:00
|
|
|
|
message.react(reactionEmojis[1]);
|
2020-11-04 17:00:23 +00:00
|
|
|
|
} else if (args.length == 3) {
|
2021-02-17 18:12:45 +00:00
|
|
|
|
message.react(reactionEmojis[1])
|
|
|
|
|
.then(() => message.react(reactionEmojis[2]));
|
2020-11-04 17:00:23 +00:00
|
|
|
|
} else if (args.length == 4) {
|
2021-02-17 18:12:45 +00:00
|
|
|
|
message.react(reactionEmojis[1])
|
|
|
|
|
.then(() => message.react(reactionEmojis[2]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[3]));
|
2020-11-04 17:00:23 +00:00
|
|
|
|
} else if (args.length == 5) {
|
2021-02-17 18:12:45 +00:00
|
|
|
|
message.react(reactionEmojis[1])
|
|
|
|
|
.then(() => message.react(reactionEmojis[2]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[3]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[4]));
|
2020-11-04 17:00:23 +00:00
|
|
|
|
} else if (args.length == 6) {
|
2021-02-17 18:12:45 +00:00
|
|
|
|
message.react(reactionEmojis[1])
|
|
|
|
|
.then(() => message.react(reactionEmojis[2]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[3]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[4]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[5]));
|
2020-11-04 17:00:23 +00:00
|
|
|
|
} else if (args.length == 7) {
|
2021-02-17 18:12:45 +00:00
|
|
|
|
message.react(reactionEmojis[1])
|
|
|
|
|
.then(() => message.react(reactionEmojis[2]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[3]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[4]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[5]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[6]));
|
2020-11-04 17:00:23 +00:00
|
|
|
|
} else if (args.length == 8) {
|
2021-02-17 18:12:45 +00:00
|
|
|
|
message.react(reactionEmojis[1])
|
|
|
|
|
.then(() => message.react(reactionEmojis[2]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[3]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[4]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[5]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[6]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[7]));
|
2020-11-04 17:00:23 +00:00
|
|
|
|
} else if (args.length == 9) {
|
2021-02-17 18:12:45 +00:00
|
|
|
|
message.react(reactionEmojis[1])
|
|
|
|
|
.then(() => message.react(reactionEmojis[2]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[3]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[4]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[5]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[6]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[7]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[8]));
|
2020-11-04 17:00:23 +00:00
|
|
|
|
} else if (args.length == 10) {
|
2021-02-17 18:12:45 +00:00
|
|
|
|
message.react(reactionEmojis[1])
|
|
|
|
|
.then(() => message.react(reactionEmojis[2]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[3]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[4]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[5]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[6]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[7]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[8]))
|
|
|
|
|
.then(() => message.react(reactionEmojis[9]));
|
2020-11-04 17:00:23 +00:00
|
|
|
|
}
|
|
|
|
|
}).catch(console.error);
|
|
|
|
|
|
2020-11-07 15:16:54 +00:00
|
|
|
|
// Delete the message
|
2020-11-04 17:00:23 +00:00
|
|
|
|
context.message.delete();
|
2020-11-07 15:16:54 +00:00
|
|
|
|
} else if (args.length >= 11) { // If the user inputted more than 9 options
|
2021-02-17 18:12:45 +00:00
|
|
|
|
const errorEmbed = new MessageEmbed()
|
2020-11-04 17:00:23 +00:00
|
|
|
|
.setDescription("The poll command can only accept up to 9 options");
|
|
|
|
|
|
|
|
|
|
context.message.channel.send(errorEmbed);
|
2020-11-07 15:16:54 +00:00
|
|
|
|
} else { // If the user didn't give enough data
|
2021-02-17 18:12:45 +00:00
|
|
|
|
const errorEmbed = new MessageEmbed()
|
2020-11-04 17:00:23 +00:00
|
|
|
|
.setDescription("Please use the correct usage: <title>;<option 1>;<option 2>... (separate options with semicolons)");
|
|
|
|
|
|
|
|
|
|
context.message.channel.send(errorEmbed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-07 15:16:54 +00:00
|
|
|
|
module.exports = poll;
|