feat: extract commands from client for easy access everywhere

This commit is contained in:
HerozDotExe 2025-06-19 15:18:48 +02:00
parent 9dbc276a9e
commit efac1632d1
3 changed files with 76 additions and 31 deletions

View file

@ -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<CacheType>) {
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;

View file

@ -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<string, Command>;
@ -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;

37
src/stores.ts Normal file
View file

@ -0,0 +1,37 @@
import { Command } from "./types.ts";
// class Store<T> {
// value: T
// constructor(defaultValue:T) {
// this.value = defaultValue
// }
// get() {
// return this.value
// }
// set(newValue: T) {
// this.value = newValue
// }
// }
// class ListStore<T> extends Store<T[]> {
// constructor(defaultValue: T[]){
// super(defaultValue)
// }
// push() {
// this.value.push()
// }
// }
// class ObjectStore<T> 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} = {}