Add commandManager and the first slash command
the command allows for summining the bot without sending an actual mention message that might hang in the chat log sent to openAi, consuming tokens
This commit is contained in:
parent
56a0e686b0
commit
8b4b35454b
6 changed files with 124 additions and 2 deletions
40
src/interactionManager.ts
Normal file
40
src/interactionManager.ts
Normal file
|
@ -0,0 +1,40 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
||||
bindClient(client: DiscordClient) {
|
||||
client.on("interactionCreate", (e) => this.onInteraction(e));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue