mirror of
https://github.com/jaronpate/unicord.git
synced 2026-10-18 21:47:40 +00:00
command manager hooked up to client
This commit is contained in:
@@ -2,6 +2,7 @@ import type {
|
|||||||
ArgumentTypeFromOptions,
|
ArgumentTypeFromOptions,
|
||||||
Unicord,
|
Unicord,
|
||||||
UnicordArgumentDefinition,
|
UnicordArgumentDefinition,
|
||||||
|
UnicordCommandHandler,
|
||||||
UnicordCommandOptions,
|
UnicordCommandOptions,
|
||||||
UnicordEventContext,
|
UnicordEventContext,
|
||||||
UnicordHandler,
|
UnicordHandler,
|
||||||
@@ -64,7 +65,7 @@ export class UnicordCommandManager {
|
|||||||
register<const Options extends UnicordCommandOptions>(
|
register<const Options extends UnicordCommandOptions>(
|
||||||
type: UnicordEventType.ChatCommands | UnicordEventType.ApplicationCommands,
|
type: UnicordEventType.ChatCommands | UnicordEventType.ApplicationCommands,
|
||||||
event: string,
|
event: string,
|
||||||
handler: UnicordHandler<Options>,
|
handler: UnicordCommandHandler<Options>,
|
||||||
options: Options = { args: [] } as unknown as Options,
|
options: Options = { args: [] } as unknown as Options,
|
||||||
) {
|
) {
|
||||||
if (this.handlers[type].has(event) === false) {
|
if (this.handlers[type].has(event) === false) {
|
||||||
@@ -74,15 +75,14 @@ export class UnicordCommandManager {
|
|||||||
this.handlers[type].get(event)!.push({ options, handler });
|
this.handlers[type].get(event)!.push({ options, handler });
|
||||||
|
|
||||||
// Generate a closure to wrap the handler
|
// Generate a closure to wrap the handler
|
||||||
const wrappedHandler = async (context: UnicordCommandContext | UnicordEventContext, args: any[]) => {
|
const wrappedHandler = (async (context: UnicordCommandContext, args: any[]) => {
|
||||||
console.log('Raw Args:', args);
|
console.log('Raw Args:', args);
|
||||||
console.log('options:', options.args);
|
console.log('options:', options.args);
|
||||||
const resolvedArgs = await this.validateAndResolveArgs<Options>(args, options.args);
|
const resolvedArgs = await this.validateAndResolveArgs<Options>(args, options.args);
|
||||||
console.log('Resolved Args:', resolvedArgs);
|
console.log('Resolved Args:', resolvedArgs);
|
||||||
// TODO: Fix context type here
|
// TODO: Fix context type here
|
||||||
// @ts-expect-error will fix later
|
|
||||||
return handler(context, resolvedArgs);
|
return handler(context, resolvedArgs);
|
||||||
};
|
}) as UnicordCommandHandler<Options>;
|
||||||
|
|
||||||
this.eventProcessor.register(`${type}:${event}`, wrappedHandler);
|
this.eventProcessor.register(`${type}:${event}`, wrappedHandler);
|
||||||
return this.self;
|
return this.self;
|
||||||
|
|||||||
+14
-6
@@ -6,6 +6,7 @@ import { UnicordEventProcessor } from './eventProcessor';
|
|||||||
import { UnicordGatewayManager } from './gatewayManager';
|
import { UnicordGatewayManager } from './gatewayManager';
|
||||||
import { hydrate } from './hydrator';
|
import { hydrate } from './hydrator';
|
||||||
import type {
|
import type {
|
||||||
|
UnicordCommandHandler,
|
||||||
UnicordCommandOptions,
|
UnicordCommandOptions,
|
||||||
UnicordConfig,
|
UnicordConfig,
|
||||||
UnicordConfigWithDefaults,
|
UnicordConfigWithDefaults,
|
||||||
@@ -78,14 +79,21 @@ export class Unicord {
|
|||||||
this.eventProcessor.emit(`${type}:${event}`, context, args);
|
this.eventProcessor.emit(`${type}:${event}`, context, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: type signature from registerChatCommand needs to transfer to here
|
// TODO: can we automate defining these methods with some TS magic?
|
||||||
get registerChatCommand() {
|
registerChatCommand<const Options extends UnicordCommandOptions>(
|
||||||
return this.commandManager.registerChatCommand;
|
event: string,
|
||||||
|
handler: UnicordCommandHandler<Options>,
|
||||||
|
options: Options = { args: [] } as unknown as Options,
|
||||||
|
) {
|
||||||
|
return this.commandManager.registerChatCommand(event, handler, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: type signature from registerApplicationCommand needs to transfer to here
|
registerApplicationCommand<const Options extends UnicordCommandOptions>(
|
||||||
get registerApplicationCommand() {
|
event: string,
|
||||||
return this.commandManager.registerApplicationCommand;
|
handler: UnicordCommandHandler<Options>,
|
||||||
|
options: Options = { args: [] } as unknown as Options,
|
||||||
|
) {
|
||||||
|
return this.commandManager.registerApplicationCommand(event, handler, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: should have a different signature for events that are not commands
|
// TODO: should have a different signature for events that are not commands
|
||||||
|
|||||||
Reference in New Issue
Block a user