fix: reload commands

This commit is contained in:
MARCROCK22 2024-03-26 13:03:45 -04:00
parent 9c42027676
commit 28fb9b8cda
3 changed files with 15 additions and 2 deletions

View File

@ -271,6 +271,12 @@ class BaseCommand {
const __tempCommand = await magicImport(this.__filePath!).then(x => x.default ?? x); const __tempCommand = await magicImport(this.__filePath!).then(x => x.default ?? x);
Object.setPrototypeOf(this, __tempCommand.prototype); Object.setPrototypeOf(this, __tempCommand.prototype);
for (const i of this.options ?? []) {
if (i instanceof SubCommand && i.__filePath) {
await i.reload();
}
}
} }
run?(context: CommandContext<any>): any; run?(context: CommandContext<any>): any;

View File

@ -36,12 +36,17 @@ export class CommandHandler extends BaseHandler {
} }
async load(commandsDir: string, client: UsingClient) { async load(commandsDir: string, client: UsingClient) {
const result = (await this.loadFilesK<typeof Command>(await this.getFiles(commandsDir))).filter(x => x.file); const result = (
await this.loadFilesK<typeof Command | typeof SubCommand | typeof ContextMenuCommand>(
await this.getFiles(commandsDir),
)
).filter(x => x.file);
this.values = []; this.values = [];
for (const command of result) { for (const command of result) {
let commandInstance; let commandInstance;
try { try {
//@ts-expect-error abstract class
commandInstance = new command.file(); commandInstance = new command.file();
} catch (e) { } catch (e) {
if (e instanceof Error && e.message === 'command.file is not a constructor') { if (e instanceof Error && e.message === 'command.file is not a constructor') {
@ -72,8 +77,10 @@ export class CommandHandler extends BaseHandler {
continue; continue;
} }
try { try {
//@ts-expect-error abstract class
const subCommand = new (result.find(x => x.path === option)!.file)(); const subCommand = new (result.find(x => x.path === option)!.file)();
if (subCommand instanceof SubCommand) { if (subCommand instanceof SubCommand) {
subCommand.__filePath = option;
commandInstance.options.push(subCommand); commandInstance.options.push(subCommand);
} }
} catch { } catch {

View File

@ -251,7 +251,7 @@ export async function magicImport(path: string) {
return require(path); return require(path);
} catch { } catch {
// biome-ignore lint/security/noGlobalEval: modules import broke // biome-ignore lint/security/noGlobalEval: modules import broke
return eval('((path) => import(`file:///${path}`))')(path.split('\\').join('\\\\')); return eval('((path) => import(`file:///${path}?update=${Date.now()}`))')(path.split('\\').join('\\\\'));
} }
} }