Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(command-duration): add duration to *commandSuccess payloads #359

Merged
merged 3 commits into from
Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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