Skip to content

Commit

Permalink
feat(command-events): pass more context (#162)
Browse files Browse the repository at this point in the history
Co-authored-by: Jeroen Claassens <support@favware.tech>
  • Loading branch information
kyranet and favna authored Feb 6, 2021
1 parent 65316a6 commit 11a6274
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
11 changes: 6 additions & 5 deletions src/events/command-handler/CoreCommandAccepted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ export class CoreEvent extends Event<Events.CommandAccepted> {
super(context, { event: Events.CommandAccepted });
}

public async run({ message, command, parameters, context }: CommandAcceptedPayload) {
public async run(payload: CommandAcceptedPayload) {
const { message, command, parameters, context } = payload;
const args = await command.preParse(message, parameters, context);
try {
message.client.emit(Events.CommandRun, message, command);
message.client.emit(Events.CommandRun, message, command, payload);
const result = await command.run(message, args, context);
message.client.emit(Events.CommandSuccess, { message, command, result, parameters });
message.client.emit(Events.CommandSuccess, { ...payload, result });
} catch (error) {
message.client.emit(Events.CommandError, error, { piece: command, message });
message.client.emit(Events.CommandError, error, { ...payload, piece: command });
} finally {
message.client.emit(Events.CommandFinish, message, command);
message.client.emit(Events.CommandFinish, message, command, payload);
}
}
}
10 changes: 4 additions & 6 deletions src/lib/types/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ export interface EventErrorPayload extends IPieceError {
piece: Event;
}

export interface CommandErrorPayload extends IPieceError {
export interface CommandErrorPayload extends CommandAcceptedPayload {
piece: Command;
message: Message;
}

export interface ICommandPayload {
Expand All @@ -106,9 +105,8 @@ export interface CommandAcceptedPayload extends ICommandPayload {
context: CommandContext;
}

export interface CommandSuccessPayload extends ICommandPayload {
export interface CommandSuccessPayload extends CommandAcceptedPayload {
result: unknown;
parameters: string;
}

export interface PreCommandRunPayload extends CommandDeniedPayload {}
Expand All @@ -126,10 +124,10 @@ declare module 'discord.js' {
[Events.PreCommandRun]: [PreCommandRunPayload];
[Events.CommandDenied]: [UserError, CommandDeniedPayload];
[Events.CommandAccepted]: [CommandAcceptedPayload];
[Events.CommandRun]: [Message, Command];
[Events.CommandRun]: [Message, Command, CommandAcceptedPayload];
[Events.CommandSuccess]: [CommandSuccessPayload];
[Events.CommandError]: [Error, CommandErrorPayload];
[Events.CommandFinish]: [Message, Command];
[Events.CommandFinish]: [Message, Command, CommandAcceptedPayload];
[Events.PluginLoaded]: [PluginHook, string | undefined];
// #endregion Sapphire load cycle events

Expand Down

0 comments on commit 11a6274

Please sign in to comment.