1 | const { readFileSync: read } = require('fs');
|
2 | const { join } = require('path');
|
3 |
|
4 | const { CommandError } = require('./Command');
|
5 |
|
6 | function load() {
|
7 | const dataPath = join(__dirname, '../../data/commands.json');
|
8 | const dataFile = read(dataPath, 'utf-8');
|
9 | const commands = JSON.parse(dataFile);
|
10 |
|
11 | for (const command of Object.keys(commands)) {
|
12 | const commandModule = commands[command];
|
13 | let CommandClass;
|
14 |
|
15 | try {
|
16 |
|
17 | CommandClass = require(commandModule);
|
18 | } catch (e) {
|
19 | throw new CommandError(
|
20 | `The command module '${commandModule}' was not found.`
|
21 | );
|
22 | }
|
23 |
|
24 | commands[command] = new CommandClass();
|
25 | }
|
26 |
|
27 | return commands;
|
28 | }
|
29 |
|
30 | module.exports = {
|
31 | CommandError,
|
32 | help() {
|
33 | const result = Object.keys(load())
|
34 | .sort()
|
35 | .join('\n ');
|
36 |
|
37 | return ` ${result}`;
|
38 | },
|
39 |
|
40 | load,
|
41 | };
|