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.once("ready", async () => {
|
||||||
client.application = await client.application?.fetch() ?? null;
|
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();
|
await client.modules.loadAllModules();
|
||||||
console.log("Started !");
|
console.log("Started !");
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,15 +1,10 @@
|
||||||
import {
|
import {ApplicationCommandData, CommandInteraction} from "discord.js";
|
||||||
ApplicationCommand, ApplicationCommandData,
|
|
||||||
CommandInteraction,
|
|
||||||
GuildResolvable,
|
|
||||||
} from "discord.js";
|
|
||||||
import {Module} from "./Module";
|
import {Module} from "./Module";
|
||||||
|
|
||||||
|
|
||||||
export abstract class Command {
|
export abstract class Command {
|
||||||
module: Module;
|
module: Module;
|
||||||
data: ApplicationCommandData;
|
data: ApplicationCommandData;
|
||||||
scope: ApplicationCommand<{ guild: GuildResolvable }> | undefined;
|
|
||||||
|
|
||||||
constructor(module: Module) {
|
constructor(module: Module) {
|
||||||
this.module = module;
|
this.module = module;
|
||||||
|
@ -18,35 +13,11 @@ export abstract class Command {
|
||||||
|
|
||||||
abstract execute(interaction: CommandInteraction): void;
|
abstract execute(interaction: CommandInteraction): void;
|
||||||
|
|
||||||
async register() {
|
load(): any {
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("Successfully registered commands " + this.scope?.name);
|
};
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async isRegister(): Promise<boolean> {
|
unload(): any {
|
||||||
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
|
|
||||||
|
|
||||||
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 {Module} from "./Module";
|
||||||
import {Command} from "./Command";
|
import {Command} from "./Command";
|
||||||
import {AdministratorClient} from "./AdministratorClient";
|
import {AdministratorClient} from "./AdministratorClient";
|
||||||
|
import {ApplicationCommandData, ApplicationCommandManager} from "discord.js";
|
||||||
|
|
||||||
export class Modules {
|
export class Modules {
|
||||||
modules: Map<string, Module> = new Map<string, Module>();
|
modules: Map<string, Module> = new Map<string, Module>();
|
||||||
|
@ -11,11 +12,15 @@ export class Modules {
|
||||||
this.client = client;
|
this.client = client;
|
||||||
}
|
}
|
||||||
|
|
||||||
async load(name: string) {
|
async load(name: string, createCommand: boolean = true) {
|
||||||
try {
|
try {
|
||||||
const module: Module = new (require(__dirname+`/../modules/${name}`)[name])(this);
|
const module: Module = new (require(__dirname+`/../modules/${name}`)[name])(this);
|
||||||
await module.load();
|
await module.load();
|
||||||
this.modules.set(name, module);
|
this.modules.set(name, module);
|
||||||
|
|
||||||
|
if (createCommand)
|
||||||
|
await this.registerCommand(module.commands.map(c => c.data));
|
||||||
|
|
||||||
console.info(`Module ${name} loaded`)
|
console.info(`Module ${name} loaded`)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Fail to load module ${name}`);
|
console.error(`Fail to load module ${name}`);
|
||||||
|
@ -56,7 +61,9 @@ export class Modules {
|
||||||
|
|
||||||
async loadAllModules() {
|
async loadAllModules() {
|
||||||
for (const module of await this.allModules())
|
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() {
|
async unloadAllModules() {
|
||||||
|
@ -69,12 +76,40 @@ export class Modules {
|
||||||
await this.reload(module.name);
|
await this.reload(module.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
getCommand(name: string): Command | null {
|
async registerCommand(data: ApplicationCommandData | ApplicationCommandData[], set: boolean = false) {
|
||||||
for (const module of Array.from(this.modules.values()))
|
if (!this.client.application) {
|
||||||
for (const command of module.commands)
|
this.client.logger.err("Fail to register command, client application is undefined !");
|
||||||
if (command.data.name == name)
|
return;
|
||||||
return command;
|
}
|
||||||
|
|
||||||
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import {Command} from "../../lib/Command";
|
import {Command} from "../../../lib/Command";
|
||||||
import {ChatInputApplicationCommandData, CommandInteraction, GuildMember} from "discord.js";
|
import {ChatInputApplicationCommandData, CommandInteraction, GuildMember} from "discord.js";
|
||||||
import {Music} from "./index";
|
import {Music} from "../index";
|
||||||
import {AudioPlayerStatus} from "@discordjs/voice";
|
|
||||||
|
|
||||||
|
|
||||||
export class DisconnectCommand extends Command {
|
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 {ChatInputApplicationCommandData, CommandInteraction, GuildMember} from "discord.js";
|
||||||
import {Music} from "./index";
|
import {Music} from "../index";
|
||||||
import {AudioPlayerPausedState, AudioPlayerStatus} from "@discordjs/voice";
|
|
||||||
|
|
||||||
|
|
||||||
export class FlushCommand extends Command {
|
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 {ChatInputApplicationCommandData, CommandInteraction, GuildMember} from "discord.js";
|
||||||
import {Music} from "./index";
|
import {Music} from "../index";
|
||||||
import {AudioPlayerPausedState, AudioPlayerStatus} from "@discordjs/voice";
|
import {AudioPlayerStatus} from "@discordjs/voice";
|
||||||
|
|
||||||
|
|
||||||
export class PauseCommand extends Command {
|
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 {ChatInputApplicationCommandData, CommandInteraction, GuildMember, VoiceChannel} from "discord.js";
|
||||||
import {Music} from "./index";
|
import {Music} from "../index";
|
||||||
import {Player} from "./Player";
|
import {Player} from "../lib/Player";
|
||||||
import {Track} from "./Track";
|
import {Track} from "../lib/Track";
|
||||||
import {entersState, VoiceConnectionStatus} from "@discordjs/voice";
|
import {entersState, VoiceConnectionStatus} from "@discordjs/voice";
|
||||||
const {Constants: { ApplicationCommandOptionTypes }} = require("discord.js");
|
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 {ChatInputApplicationCommandData, CommandInteraction, GuildMember} from "discord.js";
|
||||||
import {Music} from "./index";
|
import {Music} from "../index";
|
||||||
import {AudioPlayerStatus} from "@discordjs/voice";
|
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 {ChatInputApplicationCommandData, CommandInteraction, GuildMember} from "discord.js";
|
||||||
import {Music} from "./index";
|
import {Music} from "../index";
|
||||||
import {AudioPlayerStatus} from "@discordjs/voice";
|
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 {ChatInputApplicationCommandData, CommandInteraction, GuildMember} from "discord.js";
|
||||||
import {Music} from "./index";
|
import {Music} from "../index";
|
||||||
import {AudioPlayerStatus} from "@discordjs/voice";
|
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 {ChatInputApplicationCommandData, CommandInteraction, GuildMember} from "discord.js";
|
||||||
import {Music} from "./index";
|
import {Music} from "../index";
|
||||||
import {AudioPlayerStatus} from "@discordjs/voice";
|
import {AudioPlayerStatus} from "@discordjs/voice";
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue