feat: per file event handling

This commit is contained in:
HerozDotExe 2025-06-19 14:50:47 +02:00
parent 18c02e98f3
commit 9dbc276a9e
8 changed files with 58 additions and 18 deletions

View file

@ -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)
}

View file

@ -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<CacheType>) {
await interaction.reply("Pong!");
},
};
} as Command;

View file

@ -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;

10
src/events/template.ts Normal file
View file

@ -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;

10
src/events/utils/ready.ts Normal file
View file

@ -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;

View file

@ -1,8 +1,8 @@
import * as path from "jsr:@std/path";
export async function gatherCommandsPaths(): Promise<string[]> {
export async function gatherPaths(type: "commands" | "events"): Promise<string[]> {
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) {

View file

@ -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;

View file

@ -6,3 +6,9 @@ export type Command = {
interaction: ChatInputCommandInteraction<CacheType>
) => Promise<void>;
};
export type Event = {
name: string,
once: boolean,
execute: (...args:unknown[]) => Promise<void>
}