From 239d69d6d3c0150559dddd35bc2c10c493fe7b9c Mon Sep 17 00:00:00 2001 From: Essem Date: Sat, 10 Sep 2022 23:06:40 -0500 Subject: [PATCH] Refactor command reloading --- classes/imageCommand.js | 4 +--- commands/music/music.js | 1 + shard.js | 24 ++++++++++-------------- utils/handler.js | 29 ++++++++++++++++------------- 4 files changed, 28 insertions(+), 30 deletions(-) diff --git a/classes/imageCommand.js b/classes/imageCommand.js index d996039..0985709 100644 --- a/classes/imageCommand.js +++ b/classes/imageCommand.js @@ -27,9 +27,7 @@ class ImageCommand extends Command { params: {} }; - if (this.type === "application") { - await this.acknowledge(); - } + if (this.type === "application") await this.acknowledge(); if (this.constructor.requiresImage) { try { diff --git a/commands/music/music.js b/commands/music/music.js index 29510b3..cdc3fcc 100644 --- a/commands/music/music.js +++ b/commands/music/music.js @@ -6,6 +6,7 @@ class MusicAIOCommand extends Command { async run() { let cmd = this.type === "classic" ? this.args[0] : this.optionsArray[0].name; if (cmd === "music" || this.constructor.aliases.includes(cmd)) return "https://projectlounge.pw/robotdance.gif"; + await this.acknowledge(); if (this.type === "classic") { this.origOptions.args.shift(); } else { diff --git a/shard.js b/shard.js index 5083977..37ab604 100644 --- a/shard.js +++ b/shard.js @@ -8,7 +8,7 @@ import { fileURLToPath } from "url"; // fancy loggings import { log, error } from "./utils/logger.js"; // initialize command loader -import { load, update } from "./utils/handler.js"; +import { load, send } from "./utils/handler.js"; // lavalink stuff import { checkStatus, connect, reload, status, connected } from "./utils/soundplayer.js"; // database stuff @@ -47,22 +47,14 @@ class Shard extends BaseClusterWorker { for await (const commandFile of this.getFiles(resolve(dirname(fileURLToPath(import.meta.url)), "./commands/"))) { log("log", `Loading command from ${commandFile}...`); try { - await load(this.bot, this.clusterID, this.workerID, this.ipc, commandFile, soundStatus); + await load(this.bot, commandFile, soundStatus); } catch (e) { error(`Failed to register command from ${commandFile}: ${e}`); } } if (types.application) { - const commandArray = await update(this.bot, this.clusterID, this.workerID, this.ipc, soundStatus); try { - log("info", "Sending application command data to Discord..."); - let cmdArray = commandArray.main; - if (process.env.ADMIN_SERVER && process.env.ADMIN_SERVER !== "") { - await this.bot.bulkEditGuildCommands(process.env.ADMIN_SERVER, commandArray.private); - } else { - cmdArray = [...commandArray.main, ...commandArray.private]; - } - await this.bot.bulkEditCommands(cmdArray); + await send(this.bot); } catch (e) { log("error", e); log("error", "Failed to send command data to Discord, slash/message commands may be unavailable."); @@ -99,9 +91,13 @@ class Shard extends BaseClusterWorker { this.ipc.register("reload", async (message) => { const path = paths.get(message); if (!path) return this.ipc.broadcast("reloadFail", { result: "I couldn't find that command!" }); - const result = await load(this.bot, this.clusterID, this.workerID, this.ipc, path, await checkStatus(), true); - if (result !== message) return this.ipc.broadcast("reloadFail", { result }); - return this.ipc.broadcast("reloadSuccess"); + try { + const result = await load(this.bot, path, await checkStatus(), true); + if (result !== message) return this.ipc.broadcast("reloadFail", { result }); + return this.ipc.broadcast("reloadSuccess"); + } catch (result) { + return this.ipc.broadcast("reloadFail", { result }); + } }); this.ipc.register("soundreload", async () => { diff --git a/utils/handler.js b/utils/handler.js index 5ed5e95..b981902 100644 --- a/utils/handler.js +++ b/utils/handler.js @@ -8,7 +8,7 @@ const { blacklist } = JSON.parse(readFileSync(new URL("../config/commands.json", let queryValue = 0; // load command into memory -export async function load(client, cluster, worker, ipc, command, soundStatus, slashReload = false) { +export async function load(client, command, soundStatus, slashReload = false) { const { default: props } = await import(`${command}?v=${queryValue}`); queryValue++; if (props.requires.includes("sound") && soundStatus) { @@ -52,19 +52,10 @@ export async function load(client, cluster, worker, ipc, command, soundStatus, s commandInfo.type = 3; } else { commands.set(commandName, props); + } - if (slashReload && props.slashAllowed) { - const commandList = await client.getCommands(); - const oldCommand = commandList.filter((item) => { - return item.name === commandName; - })[0]; - await client.editCommand(oldCommand.id, { - name: commandName, - type: 1, - description: props.description, - options: props.flags - }); - } + if (slashReload && props.slashAllowed) { + await send(client); } if (Object.getPrototypeOf(props).name === "SoundboardCommand") sounds.set(commandName, props.file); @@ -124,4 +115,16 @@ export async function update() { main: commandArray, private: privateCommandArray }; +} + +export async function send(bot) { + const commandArray = await update(); + log("info", "Sending application command data to Discord..."); + let cmdArray = commandArray.main; + if (process.env.ADMIN_SERVER && process.env.ADMIN_SERVER !== "") { + await bot.bulkEditGuildCommands(process.env.ADMIN_SERVER, commandArray.private); + } else { + cmdArray = [...commandArray.main, ...commandArray.private]; + } + await bot.bulkEditCommands(cmdArray); } \ No newline at end of file