Refactor command register to make only one call
This commit is contained in:
parent
bf177e6548
commit
c25ce1c0c8
13 changed files with 66 additions and 68 deletions
|
@ -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 !");
|
||||
});
|
||||
|
|
|
@ -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<boolean> {
|
||||
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();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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<string, Module> = new Map<string, Module>();
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
|
@ -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 {
|
|
@ -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 {
|
|
@ -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");
|
||||
|
|
@ -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";
|
||||
|
||||
|
|
@ -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";
|
||||
|
||||
|
|
@ -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";
|
||||
|
||||
|
|
@ -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";
|
||||
|
||||
|
Loading…
Reference in a new issue