-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (115 loc) · 5.14 KB
/
index.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import EventModule from './structures/EventModule.js'
import CommandList from './structures/CommandList.js'
import fs from 'fs'
import { dirname, resolve } from 'path'
import ImportDir from '@yimura/import-dir'
import BaseCommand from './structures/commands/BaseCommand.js'
export default class CommandRegistrar extends EventModule {
commandList = new CommandList();
constructor(main) {
super(main);
this.register(CommandRegistrar, {
name: 'commandRegistrar'
});
Object.assign(this, {
BaseCommand
});
}
/**
* @param {string} commandName
*/
get(commandName) {
return this.commandList.get(commandName);
}
/**
* Register commands from a module
* @param {string} groupName The name to group the commands under
* @param {string} modulePath Path to the root of the module (you should have a folder called "commands")
* @param {boolean} [output = true] If the commands should be included in the generation of commands.json
*/
async registerCommands(groupName, modulePath, output = true) {
modulePath = dirname(modulePath).replace('file://', '');
const commands = ImportDir(modulePath + '/commands/', { recurse: true, noCache: true });
await this._recursiveRegister(groupName, commands);
this._awaitFullRegistration();
}
/**
* Waits for all submodules to register their commands
* @private
*/
_awaitFullRegistration() {
clearTimeout(this.waitTimeout);
this.waitTimeout = setTimeout(() => {
this.log.info('COMMANDS', `Mapping of commands done with ${this.commandList.registered} unique commands registered, ${this.commandList.size - this.commandList.registered} aliases registered.`);
if (this.config.generate_command_json)
this._updateOutput();
this.emit('ready');
}, 100);
}
/**
* @private
* @param {string} category The original category this bit was part of
* @param {Object} bits The command category object within a category folder
*/
async _recursiveRegister(category, bits) {
for (const bit in bits) {
if (bits.hasOwnProperty(bit)) {
if (bits[bit] instanceof Promise) {
try {
bits[bit] = (await bits[bit]).default;
const instance = new bits[bit](category, this._m);
if (instance.disabled) {
this.log.warn('COMMANDS', `Command disabled: "${instance.name}"`);
continue;
}
if (instance.permissions && instance.permissions.levels.filter(x => x.type === 'COMMAND_HANDLED').length == 1) {
if (!instance.permission || typeof instance.permission !== 'function') {
this.log.error('COMMANDS', `Command "${instance.name}" has COMMAND_HANDLED permission set but doesn't handle these!`);
continue;
}
}
this.commandList.set(instance.name, instance);
if (this.output && !instance.hidden) {
if (!this.output[category]) this.output[category] = [];
this.output[category].push(instance.raw);
}
else if (instance.hidden) {
this.log.info('COMMANDS', `Command hidden: "${instance.name}"`);
}
for (const alias of instance.aliases) {
this.commandList.set(alias, instance);
}
continue;
} catch (e) {
e.ignore = true;
this.log.warn('COMMANDS', `The following command: ${bit}\nGenerated the following error:\n${e.stack}`);
}
}
await this._recursiveRegister(category, bits[bit]);
}
}
}
/**
* Writes all the command information to data/commands.json
* @private
*/
_updateOutput() {
fs.writeFile(resolve(`./data/commands.json`), JSON.stringify(this.output, null, ' '), { flag: 'w+' }, (err) => {
if (err) {
throw err;
}
this.log.info('COMMANDS', 'Generated new "data/commands.json" with the mapped commands.');
});
}
async init() {
const commands = ImportDir(resolve(`./src/commands/`), { recurse: true, noCache: true });
this.commandList = new CommandList();
if (this.config.generate_command_json) this.output = {};
for (const categoryName in commands)
if (commands.hasOwnProperty(categoryName))
await this._recursiveRegister(categoryName, commands[categoryName]);
this._awaitFullRegistration();
this.globalStorage.set('prefix', this.config.development ? this.config.default_prefix.dev : this.config.default_prefix.prod);
return true;
}
}