Files
gestion-perso/fonctions/loadEvents.ts
T
2024-02-26 11:20:08 +01:00

23 lines
889 B
TypeScript

import { Client } from 'discord.js';
import fs from 'fs';
import path from 'path';
module.exports = function loadEvents(client: Client, dir: string): number {
let count = 0;
fs.readdirSync(path.join(__dirname, dir)).forEach((file: string) => {
const filePath = path.join(__dirname, dir, file);
if (fs.statSync(filePath).isDirectory()) {
loadEvents(client, path.join(dir, file));
} else if (file.endsWith('.js') || file.endsWith('.ts')) {
delete require.cache[require.resolve(filePath)];
const event = require(filePath);
if (typeof event.execute === 'function') {
client.on(event.name, (...args: any[]) => event.execute(...args, client)); // Specify the type of 'args' as an array of any type
count++;
} else {
console.error(`Event ${event.name} does not have an execute method.`);
}
}
});
return count;
}