From c25ce1c0c899768d1368c483178a25ad0e1896c6 Mon Sep 17 00:00:00 2001 From: flifloo Date: Mon, 22 Nov 2021 10:06:25 +0100 Subject: [PATCH] Refactor command register to make only one call --- src/index.ts | 6 --- src/lib/Command.ts | 39 ++------------- src/lib/Modules.ts | 49 ++++++++++++++++--- .../Music/{ => commands}/disconnect.ts | 5 +- src/modules/Music/{ => commands}/flush.ts | 5 +- src/modules/Music/{ => commands}/pause.ts | 6 +-- src/modules/Music/{ => commands}/play.ts | 8 +-- src/modules/Music/{ => commands}/queue.ts | 4 +- src/modules/Music/{ => commands}/resume.ts | 4 +- src/modules/Music/{ => commands}/skip.ts | 4 +- src/modules/Music/{ => commands}/stop.ts | 4 +- src/modules/Music/{ => lib}/Player.ts | 0 src/modules/Music/{ => lib}/Track.ts | 0 13 files changed, 66 insertions(+), 68 deletions(-) rename src/modules/Music/{ => commands}/disconnect.ts (90%) rename src/modules/Music/{ => commands}/flush.ts (89%) rename src/modules/Music/{ => commands}/pause.ts (90%) rename src/modules/Music/{ => commands}/play.ts (94%) rename src/modules/Music/{ => commands}/queue.ts (96%) rename src/modules/Music/{ => commands}/resume.ts (94%) rename src/modules/Music/{ => commands}/skip.ts (94%) rename src/modules/Music/{ => commands}/stop.ts (94%) rename src/modules/Music/{ => lib}/Player.ts (100%) rename src/modules/Music/{ => lib}/Track.ts (100%) diff --git a/src/index.ts b/src/index.ts index ff059bf..6cee2b8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,12 +9,6 @@ const client = new AdministratorClient({ intents: [Intents.FLAGS.GUILDS, Intents client.once("ready", async () => { client.application = await client.application?.fetch() ?? null; - if ("DEV" in process.env && process.env["DEV"] == "true") { - console.log("Dev mod enable"); - await client.application?.commands.set([]); - for (const name in await client.guilds.fetch()) - await (await client.guilds.fetch(name)).commands.set([]); - } await client.modules.loadAllModules(); console.log("Started !"); }); diff --git a/src/lib/Command.ts b/src/lib/Command.ts index cfb5af9..b02256e 100644 --- a/src/lib/Command.ts +++ b/src/lib/Command.ts @@ -1,15 +1,10 @@ -import { - ApplicationCommand, ApplicationCommandData, - CommandInteraction, - GuildResolvable, -} from "discord.js"; +import {ApplicationCommandData, CommandInteraction} from "discord.js"; import {Module} from "./Module"; export abstract class Command { module: Module; data: ApplicationCommandData; - scope: ApplicationCommand<{ guild: GuildResolvable }> | undefined; constructor(module: Module) { this.module = module; @@ -18,35 +13,11 @@ export abstract class Command { abstract execute(interaction: CommandInteraction): void; - async register() { - try { - if ("DEV" in process.env && process.env["DEV"] == "true") { - const devGuild = await this.module.modules.client?.guilds.fetch(process.env["DEVGUILD"] as any); - this.scope = await devGuild.commands.create(this.data); // ToDo: use only one call to avoid spamming the api - } else { - this.scope = await this.module.modules.client?.application?.commands.create(this.data); - } + load(): any { - console.log("Successfully registered commands " + this.scope?.name); - } catch (error) { - console.error(error); - } - } + }; - async isRegister(): Promise { - if (this.scope) - return !! await this.module.modules.client?.application?.commands.fetch(this.scope.id); // ToDo: use only one call to avoid spamming the api + unload(): any { - return false; - } - - async load() { - if (!await this.isRegister()) - await this.register(); - } - - async unload() { - if (await this.isRegister()) - await this.unload(); - } + }; } diff --git a/src/lib/Modules.ts b/src/lib/Modules.ts index 279a437..441abee 100644 --- a/src/lib/Modules.ts +++ b/src/lib/Modules.ts @@ -2,6 +2,7 @@ import { readdirSync } from "fs"; import {Module} from "./Module"; import {Command} from "./Command"; import {AdministratorClient} from "./AdministratorClient"; +import {ApplicationCommandData, ApplicationCommandManager} from "discord.js"; export class Modules { modules: Map = new Map(); @@ -11,11 +12,15 @@ export class Modules { this.client = client; } - async load(name: string) { + async load(name: string, createCommand: boolean = true) { try { const module: Module = new (require(__dirname+`/../modules/${name}`)[name])(this); await module.load(); this.modules.set(name, module); + + if (createCommand) + await this.registerCommand(module.commands.map(c => c.data)); + console.info(`Module ${name} loaded`) } catch (error) { console.error(`Fail to load module ${name}`); @@ -56,7 +61,9 @@ export class Modules { async loadAllModules() { for (const module of await this.allModules()) - await this.load(module.name); + await this.load(module.name, false); + + await this.registerCommand(this.allCommands().map(c => c.data), true); } async unloadAllModules() { @@ -69,12 +76,40 @@ export class Modules { await this.reload(module.name); } - getCommand(name: string): Command | null { - for (const module of Array.from(this.modules.values())) - for (const command of module.commands) - if (command.data.name == name) - return command; + async registerCommand(data: ApplicationCommandData | ApplicationCommandData[], set: boolean = false) { + if (!this.client.application) { + this.client.logger.err("Fail to register command, client application is undefined !"); + return; + } + let commands: ApplicationCommandManager = this.client.application.commands; + + if ("DEV" in process.env && process.env["DEV"] == "true") { + await commands.set([]); + commands = (await this.client?.guilds.fetch(process.env["DEVGUILD"] as any)).commands as any; + } + + if (Array.isArray(data)) + if (set) + await commands.set(data); + else + for (const d of data) + await commands.create(d); + else + if (set) + await commands.set([data]); + else + await commands.create(data); + } + + allCommands() : Command[] { + return Array.from(this.modules.values()).map(m => m.commands).reduce((l, m) => l.concat(m)); + } + + getCommand(name: string): Command | null { + for (const command of this.allCommands()) + if (command.data.name == name) + return command; return null; } } diff --git a/src/modules/Music/disconnect.ts b/src/modules/Music/commands/disconnect.ts similarity index 90% rename from src/modules/Music/disconnect.ts rename to src/modules/Music/commands/disconnect.ts index 00216ac..e4f9c6c 100644 --- a/src/modules/Music/disconnect.ts +++ b/src/modules/Music/commands/disconnect.ts @@ -1,7 +1,6 @@ -import {Command} from "../../lib/Command"; +import {Command} from "../../../lib/Command"; import {ChatInputApplicationCommandData, CommandInteraction, GuildMember} from "discord.js"; -import {Music} from "./index"; -import {AudioPlayerStatus} from "@discordjs/voice"; +import {Music} from "../index"; export class DisconnectCommand extends Command { diff --git a/src/modules/Music/flush.ts b/src/modules/Music/commands/flush.ts similarity index 89% rename from src/modules/Music/flush.ts rename to src/modules/Music/commands/flush.ts index 1bcb830..e7af19e 100644 --- a/src/modules/Music/flush.ts +++ b/src/modules/Music/commands/flush.ts @@ -1,7 +1,6 @@ -import {Command} from "../../lib/Command"; +import {Command} from "../../../lib/Command"; import {ChatInputApplicationCommandData, CommandInteraction, GuildMember} from "discord.js"; -import {Music} from "./index"; -import {AudioPlayerPausedState, AudioPlayerStatus} from "@discordjs/voice"; +import {Music} from "../index"; export class FlushCommand extends Command { diff --git a/src/modules/Music/pause.ts b/src/modules/Music/commands/pause.ts similarity index 90% rename from src/modules/Music/pause.ts rename to src/modules/Music/commands/pause.ts index 1f9dcc7..80c1b72 100644 --- a/src/modules/Music/pause.ts +++ b/src/modules/Music/commands/pause.ts @@ -1,7 +1,7 @@ -import {Command} from "../../lib/Command"; +import {Command} from "../../../lib/Command"; import {ChatInputApplicationCommandData, CommandInteraction, GuildMember} from "discord.js"; -import {Music} from "./index"; -import {AudioPlayerPausedState, AudioPlayerStatus} from "@discordjs/voice"; +import {Music} from "../index"; +import {AudioPlayerStatus} from "@discordjs/voice"; export class PauseCommand extends Command { diff --git a/src/modules/Music/play.ts b/src/modules/Music/commands/play.ts similarity index 94% rename from src/modules/Music/play.ts rename to src/modules/Music/commands/play.ts index c4fc17f..f8299d9 100644 --- a/src/modules/Music/play.ts +++ b/src/modules/Music/commands/play.ts @@ -1,8 +1,8 @@ -import {Command} from "../../lib/Command"; +import {Command} from "../../../lib/Command"; import {ChatInputApplicationCommandData, CommandInteraction, GuildMember, VoiceChannel} from "discord.js"; -import {Music} from "./index"; -import {Player} from "./Player"; -import {Track} from "./Track"; +import {Music} from "../index"; +import {Player} from "../lib/Player"; +import {Track} from "../lib/Track"; import {entersState, VoiceConnectionStatus} from "@discordjs/voice"; const {Constants: { ApplicationCommandOptionTypes }} = require("discord.js"); diff --git a/src/modules/Music/queue.ts b/src/modules/Music/commands/queue.ts similarity index 96% rename from src/modules/Music/queue.ts rename to src/modules/Music/commands/queue.ts index 74c1543..3fd4ac0 100644 --- a/src/modules/Music/queue.ts +++ b/src/modules/Music/commands/queue.ts @@ -1,6 +1,6 @@ -import {Command} from "../../lib/Command"; +import {Command} from "../../../lib/Command"; import {ChatInputApplicationCommandData, CommandInteraction, GuildMember} from "discord.js"; -import {Music} from "./index"; +import {Music} from "../index"; import {AudioPlayerStatus} from "@discordjs/voice"; diff --git a/src/modules/Music/resume.ts b/src/modules/Music/commands/resume.ts similarity index 94% rename from src/modules/Music/resume.ts rename to src/modules/Music/commands/resume.ts index 341e78e..6bbaa34 100644 --- a/src/modules/Music/resume.ts +++ b/src/modules/Music/commands/resume.ts @@ -1,6 +1,6 @@ -import {Command} from "../../lib/Command"; +import {Command} from "../../../lib/Command"; import {ChatInputApplicationCommandData, CommandInteraction, GuildMember} from "discord.js"; -import {Music} from "./index"; +import {Music} from "../index"; import {AudioPlayerStatus} from "@discordjs/voice"; diff --git a/src/modules/Music/skip.ts b/src/modules/Music/commands/skip.ts similarity index 94% rename from src/modules/Music/skip.ts rename to src/modules/Music/commands/skip.ts index f764cd1..b7869ef 100644 --- a/src/modules/Music/skip.ts +++ b/src/modules/Music/commands/skip.ts @@ -1,6 +1,6 @@ -import {Command} from "../../lib/Command"; +import {Command} from "../../../lib/Command"; import {ChatInputApplicationCommandData, CommandInteraction, GuildMember} from "discord.js"; -import {Music} from "./index"; +import {Music} from "../index"; import {AudioPlayerStatus} from "@discordjs/voice"; diff --git a/src/modules/Music/stop.ts b/src/modules/Music/commands/stop.ts similarity index 94% rename from src/modules/Music/stop.ts rename to src/modules/Music/commands/stop.ts index d18b327..add2a7e 100644 --- a/src/modules/Music/stop.ts +++ b/src/modules/Music/commands/stop.ts @@ -1,6 +1,6 @@ -import {Command} from "../../lib/Command"; +import {Command} from "../../../lib/Command"; import {ChatInputApplicationCommandData, CommandInteraction, GuildMember} from "discord.js"; -import {Music} from "./index"; +import {Music} from "../index"; import {AudioPlayerStatus} from "@discordjs/voice"; diff --git a/src/modules/Music/Player.ts b/src/modules/Music/lib/Player.ts similarity index 100% rename from src/modules/Music/Player.ts rename to src/modules/Music/lib/Player.ts diff --git a/src/modules/Music/Track.ts b/src/modules/Music/lib/Track.ts similarity index 100% rename from src/modules/Music/Track.ts rename to src/modules/Music/lib/Track.ts