diff --git a/src/events/utils/interactionCreate.ts b/src/events/utils/interactionCreate.ts new file mode 100644 index 0000000..b25979b --- /dev/null +++ b/src/events/utils/interactionCreate.ts @@ -0,0 +1,37 @@ +import { Events, CacheType, ChatInputCommandInteraction, MessageFlags } from "npm:discord.js"; +import { Event } from "../../types.ts"; +import { commands } from "../../stores.ts"; + +export default { + name: Events.InteractionCreate, + once: true, + async execute(interaction: ChatInputCommandInteraction) { + if (!interaction.isChatInputCommand()) return; + + const command = commands[interaction.commandName]; + + if (!command) { + console.error( + `No command matching ${interaction.commandName} was found.` + ); + return; + } + + try { + await command.execute(interaction); + } catch (error) { + console.error(error); + if (interaction.replied || interaction.deferred) { + await interaction.followUp({ + content: "There was an error while executing this command!", + flags: MessageFlags.Ephemeral, + }); + } else { + await interaction.reply({ + content: "There was an error while executing this command!", + flags: MessageFlags.Ephemeral, + }); + } + } + }, +} as Event; diff --git a/src/main.ts b/src/main.ts index b590d8d..0bcd3e4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,15 +2,14 @@ import { Client as _client, ClientOptions, Collection, - Events, GatewayIntentBits, - MessageFlags, } from "npm:discord.js"; const token = Deno.env.get("token"); import { Command, Event } from "./types.ts"; import { gatherPaths } from "./gatherer.ts"; import { exists } from "jsr:@std/fs/exists"; import cai from "./cai.ts"; +import { commands } from "./stores.ts"; class Client extends _client { commands: Collection; @@ -27,7 +26,7 @@ const client = new Client({ async function registerCommand(path: string) { const { default: command }: { default: Command } = await import(path); if ("data" in command && "execute" in command) { - client.commands.set(command.data.name, command); + commands[command.data.name] = command } else { console.log( `[WARNING] The command at ${path} is missing a required "data" or "execute" property.` @@ -51,34 +50,6 @@ async function registerEvent(path: string) { (await gatherPaths("commands")).forEach(registerCommand); (await gatherPaths("events")).forEach(registerEvent); -client.on(Events.InteractionCreate, async (interaction) => { - if (!interaction.isChatInputCommand()) return; - - const command = client.commands.get(interaction.commandName); - - if (!command) { - console.error(`No command matching ${interaction.commandName} was found.`); - return; - } - - try { - await command.execute(interaction); - } catch (error) { - console.error(error); - if (interaction.replied || interaction.deferred) { - await interaction.followUp({ - content: "There was an error while executing this command!", - flags: MessageFlags.Ephemeral, - }); - } else { - await interaction.reply({ - content: "There was an error while executing this command!", - flags: MessageFlags.Ephemeral, - }); - } - } -}); - if (await exists("./cai.token", { isFile: true })) { await cai.login(await Deno.readTextFile("./cai.token")); cai.logged = true; diff --git a/src/stores.ts b/src/stores.ts new file mode 100644 index 0000000..94a1f6e --- /dev/null +++ b/src/stores.ts @@ -0,0 +1,37 @@ +import { Command } from "./types.ts"; + +// class Store { +// value: T +// constructor(defaultValue:T) { +// this.value = defaultValue +// } +// get() { +// return this.value +// } +// set(newValue: T) { +// this.value = newValue +// } +// } + +// class ListStore extends Store { +// constructor(defaultValue: T[]){ +// super(defaultValue) +// } +// push() { +// this.value.push() +// } +// } + +// class ObjectStore extends Store<{[index: string | number | symbol]: T}> { +// constructor(defaultValue: {[index in string | number | symbol]: T}){ +// super(defaultValue) +// } +// getProperty(index: string | number | symbol) { +// return this.value[index] +// } +// setProperty(index: string | number | symbol, newValue: T) { +// this.value[index] = newValue +// } +// } + +export const commands: {[index:string]:Command} = {} \ No newline at end of file