Skip to content

Commit 13fde9e

Browse files
committed
feat: switch to new handler
1 parent 13a6de5 commit 13fde9e

File tree

19 files changed

+344
-1932
lines changed

19 files changed

+344
-1932
lines changed

packages/commandkit/src/CommandKit.ts

Lines changed: 30 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
import EventEmitter from 'node:events';
2-
import {
3-
CommandHandler,
4-
EventHandler,
5-
ValidationHandler,
6-
} from './legacy/handlers';
7-
import type {
8-
CommandKitData,
9-
CommandKitOptions,
10-
CommandObject,
11-
ReloadOptions,
12-
} from './types';
2+
import type { CommandKitOptions } from './types';
133
import colors from './utils/colors';
144
import { CacheProvider } from './cache/CacheProvider';
155
import { MemoryCache } from './cache/MemoryCache';
@@ -19,9 +9,10 @@ import { Awaitable, Locale, Message } from 'discord.js';
199
import { DefaultLocalizationStrategy } from './app/i18n/DefaultLocalizationStrategy';
2010
import { findAppDirectory } from './utils/utilities';
2111
import { join } from 'node:path';
22-
import { AppCommandHandler } from './app/command-handler/AppCommandHandler';
12+
import { AppCommandHandler } from './app/handlers/AppCommandHandler';
2313
import { LocalizationStrategy } from './app/i18n/LocalizationStrategy';
2414
import { CommandsRouter, EventsRouter } from './app/router';
15+
import { AppEventsHandler } from './app/handlers/AppEventsHandler';
2516

2617
export interface CommandKitConfiguration {
2718
defaultLocale: Locale;
@@ -30,7 +21,6 @@ export interface CommandKitConfiguration {
3021
}
3122

3223
export class CommandKit extends EventEmitter {
33-
#data: CommandKitData;
3424
#started = false;
3525
public readonly eventInterceptor: EventInterceptor;
3626

@@ -45,7 +35,8 @@ export class CommandKit extends EventEmitter {
4535

4636
public commandsRouter!: CommandsRouter;
4737
public eventsRouter!: EventsRouter;
48-
public appCommandsHandler = new AppCommandHandler(this);
38+
public commandHandler = new AppCommandHandler(this);
39+
public eventHandler = new AppEventsHandler(this);
4940

5041
static instance: CommandKit | undefined = undefined;
5142

@@ -55,7 +46,7 @@ export class CommandKit extends EventEmitter {
5546
* @param options - The default CommandKit configuration.
5647
* @see {@link https://commandkit.js.org/docs/guide/commandkit-setup}
5748
*/
58-
constructor(options: CommandKitOptions) {
49+
constructor(private options: CommandKitOptions) {
5950
if (CommandKit.instance) {
6051
process.emitWarning(
6152
'CommandKit instance already exists. Having multiple instance in same project is discouraged and it may lead to unexpected behavior.',
@@ -91,8 +82,6 @@ export class CommandKit extends EventEmitter {
9182

9283
this.eventInterceptor = new EventInterceptor(options.client);
9384

94-
this.#data = options;
95-
9685
if (!CommandKit.instance) {
9786
CommandKit.instance = this;
9887
}
@@ -109,13 +98,15 @@ export class CommandKit extends EventEmitter {
10998

11099
this.incrementClientListenersCount();
111100

112-
if (token !== false && !this.#data.client.isReady()) {
113-
await this.#data.client.login(
101+
if (token !== false && !this.options.client.isReady()) {
102+
await this.options.client.login(
114103
token ?? process.env.TOKEN ?? process.env.DISCORD_TOKEN,
115104
);
116105
}
117106

118107
this.#started = true;
108+
109+
await this.commandHandler.registrar.register();
119110
}
120111

121112
/**
@@ -158,77 +149,25 @@ export class CommandKit extends EventEmitter {
158149
* Resolves the current cache provider.
159150
*/
160151
getCacheProvider(): CacheProvider | null {
161-
const provider = this.#data.cacheProvider;
152+
const provider = this.options.cacheProvider;
162153
return provider ?? null;
163154
}
164155

165156
/**
166157
* Whether or not to debug the command handler.
167158
*/
168159
isDebuggingCommands() {
169-
return this.#data.debugCommands || false;
160+
return this.options.debugCommands || false;
170161
}
171162

172163
/**
173164
* Get the client attached to this CommandKit instance.
174165
*/
175166
get client() {
176-
return this.#data.client;
167+
return this.options.client;
177168
}
178169

179-
/**
180-
* Get command handler instance.
181-
*/
182-
get commandHandler() {
183-
return this.#data.commandHandler;
184-
}
185-
186-
/**
187-
* (Private) Initialize CommandKit.
188-
*/
189170
async #init() {
190-
// <!-- Setup event handler -->
191-
const eventHandler = new EventHandler({
192-
client: this.#data.client,
193-
eventsPath: this.#data.eventsPath,
194-
commandKitInstance: this,
195-
});
196-
197-
await eventHandler.init();
198-
199-
this.#data.eventHandler = eventHandler;
200-
201-
// <!-- Setup validation handler -->
202-
if (this.#data.validationsPath) {
203-
const validationHandler = new ValidationHandler({
204-
validationsPath: this.#data.validationsPath,
205-
});
206-
207-
await validationHandler.init();
208-
209-
this.#data.validationHandler = validationHandler;
210-
}
211-
212-
// <!-- Setup command handler -->
213-
const commandHandler = new CommandHandler({
214-
client: this.#data.client,
215-
commandsPath: this.#data.commandsPath,
216-
devGuildIds: this.#data.devGuildIds || [],
217-
devUserIds: this.#data.devUserIds || [],
218-
devRoleIds: this.#data.devRoleIds || [],
219-
validationHandler: this.#data.validationHandler,
220-
skipBuiltInValidations: this.#data.skipBuiltInValidations || false,
221-
commandkitInstance: this,
222-
bulkRegister: this.#data.bulkRegister || false,
223-
});
224-
225-
this.#data.commandHandler = commandHandler;
226-
227-
await this.#initApp();
228-
await commandHandler.init();
229-
}
230-
231-
async #initApp() {
232171
const appDir = this.getAppDirectory();
233172
if (!appDir) return;
234173

@@ -252,117 +191,89 @@ export class CommandKit extends EventEmitter {
252191
await this.commandsRouter.scan();
253192
}
254193

255-
await this.appCommandsHandler.loadCommands();
194+
await this.commandHandler.loadCommands();
256195
}
257196

258197
async #initEvents() {
259198
if (this.eventsRouter.isValidPath()) {
260199
await this.eventsRouter.scan();
261200
}
262201

263-
if (!this.#data.eventHandler) return;
264-
265-
for (const event of Object.values(this.eventsRouter.toJSON())) {
266-
this.#data.eventHandler.registerExternal(event);
267-
}
268-
269-
this.#data.eventHandler.resyncListeners();
202+
await this.eventHandler.loadEvents();
270203
}
271204

272205
/**
273206
* Updates application commands with the latest from "commandsPath".
274207
*/
275-
async reloadCommands(type?: ReloadOptions) {
276-
if (!this.#data.commandHandler) return;
277-
await this.#data.commandHandler.reloadCommands(type);
208+
async reloadCommands() {
209+
await this.commandHandler.reloadCommands();
278210
}
279211

280212
/**
281213
* Updates application events with the latest from "eventsPath".
282214
*/
283215
async reloadEvents() {
284-
if (!this.#data.eventHandler) return;
285-
await this.#data.eventHandler.reloadEvents(this.#data.commandHandler);
286-
}
287-
288-
/**
289-
* Updates application command validations with the latest from "validationsPath".
290-
*/
291-
async reloadValidations() {
292-
if (!this.#data.validationHandler) return;
293-
await this.#data.validationHandler.reloadValidations();
294-
}
295-
296-
/**
297-
* @returns An array of objects of all the commands that CommandKit is handling.
298-
*/
299-
get commands(): CommandObject[] {
300-
if (!this.#data.commandHandler) {
301-
return [];
302-
}
303-
304-
const commands = this.#data.commandHandler.commands.map((cmd) => {
305-
const { run, autocomplete, ...command } = cmd;
306-
return command;
307-
});
308-
309-
return commands;
216+
await this.eventHandler.reloadEvents();
310217
}
311218

312219
/**
313220
* @returns The path to the commands folder which was set when instantiating CommandKit.
314221
*/
315222
get commandsPath(): string | undefined {
316-
return this.#data.commandsPath;
223+
return this.options.commandsPath;
317224
}
318225

319226
/**
320227
* @returns The path to the events folder which was set when instantiating CommandKit.
321228
*/
322229
get eventsPath(): string | undefined {
323-
return this.#data.eventsPath;
230+
return this.options.eventsPath;
324231
}
325232

326233
/**
327234
* @returns The path to the validations folder which was set when instantiating CommandKit.
328235
*/
329236
get validationsPath(): string | undefined {
330-
return this.#data.validationsPath;
237+
return this.options.validationsPath;
331238
}
332239

333240
/**
334241
* @returns An array of all the developer user IDs which was set when instantiating CommandKit.
335242
*/
336243
get devUserIds(): string[] {
337-
return this.#data.devUserIds || [];
244+
return this.options.devUserIds || [];
338245
}
339246

340247
/**
341248
* @returns An array of all the developer guild IDs which was set when instantiating CommandKit.
342249
*/
343250
get devGuildIds(): string[] {
344-
return this.#data.devGuildIds || [];
251+
return this.options.devGuildIds || [];
345252
}
346253

347254
/**
348255
* @returns An array of all the developer role IDs which was set when instantiating CommandKit.
349256
*/
350257
get devRoleIds(): string[] {
351-
return this.#data.devRoleIds || [];
258+
return this.options.devRoleIds || [];
352259
}
353260

354261
/**
355262
* Increment the client listeners count.
356263
*/
357264
incrementClientListenersCount() {
358-
this.#data.client.setMaxListeners(this.#data.client.getMaxListeners() + 1);
265+
this.options.client.setMaxListeners(
266+
this.options.client.getMaxListeners() + 1,
267+
);
359268
}
360269

361270
/**
362271
* Decrement the client listeners count.
363272
*/
364273
decrementClientListenersCount() {
365-
this.#data.client.setMaxListeners(this.#data.client.getMaxListeners() - 1);
274+
this.options.client.setMaxListeners(
275+
this.options.client.getMaxListeners() - 1,
276+
);
366277
}
367278

368279
/**

packages/commandkit/src/app/command-handler/AppCommandHandler.ts renamed to packages/commandkit/src/app/handlers/AppCommandHandler.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { TranslatableCommandOptions } from '../i18n/Translation';
1414
import { MessageCommandParser } from '../commands/MessageCommandParser';
1515
import { CommandKitErrorCodes, isErrorType } from '../../utils/error-codes';
1616
import { ParsedCommand, ParsedMiddleware } from '../router';
17+
import { CommandRegistrar } from '../register/CommandRegistrar';
1718

1819
interface AppCommand {
1920
command: SlashCommandBuilder | Record<string, any>;
@@ -29,7 +30,7 @@ interface AppCommandMiddleware {
2930
afterExecute: (ctx: Context) => Awaitable<unknown>;
3031
}
3132

32-
interface LoadedCommand {
33+
export interface LoadedCommand {
3334
command: ParsedCommand;
3435
data: AppCommand;
3536
guilds?: string[];
@@ -71,11 +72,15 @@ const middlewareDataSchema = {
7172
export class AppCommandHandler {
7273
private loadedCommands = new Collection<string, LoadedCommand>();
7374
private loadedMiddlewares = new Collection<string, LoadedMiddleware>();
75+
public readonly registrar: CommandRegistrar;
7476

75-
public constructor(public readonly commandkit: CommandKit) {}
77+
public constructor(public readonly commandkit: CommandKit) {
78+
this.registrar = new CommandRegistrar(this.commandkit);
79+
}
7680

7781
public getCommandsArray() {
78-
return Array.from(this.loadedCommands.values());
82+
const loaded = Array.from(this.loadedCommands.values());
83+
return loaded;
7984
}
8085

8186
public async prepareCommandRun(
@@ -168,6 +173,13 @@ export class AppCommandHandler {
168173
};
169174
}
170175

176+
public async reloadCommands() {
177+
this.loadedCommands.clear();
178+
this.loadedMiddlewares.clear();
179+
180+
await this.loadCommands();
181+
}
182+
171183
public async loadCommands() {
172184
const commandsRouter = this.commandkit.commandsRouter;
173185

0 commit comments

Comments
 (0)