Skip to content

Commit

Permalink
feat(command-duration): add duration to *commandSuccess payloads (#359
Browse files Browse the repository at this point in the history
)

Co-authored-by: Jeroen Claassens <support@favware.tech>
  • Loading branch information
feralheart and favna committed Jan 16, 2022
1 parent 81c9440 commit 76eebfa
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/lib/types/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ export interface MessageCommandErrorPayload extends MessageCommandRunPayload {

export interface MessageCommandSuccessPayload extends MessageCommandRunPayload {
result: unknown;
duration: number;
}

export interface MessageCommandTypingErrorPayload extends MessageCommandRunPayload {}
Expand Down Expand Up @@ -252,6 +253,7 @@ export interface ChatInputCommandFinishPayload extends ChatInputCommandAcceptedP

export interface ChatInputCommandSuccessPayload extends ChatInputCommandRunPayload {
result: unknown;
duration: number;
}

export interface ChatInputCommandErrorPayload extends IChatInputCommandPayload {
Expand Down Expand Up @@ -292,6 +294,7 @@ export interface ContextMenuCommandFinishPayload extends ContextMenuCommandAccep

export interface ContextMenuCommandSuccessPayload extends ContextMenuCommandRunPayload {
result: unknown;
duration: number;
}

export interface ContextMenuCommandErrorPayload extends IContextMenuCommandPayload {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ export class CoreListener extends Listener<typeof Events.ChatInputCommandAccepte
public async run(payload: ChatInputCommandAcceptedPayload) {
const { command, context, interaction } = payload;

const stopwatch = new Stopwatch();
const result = await fromAsync(async () => {
this.container.client.emit(Events.ChatInputCommandRun, interaction, command, { ...payload });
const stopwatch = new Stopwatch();
const result = await command.chatInputRun(interaction, context);
this.container.client.emit(Events.ChatInputCommandSuccess, { ...payload, result });
const { duration } = stopwatch.stop();
this.container.client.emit(Events.ChatInputCommandSuccess, { ...payload, result, duration });

return { result, duration };
});

const { duration } = stopwatch.stop();
const { duration } = result.value ?? { duration: -1 };

if (isErr(result)) {
this.container.client.emit(Events.ChatInputCommandError, result.error, { ...payload, duration });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ export class CoreListener extends Listener<typeof Events.ContextMenuCommandAccep
public async run(payload: ContextMenuCommandAcceptedPayload) {
const { command, context, interaction } = payload;

const stopwatch = new Stopwatch();
const result = await fromAsync(async () => {
this.container.client.emit(Events.ContextMenuCommandRun, interaction, command, { ...payload });
const stopwatch = new Stopwatch();
const result = await command.contextMenuRun(interaction, context);
this.container.client.emit(Events.ContextMenuCommandSuccess, { ...payload, result });
const { duration } = stopwatch.stop();

this.container.client.emit(Events.ContextMenuCommandSuccess, { ...payload, result, duration });

return { result, duration };
});

const { duration } = stopwatch.stop();
const { duration } = result.value ?? { duration: -1 };

if (isErr(result)) {
this.container.client.emit(Events.ContextMenuCommandError, result.error, { ...payload, duration });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ export class CoreListener extends Listener<typeof Events.MessageCommandAccepted>
const { message, command, parameters, context } = payload;
const args = await command.messagePreParse(message, parameters, context);

const stopwatch = new Stopwatch();
const result = await fromAsync(async () => {
message.client.emit(Events.MessageCommandRun, message, command, { ...payload, args });
const stopwatch = new Stopwatch();
const result = await command.messageRun(message, args, context);
message.client.emit(Events.MessageCommandSuccess, { ...payload, args, result });
const { duration } = stopwatch.stop();
message.client.emit(Events.MessageCommandSuccess, { ...payload, args, result, duration });

return { result, duration };
});

const { duration } = stopwatch.stop();
const { duration } = result.value ?? { duration: -1 };

if (isErr(result)) {
message.client.emit(Events.MessageCommandError, result.error, { ...payload, args, duration });
}
Expand Down

0 comments on commit 76eebfa

Please sign in to comment.