From 02df08558076d8ec6c2110fca48ded507c4b4aca Mon Sep 17 00:00:00 2001 From: TheEssem Date: Tue, 10 Mar 2020 17:24:57 -0500 Subject: [PATCH] Converted multiple functions to ES6 syntax, moved from collections to maps for storing commands --- commands/dice.js | 10 +++------- commands/help.js | 18 +++++++++--------- commands/purge.js | 4 ++-- commands/restart.js | 4 ++-- commands/tags.js | 6 +++--- utils/collections.js | 8 ++++---- utils/handler.js | 4 ++-- utils/help.js | 6 +++--- 8 files changed, 28 insertions(+), 32 deletions(-) diff --git a/commands/dice.js b/commands/dice.js index 789b4a3..d039506 100644 --- a/commands/dice.js +++ b/commands/dice.js @@ -1,14 +1,10 @@ const misc = require("../utils/misc.js"); exports.run = async (message, args) => { - if (args.length === 0) { - return `🎲 The dice landed on ${misc.random(Array.from(Array(6).keys())) + 1}.`; + if (args.length === 0 || !args[0].match(/^\d+$/)) { + return `🎲 The dice landed on ${misc.random([...Array(6).keys()]) + 1}.`; } else { - if (args[0].match(/^\d+$/)) { - return `🎲 The dice landed on ${misc.random(Array.from(Array(parseInt(args[0])).keys())) + 1}.`; - } else { - return `🎲 The dice landed on ${misc.random(Array.from(Array(6).keys())) + 1}.`; - } + return `🎲 The dice landed on ${misc.random([...Array(parseInt(args[0])).keys()]) + 1}.`; } }; diff --git a/commands/help.js b/commands/help.js index dba0d7c..2fb77cc 100644 --- a/commands/help.js +++ b/commands/help.js @@ -7,17 +7,17 @@ const tips = ["You can change the bot's prefix using the prefix command.", "Imag exports.run = async (message, args) => { const guild = (await database.guilds.find({ id: message.channel.guild.id }).exec())[0]; - const commands = Array.from(collections.commands.keys()); - const aliases = Array.from(collections.aliases.keys()); - if (args.length !== 0 && (commands.includes(args[0].toLowerCase()) || aliases.includes(args[0].toLowerCase()))) { - const info = aliases.includes(args[0].toLowerCase()) ? collections.info.get(collections.aliases.get(args[0].toLowerCase())) : collections.info.get(args[0].toLowerCase()); + const commands = collections.commands; + const aliases = collections.aliases; + if (args.length !== 0 && (commands.has(args[0].toLowerCase()) || aliases.has(args[0].toLowerCase()))) { + const info = aliases.has(args[0].toLowerCase()) ? collections.info.get(collections.aliases.get(args[0].toLowerCase())) : collections.info.get(args[0].toLowerCase()); const embed = { "embed": { "author": { "name": "esmBot Help", "icon_url": client.user.avatarURL }, - "title": `${guild.prefix}${aliases.includes(args[0].toLowerCase()) ? collections.aliases.get(args[0].toLowerCase()) : args[0].toLowerCase()}`, + "title": `${guild.prefix}${aliases.has(args[0].toLowerCase()) ? collections.aliases.get(args[0].toLowerCase()) : args[0].toLowerCase()}`, "description": info.description, "color": 16711680, "fields": [{ @@ -39,7 +39,7 @@ exports.run = async (message, args) => { images: ["**These commands support the PNG, JPEG, WEBP, and GIF formats. (GIF support is experimental)**\n"], soundboard: [] }; - for (const command of commands) { + for (const [command] of commands) { const category = collections.info.get(command).category; const description = collections.info.get(command).description; const params = collections.info.get(command).params; @@ -48,7 +48,7 @@ exports.run = async (message, args) => { } else if (category === 2) { categories.moderation.push(`**${command}**${params ? ` ${params}` : ""} - ${description}`); } else if (category === 3) { - const subCommands = Array.from(Object.keys(description)); + const subCommands = [...Object.keys(description)]; for (const subCommand of subCommands) { categories.tags.push(`**tags${subCommand !== "default" ? ` ${subCommand}` : ""}**${params[subCommand] ? ` ${params[subCommand]}` : ""} - ${description[subCommand]}`); } @@ -67,12 +67,12 @@ exports.run = async (message, args) => { }).filter((item) => { return item; }); - splitPages.forEach(page => { + for (const page of splitPages) { pages.push({ title: category.charAt(0).toUpperCase() + category.slice(1), page: page }); - }); + } } const embeds = []; for (const [i, value] of pages.entries()) { diff --git a/commands/purge.js b/commands/purge.js index e7ec3f0..aaf563c 100644 --- a/commands/purge.js +++ b/commands/purge.js @@ -5,8 +5,8 @@ exports.run = async (message, args) => { if (!message.channel.guild.members.get(client.user.id).permission.has("manageMessages") && !message.channel.permissionsOf(client.user.id).has("manageMessages")) return `${message.author.mention}, I don't have the \`Manage Messages\` permission!`; if (args.length === 0 || !args[0].match(/^\d+$/)) return `${message.author.mention}, you need to provide the number of messages to purge!`; const numberOfMessages = parseInt(args[0]) + 1; - await message.channel.purge(numberOfMessages); - return `Successfully purged ${args[0]} messages.`; + const number = await message.channel.purge(numberOfMessages); + return `Successfully purged ${number - 1} messages.`; }; exports.aliases = ["prune"]; diff --git a/commands/restart.js b/commands/restart.js index b1ee552..e07bd3c 100644 --- a/commands/restart.js +++ b/commands/restart.js @@ -4,9 +4,9 @@ const collections = require("../utils/collections.js"); exports.run = async (message) => { if (message.author.id !== process.env.OWNER) return `${message.author.mention}, only the bot owner can restart me!`; await message.channel.createMessage(`${message.author.mention}, esmBot is restarting.`); - collections.commands.forEach(async (command) => { + for (const command of collections.commands) { await handler.unload(command); - }); + } process.exit(1); }; diff --git a/commands/tags.js b/commands/tags.js index 3a0ca42..85d6af5 100644 --- a/commands/tags.js +++ b/commands/tags.js @@ -40,8 +40,8 @@ exports.run = async (message, args) => { if (!message.channel.guild.members.get(client.user.id).permission.has("embedLinks") && !message.channel.permissionsOf(client.user.id).has("embedLinks")) return `${message.author.mention}, I don't have the \`Embed Links\` permission!`; var pageSize = 15; var embeds = []; - var groups = Array.from(tags.keys()).map((item, index) => { - return index % pageSize === 0 ? Array.from(tags.keys()).slice(index, index + pageSize) : null; + var groups = [...tags.keys()].map((item, index) => { + return index % pageSize === 0 ? [...tags.keys()].slice(index, index + pageSize) : null; }).filter((item) => { return item; }); @@ -65,7 +65,7 @@ exports.run = async (message, args) => { if (embeds.length === 0) return `${message.author.mention}, I couldn't find any tags!`; return paginator(message, embeds); case "random": - return random(Array.from(tags))[1].content; + return random([...tags])[1].content; default: if (!tags.has(args[0].toLowerCase())) return `${message.author.mention}, this tag doesn't exist!`; return tags.get(args[0].toLowerCase()).content; diff --git a/utils/collections.js b/utils/collections.js index f823cfb..c122348 100644 --- a/utils/collections.js +++ b/utils/collections.js @@ -1,5 +1,5 @@ -const { Collection } = require("eris"); +//const { Collection } = require("eris"); -exports.commands = new Collection(); -exports.aliases = new Collection(); -exports.info = new Collection(); \ No newline at end of file +exports.commands = new Map(); +exports.aliases = new Map(); +exports.info = new Map(); \ No newline at end of file diff --git a/utils/handler.js b/utils/handler.js index a8579c4..cf22d39 100644 --- a/utils/handler.js +++ b/utils/handler.js @@ -16,9 +16,9 @@ exports.load = async (command) => { params: props.params }); if (props.aliases) { - props.aliases.forEach(alias => { + for (const alias of props.aliases) { collections.aliases.set(alias, command.split(".")[0]); - }); + } } return false; }; diff --git a/utils/help.js b/utils/help.js index 83b37f0..59cf567 100644 --- a/utils/help.js +++ b/utils/help.js @@ -19,7 +19,7 @@ Default prefix is \`&\`. + [**Image Editing**](#🖼️-image-editing) + [**Soundboard**](#🔊-soundboard) `; - const commands = Array.from(collections.commands.keys()); + const commands = collections.commands; const categories = { general: ["## 💻 General"], moderation: ["## 🔨 Moderation"], @@ -28,7 +28,7 @@ Default prefix is \`&\`. images: ["## 🖼️ Image Editing", "> These commands support the PNG, JPEG, WEBP, and GIF formats. (GIF support is currently experimental)"], soundboard: ["## 🔊 Soundboard"] }; - for (const command of commands) { + for (const [command] of commands) { const category = collections.info.get(command).category; const description = collections.info.get(command).description; const params = collections.info.get(command).params; @@ -37,7 +37,7 @@ Default prefix is \`&\`. } else if (category === 2) { categories.moderation.push(`+ **${command}**${params ? ` ${params}` : ""} - ${description}`); } else if (category === 3) { - const subCommands = Array.from(Object.keys(description)); + const subCommands = [...Object.keys(description)]; for (const subCommand of subCommands) { categories.tags.push(`+ **tags${subCommand !== "default" ? ` ${subCommand}` : ""}**${params[subCommand] ? ` ${params[subCommand]}` : ""} - ${description[subCommand]}`); }