GPTcord/src/interactionManager.ts

49 lines
1.7 KiB
TypeScript
Raw Normal View History

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
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
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;
}
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;
}
}
bindClient(client: DiscordClient) {
client.on("interactionCreate", (e) => this.onInteraction(e));
}
}