|
1 | | -import { CommandHandler, EventHandler, ValidationHandler } from './handlers'; |
2 | | -import { CommandKitData, CommandKitOptions } from '../typings'; |
| 1 | +import { CommandHandler, EventHandler, ValidationHandler } from "./handlers"; |
| 2 | +import { CommandKitData, CommandKitOptions } from "./typings"; |
| 3 | +import "colors"; |
3 | 4 |
|
4 | 5 | export class CommandKit { |
5 | | - private _data: CommandKitData; |
| 6 | + #data: CommandKitData; |
6 | 7 |
|
7 | 8 | constructor({ ...options }: CommandKitOptions) { |
8 | 9 | if (!options.client) { |
9 | | - throw new Error('"client" is required when instantiating CommandKit.'); |
| 10 | + throw new Error('"client" is required when instantiating CommandKit.'.red); |
10 | 11 | } |
11 | 12 |
|
12 | 13 | if (options.validationsPath && !options.commandsPath) { |
13 | | - throw new Error('"commandsPath" is required when "validationsPath" is set.'); |
| 14 | + throw new Error('"commandsPath" is required when "validationsPath" is set.'.red); |
14 | 15 | } |
15 | 16 |
|
16 | | - this._data = { |
| 17 | + this.#data = { |
17 | 18 | ...options, |
18 | 19 | commands: [], |
19 | 20 | }; |
20 | 21 |
|
21 | | - this._init(); |
| 22 | + this.#init(); |
22 | 23 | } |
23 | 24 |
|
24 | | - private _init() { |
| 25 | + async #init() { |
25 | 26 | // Event handler |
26 | | - if (this._data.eventsPath) { |
27 | | - new EventHandler({ |
28 | | - client: this._data.client, |
29 | | - eventsPath: this._data.eventsPath, |
| 27 | + if (this.#data.eventsPath) { |
| 28 | + const eventHandler = new EventHandler({ |
| 29 | + client: this.#data.client, |
| 30 | + eventsPath: this.#data.eventsPath, |
30 | 31 | }); |
| 32 | + |
| 33 | + await eventHandler.init(); |
31 | 34 | } |
32 | 35 |
|
33 | 36 | // Validation handler |
34 | 37 | let validationFunctions: Function[] = []; |
35 | 38 |
|
36 | | - if (this._data.validationsPath) { |
| 39 | + if (this.#data.validationsPath) { |
37 | 40 | const validationHandler = new ValidationHandler({ |
38 | | - validationsPath: this._data.validationsPath, |
| 41 | + validationsPath: this.#data.validationsPath, |
39 | 42 | }); |
40 | 43 |
|
41 | | - validationHandler.getValidations().forEach((v) => validationFunctions.push(v)); |
| 44 | + await validationHandler.init(); |
| 45 | + |
| 46 | + validationHandler.validations.forEach((v) => validationFunctions.push(v)); |
42 | 47 | } |
43 | 48 |
|
44 | 49 | // Command handler |
45 | | - if (this._data.commandsPath) { |
| 50 | + if (this.#data.commandsPath) { |
46 | 51 | const commandHandler = new CommandHandler({ |
47 | | - client: this._data.client, |
48 | | - commandsPath: this._data.commandsPath, |
49 | | - devGuildIds: this._data.devGuildIds || [], |
50 | | - devUserIds: this._data.devUserIds || [], |
51 | | - devRoleIds: this._data.devRoleIds || [], |
| 52 | + client: this.#data.client, |
| 53 | + commandsPath: this.#data.commandsPath, |
| 54 | + devGuildIds: this.#data.devGuildIds || [], |
| 55 | + devUserIds: this.#data.devUserIds || [], |
| 56 | + devRoleIds: this.#data.devRoleIds || [], |
52 | 57 | customValidations: validationFunctions, |
53 | | - skipBuiltInValidations: this._data.skipBuiltInValidations || false, |
| 58 | + skipBuiltInValidations: this.#data.skipBuiltInValidations || false, |
54 | 59 | }); |
55 | 60 |
|
56 | | - this._data.commands = commandHandler.getCommands(); |
| 61 | + await commandHandler.init(); |
| 62 | + |
| 63 | + this.#data.commands = commandHandler.commands; |
57 | 64 | } |
58 | 65 | } |
59 | 66 |
|
60 | 67 | get commands() { |
61 | | - return this._data.commands.map((cmd) => { |
| 68 | + return this.#data.commands.map((cmd) => { |
62 | 69 | const { run, ...command } = cmd; |
63 | 70 | return command; |
64 | 71 | }); |
|
0 commit comments