refactored command management into a seperate service for future features

This commit is contained in:
2025-10-27 02:38:45 -05:00
parent 6c59334c27
commit e711473b7f
3 changed files with 113 additions and 75 deletions
+17 -20
View File
@@ -1,11 +1,11 @@
import { UnicordChannelsCache, UnicordGuildsCache, UnicordUsersCache } from './caches';
import { UnicordCommandManager } from './commandManager';
import type { UnicordCommandContext } from './context';
import { UnicordDiscordAPI } from './discordAPI';
import { UnicordEventProcessor } from './eventProcessor';
import { UnicordGatewayManager } from './gatewayManager';
import { hydrate } from './hydrator';
import type {
UnicordCommandHandler,
UnicordCommandOptions,
UnicordConfig,
UnicordConfigWithDefaults,
@@ -27,6 +27,12 @@ export class Unicord {
readonly discordAPI: UnicordDiscordAPI;
private readonly eventProcessor: UnicordEventProcessor;
private readonly gatewayManager: UnicordGatewayManager;
// TODO: split command management out of the event processor
// This way we can properly store and manage registered commands
// This will enable features like command listing, removal, autogenerated help, etc.
// We can also go registration with the Discord API here
// This will also eliminate the weird helper methods on the Unicord class itself
private readonly commandManager: UnicordCommandManager;
readonly caches: {
readonly users: UnicordUsersCache;
readonly channels: UnicordChannelsCache;
@@ -38,6 +44,7 @@ export class Unicord {
this.discordAPI = new UnicordDiscordAPI(this);
this.eventProcessor = new UnicordEventProcessor(this);
this.gatewayManager = new UnicordGatewayManager(this);
this.commandManager = new UnicordCommandManager(this, this.eventProcessor);
this.caches = {
users: new UnicordUsersCache(this),
channels: new UnicordChannelsCache(this),
@@ -59,41 +66,31 @@ export class Unicord {
return hydrate.bind(null, this);
}
private registerEvent<Options extends UnicordCommandOptions>(
private __onEvent<Options extends UnicordCommandOptions>(
type: UnicordEventType,
event: string,
handler: UnicordHandler<Options>,
options: Options = { args: [] } as unknown as Options,
) {
return this.eventProcessor.register(`${type}:${event}`, handler, options);
return this.eventProcessor.register(`${type}:${event}`, handler);
}
emit(type: UnicordEventType, event: string, context: UnicordCommandContext | UnicordEventContext, args: any[]) {
this.eventProcessor.emit(`${type}:${event}`, context, args);
}
registerChatCommand<const Options extends UnicordCommandOptions>(
event: string,
handler: UnicordCommandHandler<Options>,
options: Options = { args: [] } as unknown as Options,
): this {
this.registerEvent(UnicordEventType.ChatCommands, event, handler, options);
return this;
// TODO: type signature from registerChatCommand needs to transfer to here
get registerChatCommand() {
return this.commandManager.registerChatCommand;
}
registerApplicationCommand<const Options extends UnicordCommandOptions>(
event: string,
handler: UnicordCommandHandler<Options>,
options: Options = { args: [] } as unknown as Options,
): this {
// TODO: Register with Discord API
this.registerEvent(UnicordEventType.ApplicationCommands, event, handler, options);
return this;
// TODO: type signature from registerApplicationCommand needs to transfer to here
get registerApplicationCommand() {
return this.commandManager.registerApplicationCommand;
}
// TODO: should have a different signature for events that are not commands
onEvent(event: string, handler: UnicordEventHandler): this {
this.registerEvent(UnicordEventType.SystemEvent, event, handler);
this.__onEvent(UnicordEventType.SystemEvent, event, handler);
return this;
}