From 49c83500542f4deb548eed23590969132b139419 Mon Sep 17 00:00:00 2001 From: Vermium Sifell Date: Sun, 17 Apr 2022 18:09:46 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20remove=20redundant=20plugi?= =?UTF-8?q?n=20loading?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/handlers/deployCommands.ts | 73 +++++++++++++++++----------------- 1 file changed, 36 insertions(+), 37 deletions(-) diff --git a/src/handlers/deployCommands.ts b/src/handlers/deployCommands.ts index 65d4627..d408886 100644 --- a/src/handlers/deployCommands.ts +++ b/src/handlers/deployCommands.ts @@ -3,52 +3,51 @@ import { token, clientId } from "@config/discord"; import { devMode, guildId } from "@config/other"; import logger from "../logger"; -import fs from "fs"; +import { Client } from "@root/types/common/discord"; import { REST } from "@discordjs/rest"; import { Routes } from "discord-api-types/v9"; -export default async () => { - fs.readdir("./src/plugins", async (error: any, plugins: any) => { - if (error) { - return logger?.error(new Error(error)); - } +export default async (client: Client) => { + const pluginList = [] as string[]; - const pluginList = [] as any; + await Promise.all( + client.commands.map(async (pluginData: any) => { + pluginList.push(pluginData.data.toJSON()); + logger.verbose( + `${pluginData.data.name} successfully pushed to plugin list.` + ); + }) + ) + .then(async () => { + logger.debug("Successfully pushed all plugins to plugin list."); + }) + .catch(async (error) => { + logger.error(error); + }); - await Promise.all( - plugins?.map(async (pluginName: any) => { - const plugin = await import(`../plugins/${pluginName}`); + const rest = new REST({ version: "9" }).setToken(token); - pluginList.push(plugin.default.data.toJSON()); - - logger?.verbose(`Loaded plugin: ${pluginName} for deployment`); - }) - ); - - const rest = new REST({ version: "9" }).setToken(token); + await rest + .put(Routes.applicationCommands(clientId), { + body: pluginList, + }) + .then(async () => { + logger.debug(`Successfully deployed plugins to Discord`); + }) + .catch(async (error) => { + logger.error(error); + }); + if (devMode) { await rest - .put(Routes.applicationCommands(clientId), { + .put(Routes.applicationGuildCommands(clientId, guildId), { body: pluginList, }) - .then(async () => { - logger?.info(`Successfully deployed plugins to Discord`); - }) - .catch(async (err: any) => { - logger.error(err); + .then(async () => + logger.debug(`Successfully deployed guild plugins to Discord`) + ) + .catch(async (error) => { + logger.error(error); }); - - if (devMode) { - await rest - .put(Routes.applicationGuildCommands(clientId, guildId), { - body: pluginList, - }) - .then(async () => - logger?.info(`Successfully deployed guild plugins to Discord`) - ) - .catch(async (err: any) => { - logger.error(err); - }); - } - }); + } };