2023-05-08 08:53:06 +02:00
|
|
|
import { Interaction, Client as DiscordClient } from "discord.js";
|
|
|
|
import requireDirectory from "require-directory";
|
|
|
|
|
|
|
|
import Command from "./command";
|
|
|
|
|
|
|
|
export default class CommandManager {
|
|
|
|
readonly commands: Command[] = [];
|
|
|
|
|
|
|
|
constructor(directory = "./commands") {
|
|
|
|
const files = requireDirectory(module, directory);
|
|
|
|
for (const i in files ) {
|
|
|
|
try {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
this.commands.push(new (files[i].default as Command)());
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
console.error(`Failed to construct command ${i} (${typeof e}):`);
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onInteraction(interaction: Interaction) {
|
|
|
|
if (
|
|
|
|
interaction.isChatInputCommand() ||
|
|
|
|
interaction.isMessageContextMenuCommand() ||
|
|
|
|
interaction.isUserContextMenuCommand()
|
|
|
|
) {
|
|
|
|
const foundCommand = this.commands.find((command) => command.name == interaction.commandName );
|
|
|
|
if (!foundCommand) throw new Error(`Unknown command received (${interaction.commandName}). Did you forgot to push updated commands?`);
|
|
|
|
foundCommand.execute(interaction);
|
|
|
|
return;
|
|
|
|
}
|
2023-05-10 04:19:49 +02:00
|
|
|
if (interaction.isAutocomplete()) {
|
|
|
|
const foundCommand = this.commands.find((command) => command.name == interaction.commandName );
|
|
|
|
if (!foundCommand) throw new Error(`Unknown command received (${interaction.commandName}). Did you forgot to push updated commands?`);
|
|
|
|
if (!foundCommand.autocomplete) return;
|
|
|
|
foundCommand.autocomplete(interaction);
|
|
|
|
return;
|
|
|
|
}
|
2023-05-08 08:53:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bindClient(client: DiscordClient) {
|
|
|
|
client.on("interactionCreate", (e) => this.onInteraction(e));
|
|
|
|
}
|
|
|
|
}
|