feat: initial Papo bot scaffold
This commit is contained in:
36
src/services/eventHandler.ts
Normal file
36
src/services/eventHandler.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { Client } from 'discord.js';
|
||||
import { EventHandler } from '../utils/types.js';
|
||||
import { logger } from '../utils/logger.js';
|
||||
|
||||
export class EventHandlerService {
|
||||
constructor(private client: Client) {}
|
||||
|
||||
public async loadEvents() {
|
||||
const eventsPath = path.join(process.cwd(), 'src', 'events');
|
||||
const files = this.walk(eventsPath);
|
||||
for (const file of files) {
|
||||
const mod = await import(file);
|
||||
const event: EventHandler = mod.default;
|
||||
if (!event?.name || !event.execute) continue;
|
||||
if (event.once) {
|
||||
this.client.once(event.name, (...args) => event.execute(...args));
|
||||
} else {
|
||||
this.client.on(event.name, (...args) => event.execute(...args));
|
||||
}
|
||||
logger.info(`Bound event ${event.name}`);
|
||||
}
|
||||
}
|
||||
|
||||
private walk(dir: string): string[] {
|
||||
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
||||
const files: string[] = [];
|
||||
for (const entry of entries) {
|
||||
const res = path.resolve(dir, entry.name);
|
||||
if (entry.isDirectory()) files.push(...this.walk(res));
|
||||
else if (entry.isFile() && (entry.name.endsWith('.ts') || entry.name.endsWith('.js'))) files.push(res);
|
||||
}
|
||||
return files;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user