From 9dbc276a9e5de54428d97294eadc17284335005a Mon Sep 17 00:00:00 2001 From: HerozDotExe Date: Thu, 19 Jun 2025 14:50:47 +0200 Subject: [PATCH] feat: per file event handling --- scripts/deployCommands.ts | 4 ++-- src/commands/template.ts | 3 ++- src/commands/utils/login.ts | 3 ++- src/events/template.ts | 10 ++++++++++ src/events/utils/ready.ts | 10 ++++++++++ src/{commands.ts => gatherer.ts} | 6 +++--- src/main.ts | 34 +++++++++++++++++++++----------- src/types.ts | 6 ++++++ 8 files changed, 58 insertions(+), 18 deletions(-) create mode 100644 src/events/template.ts create mode 100644 src/events/utils/ready.ts rename src/{commands.ts => gatherer.ts} (82%) diff --git a/scripts/deployCommands.ts b/scripts/deployCommands.ts index f333711..670b72e 100644 --- a/scripts/deployCommands.ts +++ b/scripts/deployCommands.ts @@ -1,7 +1,7 @@ import { REST, RESTPostAPIChatInputApplicationCommandsJSONBody, Routes } from "npm:discord.js"; const { clientId, guildId, token } = Deno.env.toObject(); import { Command } from "../src/types.ts"; -import { gatherCommandsPaths } from "../src/commands.ts"; +import { gatherPaths } from "../src/gatherer.ts"; const commands: RESTPostAPIChatInputApplicationCommandsJSONBody[] = []; async function register(commandPath: string) { @@ -16,7 +16,7 @@ async function register(commandPath: string) { } } -for (const commandPath of await gatherCommandsPaths()) { +for (const commandPath of await gatherPaths("commands")) { await register(commandPath) } diff --git a/src/commands/template.ts b/src/commands/template.ts index 4db9d9a..c1aef66 100644 --- a/src/commands/template.ts +++ b/src/commands/template.ts @@ -1,4 +1,5 @@ import { CacheType, ChatInputCommandInteraction, SlashCommandBuilder } from "npm:discord.js"; +import { Command } from "../types.ts"; export default { data: new SlashCommandBuilder() @@ -7,4 +8,4 @@ export default { async execute(interaction: ChatInputCommandInteraction) { await interaction.reply("Pong!"); }, -}; +} as Command; diff --git a/src/commands/utils/login.ts b/src/commands/utils/login.ts index 561545c..2ac455c 100644 --- a/src/commands/utils/login.ts +++ b/src/commands/utils/login.ts @@ -5,6 +5,7 @@ import { SlashCommandBuilder, } from "npm:discord.js"; import cai from "../../cai.ts"; +import { Command } from "../../types.ts"; async function getToken() { try { @@ -39,4 +40,4 @@ export default { flags: MessageFlags.Ephemeral, }); }, -}; +} as Command; diff --git a/src/events/template.ts b/src/events/template.ts new file mode 100644 index 0000000..f29c752 --- /dev/null +++ b/src/events/template.ts @@ -0,0 +1,10 @@ +import { Events, Client } from "npm:discord.js"; +import { Event } from "../types.ts"; + +export default { + name: Events.ClientReady, + once: true, + execute(readyClient: Client) { + console.log(`Ready! Logged in as ${readyClient.user!.tag}`); + }, +} as Event; diff --git a/src/events/utils/ready.ts b/src/events/utils/ready.ts new file mode 100644 index 0000000..6cfbbd6 --- /dev/null +++ b/src/events/utils/ready.ts @@ -0,0 +1,10 @@ +import { Events, Client } from "npm:discord.js"; +import { Event } from "../../types.ts"; + +export default { + name: Events.ClientReady, + once: true, + execute(readyClient: Client) { + console.log(`Ready! Logged in as ${readyClient.user!.tag}`); + }, +} as Event; diff --git a/src/commands.ts b/src/gatherer.ts similarity index 82% rename from src/commands.ts rename to src/gatherer.ts index 5299d4e..b4fe897 100644 --- a/src/commands.ts +++ b/src/gatherer.ts @@ -1,8 +1,8 @@ import * as path from "jsr:@std/path"; -export async function gatherCommandsPaths(): Promise { +export async function gatherPaths(type: "commands" | "events"): Promise { const commandsPath: string[] = []; - const foldersPath = path.join(import.meta.dirname!, "commands"); + const foldersPath = path.join(import.meta.dirname!, type); const commandsRoot = Deno.readDir(foldersPath); for await (const entry of commandsRoot) { @@ -22,4 +22,4 @@ export async function gatherCommandsPaths(): Promise { } return commandsPath; -} +} \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index c36dae3..b590d8d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -7,8 +7,8 @@ import { MessageFlags, } from "npm:discord.js"; const token = Deno.env.get("token"); -import { Command } from "./types.ts"; -import { gatherCommandsPaths } from "./commands.ts"; +import { Command, Event } from "./types.ts"; +import { gatherPaths } from "./gatherer.ts"; import { exists } from "jsr:@std/fs/exists"; import cai from "./cai.ts"; @@ -20,20 +20,36 @@ class Client extends _client { } } -const client = new Client({ intents: [GatewayIntentBits.Guilds] }); +const client = new Client({ + intents: [GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent], +}); -async function register(commandPath: string) { - const { default: command }: { default: Command } = await import(commandPath); +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); } else { console.log( - `[WARNING] The command at ${commandPath} is missing a required "data" or "execute" property.` + `[WARNING] The command at ${path} is missing a required "data" or "execute" property.` ); } } -(await gatherCommandsPaths()).forEach(register); +async function registerEvent(path: string) { + const { default: event }: { default: Event } = await import(path); + if ("name" in event && "once" in event && "execute" in event) { + if(event.once) { + client.once(event.name, event.execute) + } else client.on(event.name, event.execute) + } else { + console.log( + `[WARNING] The event at ${path} is missing a required "name", "once" or "execute" property.` + ); + } +} + +(await gatherPaths("commands")).forEach(registerCommand); +(await gatherPaths("events")).forEach(registerEvent); client.on(Events.InteractionCreate, async (interaction) => { if (!interaction.isChatInputCommand()) return; @@ -63,10 +79,6 @@ client.on(Events.InteractionCreate, async (interaction) => { } }); -client.once(Events.ClientReady, (readyClient) => { - console.log(`Ready! Logged in as ${readyClient.user.tag}`); -}); - if (await exists("./cai.token", { isFile: true })) { await cai.login(await Deno.readTextFile("./cai.token")); cai.logged = true; diff --git a/src/types.ts b/src/types.ts index fbb5342..80e6d7a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -6,3 +6,9 @@ export type Command = { interaction: ChatInputCommandInteraction ) => Promise; }; + +export type Event = { + name: string, + once: boolean, + execute: (...args:unknown[]) => Promise +} \ No newline at end of file