-
Notifications
You must be signed in to change notification settings - Fork 1
/
commandHandler.js
97 lines (95 loc) · 3.86 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
87
88
89
90
91
92
93
94
95
96
97
const fs = require('fs');
const CooldownManager = require(`${process.cwd()}/cooldown.js`);
class CommandHandler {
constructor () {
this.commands = new Map();
this.cooldownManager = new CooldownManager();
this.hasWarnedAboutDev = false;
}
registerCommand (commandFileLink) {
const command = require(`${process.cwd()}/${commandFileLink}`);
if (command.info === undefined) {console.log(`${commandFileLink} has no info value, will skip`); return;}
if (command.info.name === undefined) {console.log(`${commandFileLink} has no name, will skip`);}
if (Array.isArray(command.info.name)) {
for (let i = 0; i < command.info.name.length; i++) {
if (!this.commands.has(command.info.name[i])) {
this.commands.set(command.info.name[i], command);
this.cooldownManager.register(command.info.name[i], command.info.cooldown);
}
else {console.log(`Duplicate alias/name for ${commandFileLink} "${command.info.name[i]}" `);}
}
}
else {
this.cooldownManager.register(command.info.name, command.info.cooldown);
this.commands.set(command.info.name, command);
}
}
registerCommandsIn (folder) {
const files = fs.readdirSync(folder);
for (let i = 0; i < files.length; i++) {
this.registerCommand(`${folder}/${files[i]}`);
}
}
getAllInfos () {
const infos = [];
const keysToIgnore = []; //for aliases, idk what im doing
this.commands.forEach((value, key, map) => {
if (!keysToIgnore.includes(key)) {
if (Array.isArray(value.info.name)) {
keysToIgnore.push(...value.info.name);
infos.push(value);
}
else {infos.push(value);}
}
});
return infos;
}
getInfoFor (command) {
return (this.commands.has(command)?this.commands.get(command).info:{});
}
async go (what, client, message, args) {
const cooldown = this.cooldownManager.checkCooldown(message.author);
if (cooldown) {
if (process.argv[2] == "dev" && !this.hasWarnedAboutDev) {
message.channel.send(":warning: I am in dev mode - I am probably running on the development server and database. Things may not work.");
this.hasWarnedAboutDev = true;
}
this.commands.get(what).run(client, message, args);
this.cooldownManager.insertCooldown(message.author, what);
}
else {
const cooldownLeft = this.cooldownManager.checkTimeLeft(message.author);
const msg = await message.channel.send(embeds.cooldown(message, cooldownLeft, what));
client.setTimeout(()=>{msg.delete();}, cooldownLeft);
}
}
run (what, client, message, args) {
try {
if (this.commands.has(what)) {
//check for permissions
if (this.commands.get(what).permission) {
if (this.commands.get(what).permission(message)) {
this.go(what, client, message, args);
}
else {
message.channel.send(embeds.noPermissionEmbed(message, what, this.commands.get(what).info.restriction));
}
}
else {
this.go(what, client, message, args);
}
}
else {
message.channel.send(embeds.commandDoesntExistError(message, what));
}
}
catch (err) {
console.error(err);
message.channel.send(embeds.errorOccured(message, err));
}
}
has (command) {
return this.commands.has(command);
}
}
module.exports = CommandHandler;