-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
command.js
76 lines (65 loc) · 2.88 KB
/
command.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const { Collection } = require('discord.js');
module.exports = class CommandHandler {
/**
*
* @param {*} client - discord.js client
*/
constructor(client) {
this.client = client;
this.client.commands = new Collection();
}
/**
* @param {Object} options - Options for the command handler
* @param {String} options.directory - Directory of commands | Example: /commands
* @param {Boolean} options.singleFolder - If you have all commands in one folder, set this to true.
*
* @returns {Collection} - Returns a collection of commands
*/
fetchCommands({ 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 cmd = require(`${directory}/${folder}/${file}`);
cmd.path = `${directory}/${folder}/${file}`;
if (cmd.name && cmd.execute) this.client.commands.set(cmd.name.toLowerCase(), cmd);
else require(`${__dirname}/index`).log(`Failed to load command: ${cmd.name} - Missing name or execute function.`);
}
}
else for (const file of readdirSync(directory).filter(file => file.endsWith('.js'))) {
const cmd = require(`${directory}/${file}`);
cmd.path = `${directory}/${file}`;
if (cmd.name && cmd.execute) this.client.commands.set(cmd.name.toLowerCase(), cmd);
else require(`${__dirname}/index`).log(`Failed to load command: ${cmd.name} - Missing name or execute function.`);
}
return this.client.commands;
}
/**
*
* @param {String} path - The command path | Example: /commands/ping.js
*
*/
addCommand(path) {
const cmd = require(path);
if (cmd.name && cmd.execute) this.client.commands.set(cmd.name, cmd);
else require(`${__dirname}/index`).log(`Failed to load command: ${cmd.name} - Missing name or execute function.`);
}
/**
*
* @param {String} cmd - Command name
*
*/
reloadCommand(cmd) {
const command = this.client.commands.get(cmd.toLowerCase()) || this.client.commands.find((c) => c.aliases && c.aliases.includes(cmd.toLowerCase()));
if (!command) return { error: 'Command not found', success: false };
delete require.cache[require.resolve(command.path)];
try {
const c = require(command.path);
if (c.name && c.execute) {
this.client.commands.set(c.name, c);
return { success: true, error: false };
} else return { error: 'Command is missing name or execute function', success: false };
} catch (error) {
return { error: error.message, success: false };
}
}
}