32 lines
991 B
TypeScript
32 lines
991 B
TypeScript
import { join } from "path";
|
|
import { Collection } from "discord.js";
|
|
import getAllFiles from "@bot/utils/getAllFiles";
|
|
import Event from "@bot/components/event";
|
|
import DiscordBot from "@bot/bot";
|
|
|
|
export default class EventHandler extends Collection<string, Event> {
|
|
readonly client: DiscordBot;
|
|
|
|
constructor(client: DiscordBot) {
|
|
super();
|
|
this.client = client
|
|
this.init();
|
|
}
|
|
|
|
private async init() {
|
|
const eventsDirectory = join(__dirname, "../events");
|
|
const modules = getAllFiles(eventsDirectory);
|
|
|
|
for (const module of modules) {
|
|
const imported = await import(module);
|
|
const eventClass = imported.default || imported;
|
|
const event: Event = new eventClass(this.client);
|
|
this.set(event.name, event);
|
|
|
|
this.client[event.once ? "once" : "on"](
|
|
event.name,
|
|
(...args: unknown[]) => event.execute(...args)
|
|
);
|
|
}
|
|
}
|
|
} |