-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCommandHandler.js
86 lines (80 loc) · 3.33 KB
/
CommandHandler.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
77
78
79
80
81
82
83
84
85
86
const { Collection } = require('discord.js');
const { readdirSync } = require('fs');
const path = require('path')
class CommandHandler {
constructor(client) {
this.commands = new Collection();
this.aliases = new Collection();
this.commandSize = this.commands.size;
this.client = client;
}
/**
* @param {String} [key='commands'|'aliases']
*/
toArray(key = 'commands') {
if (key == 'aliases')
return this.aliases.toJSON();
else
return this.commands.toJSON();
};
async init() {
const directories = await readdirSync("./src/commands/");
this.client.logger.log('CommandHandler', `Loading a total of ${directories.length} categories.`);
directories.forEach(async (dir) => {
const commands = await readdirSync("./src/commands/" + dir + "/");
commands.filter((cmd) => cmd.split(".").pop() === "js").forEach((cmd) => {
const response = this.loadCommand("./commands/" + dir, cmd);
if (response) {
if (typeof response == 'string')
this.client.logger.templates.Log('CommandHandler', response);
else
this.client.ErrorHandler.error({
params: [cmd],
errorCode: response.errorCode,
error: response.error
});
}
});
});
};
loadCommand(commandPath, commandName) {
try {
const checkCommandLoaded = require.cache[require.resolve(`.${commandPath}${path.sep}${commandName}`)];
if (checkCommandLoaded?.loaded) delete require.cache[require.resolve(`.${commandPath}${path.sep}${commandName}`)];
const props = new (require(`.${commandPath}${path.sep}${commandName}`));
this.client.logger.log('CommandHandler', `Loading Command: ${props.name}. 👌`, "log");
props.location = commandPath;
props.fileName = commandName;
if (props.init) {
props.init(this);
}
this.commands.set(props.name, props);
props.aliases.forEach((alias) => {
this.aliases.set(alias, props.name);
});
return false;
} catch (e) {
return {
errorCode: 'CmdLoadError',
error: e.message == 'require(...) is not a constructor' ? 'Komut bir class değil.': e.message
};
}
}
async unloadCommand(commandPath, commandName) {
let command;
if (this.client.commands.has(commandName)) {
command = this.client.commands.get(commandName);
} else if (this.client.aliases.has(commandName)) {
command = this.client.commands.get(this.client.aliases.get(commandName));
}
if (!command) {
return `The command \`${commandName}\` doesn't seem to exist, nor is it an alias. Try again!`;
}
if (command.shutdown) {
await command.shutdown(this);
}
delete require.cache[require.resolve(`.${commandPath}${path.sep}${commandName}`)];
return false;
}
}
module.exports = CommandHandler;