-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
event.js
33 lines (30 loc) · 1.54 KB
/
event.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
module.exports = class EventHandler {
/**
*
* @param {*} client - discord.js client
*/
constructor(client) {
this.client = client;
}
/**
* @param {Object} options - Options for the event handler
* @param {String} options.dir - Directory of events | Example: /events
* @param {Boolean} options.singleFolder - If you have all events in one folder, set this to true.
*
* @returns {void} - Returns nothing
*/
fetchEvents({ directory, singleFolder = false }) {
const { readdirSync } = require('fs');
if (!singleFolder) for (const folder of readdirSync(directory)) {
for (const file of readdirSync(`${directory}/${folder}`).filter(file => file.endsWith('.js'))) {
const event = require(`${directory}/${folder}/${file}`);
if (event.name && event.execute) this.client[event.once ? 'once' : 'on'](event.name, (...args) => event.execute(this.client, ...args));
else require(`${__dirname}/index`).log(`Failed to load event: ${event.name} - Missing name or execute function.`);
}
} else for (const file of readdirSync(directory).filter(file => file.endsWith('.js'))) {
const event = require(`${directory}/${file}`);
if (event.name && event.execute) this.client[event.once ? 'once' : 'on'](event.name, (...args) => event.execute(this.client, ...args));
else require(`${__dirname}/index`).log(`Failed to load event: ${event.name} - Missing name or execute function.`);
}
}
}