From e7169d960a8e15e24b26266af7515aa2d672e88f Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 29 Nov 2021 15:34:57 +0000 Subject: [PATCH] Migrate poll command --- commands/partner.js | 60 ----------------- commands/poll.js | 150 ------------------------------------------- src/commands/poll.ts | 56 ++++++++++++++++ 3 files changed, 56 insertions(+), 210 deletions(-) delete mode 100644 commands/partner.js delete mode 100644 commands/poll.js create mode 100644 src/commands/poll.ts diff --git a/commands/partner.js b/commands/partner.js deleted file mode 100644 index bf59622..0000000 --- a/commands/partner.js +++ /dev/null @@ -1,60 +0,0 @@ -// Required components -const { command } = require('vylbot-core'); -const { MessageEmbed } = require('discord.js'); -const { existsSync, readFileSync } = require('fs'); - -// Command Variables -const embedColor = "0x3050ba"; - -// Command class -class partner extends command { - constructor() { - // Set the command's run method, description, and category - super("partner"); - super.description = "Generates the embeds for the partner from the partners.json file"; - super.category = "Admin"; - - // Require in the config the name of the admin role and the rules file name - super.requiredConfigs = "adminrole"; - super.requiredConfigs = "partnersfile"; - } - - // Run method - partner(context) { - if (context.message.member.roles.cache.find(role => role.name == context.client.config.partner.adminrole)) { - if (existsSync(context.client.config.partner.partnersfile)) { - const partnerJson = JSON.parse(readFileSync(context.client.config.partner.partnersfile)); - - for (const i in partnerJson) { - const serverName = partnerJson[i].name; - const serverInvite = partnerJson[i].invite; - const serverDescription = partnerJson[i].description; - const serverIcon = partnerJson[i].icon; - - const embed = new MessageEmbed() - .setColor(embedColor) - .setTitle(serverName) - .setDescription(serverDescription) - .setURL(serverInvite) - .setThumbnail(serverIcon); - - context.message.channel.send(embed); - } - } else { - const errorEmbed = new MessageEmbed() - .setColor(embedColor) - .setDescription('File does not exist'); - - context.message.channel.send(errorEmbed); - } - } else { - const errorEmbed = new MessageEmbed() - .setColor(embedColor) - .setDescription('You do not have permission to run this command'); - - context.message.channel.send(errorEmbed); - } - } -} - -module.exports = partner; \ No newline at end of file diff --git a/commands/poll.js b/commands/poll.js deleted file mode 100644 index 7afe92e..0000000 --- a/commands/poll.js +++ /dev/null @@ -1,150 +0,0 @@ -// Required components -const { command } = require('vylbot-core'); -const { MessageEmbed } = require('discord.js'); -const emojiRegex = require('emoji-regex/RGI_Emoji'); - -// Command variables -const embedColor = "0x3050ba"; - -// Command class -class poll extends command { - constructor() { - // Set the command's run method, description, category, and example usage - super("poll"); - super.description = "Generates a poll with reaction numbers"; - super.category = "General"; - super.usage = ";<option 1>;<option 2>..."; - } - - // Run method - 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 - let args = context.arguments; - const 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 - if (args.length >= 3 && args.length < 11) { - // Set the title to the first argument - const title = args[0]; - let optionString = ""; - - // Array used to get the numbers as their words - // arrayOfNumbers[n] = "n written in full words" - const arrayOfNumbers = [ - ':zero:', - ':one:', - ':two:', - ':three:', - ':four:', - ':five:', - ':six:', - ':seven:', - ':eight:', - ':nine:' - ]; - - // Array containing the numbers as their emoji - const reactionEmojis = ["0️⃣", "1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "8️⃣", "9️⃣"]; - - // 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 - for (let i = 1; i < args.length; i++) { - // 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`; - } - - // Create the embed with the title at the top of the description with the options below - const 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 - context.message.channel.send(embed).then(message => { - if (args.length == 2) { - message.react(reactionEmojis[1]); - } else if (args.length == 3) { - message.react(reactionEmojis[1]) - .then(() => message.react(reactionEmojis[2])); - } else if (args.length == 4) { - message.react(reactionEmojis[1]) - .then(() => message.react(reactionEmojis[2])) - .then(() => message.react(reactionEmojis[3])); - } else if (args.length == 5) { - message.react(reactionEmojis[1]) - .then(() => message.react(reactionEmojis[2])) - .then(() => message.react(reactionEmojis[3])) - .then(() => message.react(reactionEmojis[4])); - } else if (args.length == 6) { - message.react(reactionEmojis[1]) - .then(() => message.react(reactionEmojis[2])) - .then(() => message.react(reactionEmojis[3])) - .then(() => message.react(reactionEmojis[4])) - .then(() => message.react(reactionEmojis[5])); - } else if (args.length == 7) { - 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])); - } else if (args.length == 8) { - 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])); - } else if (args.length == 9) { - 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])); - } else if (args.length == 10) { - 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])); - } - }).catch(console.error); - - // Delete the message - context.message.delete(); - } else if (args.length >= 11) { // If the user inputted more than 9 options - const 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 - const 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; diff --git a/src/commands/poll.ts b/src/commands/poll.ts new file mode 100644 index 0000000..3d53194 --- /dev/null +++ b/src/commands/poll.ts @@ -0,0 +1,56 @@ +import { Command, ICommandContext } from "vylbot-core"; +import ErrorEmbed from "../helpers/ErrorEmbed"; +import PublicEmbed from "../helpers/PublicEmbed"; + +export default class Poll extends Command { + constructor() { + super(); + + super._category = "General"; + } + + public override async execute(context: ICommandContext) { + const argsJoined = context.args.join(" "); + const argsSplit = argsJoined.split(";"); + + if (argsSplit.length < 3 || argsSplit.length > 10) { + const errorEmbed = new ErrorEmbed(context, "Usage: <title>;<option 1>;<option 2>... (separate options with semicolons), maximum of 9 options"); + errorEmbed.SendToCurrentChannel(); + return; + } + + const title = argsSplit[0]; + + const arrayOfNumbers = [ + ':one:', + ':two:', + ':three:', + ':four:', + ':five:', + ':six:', + ':seven:', + ':eight:', + ':nine:' + ]; + + const reactionEmojis = ["1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "8️⃣", "9️⃣"]; + + const description = arrayOfNumbers.splice(0, argsSplit.length - 1); + + description.forEach((value, index) => { + description[index] = `${value} ${argsSplit[index + 1]}`; + }); + + const embed = new PublicEmbed(context, title, description.join("\n")); + + const message = await context.message.channel.send(embed); + + description.forEach(async (value, index) => { + await message.react(reactionEmojis[index]); + }); + + if (context.message.deletable) { + await context.message.delete({ reason: "Poll command" }); + } + } +} \ No newline at end of file