Skip to content

Commit

Permalink
feat: add duration to *Error and *Finished Message, ChatInput
Browse files Browse the repository at this point in the history
… and `ContextMenu` Command events (#357)

BREAKING CHANGE: The payload for `Events.ChatInputCommandFinish` has been changed from `ChatInputCommandRunPayload` to `ChatInputCommandFinishPayload`
BREAKING CHANGE: The payload for `Events.ContextMenuCommandFinish` has been changed from `ContextMenuCommandRunPayload` to `ContextMenuCommandFinishPayload`
  • Loading branch information
feralheart authored Jan 10, 2022
1 parent 15f4e13 commit 506fd58
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@sapphire/discord.js-utilities": "^4.1.5",
"@sapphire/pieces": "^3.2.0",
"@sapphire/ratelimits": "^2.1.10",
"@sapphire/stopwatch": "^1.2.0",
"@sapphire/utilities": "^3.1.0",
"lexure": "^0.17.0",
"tslib": "^2.3.1"
Expand Down
32 changes: 26 additions & 6 deletions src/lib/types/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,13 @@ export interface MessageCommandRunPayload extends MessageCommandAcceptedPayload
args: unknown;
}

export interface MessageCommandFinishPayload extends MessageCommandRunPayload {}
export interface MessageCommandFinishPayload extends MessageCommandRunPayload {
duration: number;
}

export interface MessageCommandErrorPayload extends MessageCommandRunPayload {}
export interface MessageCommandErrorPayload extends MessageCommandRunPayload {
duration: number;
}

export interface MessageCommandSuccessPayload extends MessageCommandRunPayload {
result: unknown;
Expand Down Expand Up @@ -242,11 +246,17 @@ export interface ChatInputCommandAcceptedPayload extends PreChatInputCommandRunP

export interface ChatInputCommandRunPayload extends ChatInputCommandAcceptedPayload {}

export interface ChatInputCommandFinishPayload extends ChatInputCommandAcceptedPayload {
duration: number;
}

export interface ChatInputCommandSuccessPayload extends ChatInputCommandRunPayload {
result: unknown;
}

export interface ChatInputCommandErrorPayload extends IChatInputCommandPayload {}
export interface ChatInputCommandErrorPayload extends IChatInputCommandPayload {
duration: number;
}

export interface UnknownContextMenuCommandPayload {
interaction: ContextMenuInteraction;
Expand Down Expand Up @@ -276,11 +286,17 @@ export interface ContextMenuCommandAcceptedPayload extends PreContextMenuCommand

export interface ContextMenuCommandRunPayload extends ContextMenuCommandAcceptedPayload {}

export interface ContextMenuCommandFinishPayload extends ContextMenuCommandAcceptedPayload {
duration: number;
}

export interface ContextMenuCommandSuccessPayload extends ContextMenuCommandRunPayload {
result: unknown;
}

export interface ContextMenuCommandErrorPayload extends IContextMenuCommandPayload {}
export interface ContextMenuCommandErrorPayload extends IContextMenuCommandPayload {
duration: number;
}

export interface IInteractionHandlerPayload {
interaction: Interaction;
Expand Down Expand Up @@ -347,7 +363,7 @@ declare module 'discord.js' {
[Events.ChatInputCommandRun]: [interaction: CommandInteraction, command: ChatInputCommand, payload: ChatInputCommandRunPayload];
[Events.ChatInputCommandSuccess]: [payload: ChatInputCommandSuccessPayload];
[Events.ChatInputCommandError]: [error: unknown, payload: ChatInputCommandErrorPayload];
[Events.ChatInputCommandFinish]: [interaction: CommandInteraction, command: ChatInputCommand, payload: ChatInputCommandRunPayload];
[Events.ChatInputCommandFinish]: [interaction: CommandInteraction, command: ChatInputCommand, payload: ChatInputCommandFinishPayload];

// Context menu command chain
[Events.PossibleContextMenuCommand]: [interaction: ContextMenuInteraction];
Expand All @@ -361,7 +377,11 @@ declare module 'discord.js' {
[Events.ContextMenuCommandRun]: [interaction: ContextMenuInteraction, command: ContextMenuCommand, payload: ContextMenuCommandRunPayload];
[Events.ContextMenuCommandSuccess]: [payload: ContextMenuCommandSuccessPayload];
[Events.ContextMenuCommandError]: [error: unknown, payload: ContextMenuCommandErrorPayload];
[Events.ContextMenuCommandFinish]: [interaction: ContextMenuInteraction, command: ContextMenuCommand, payload: ContextMenuCommandRunPayload];
[Events.ContextMenuCommandFinish]: [
interaction: ContextMenuInteraction,
command: ContextMenuCommand,
payload: ContextMenuCommandFinishPayload
];

// #endregion Sapphire load cycle events

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { PieceContext } from '@sapphire/pieces';
import { Stopwatch } from '@sapphire/stopwatch';
import { fromAsync, isErr } from '../../../lib/parsers/Result';
import { Listener } from '../../../lib/structures/Listener';
import { ChatInputCommandAcceptedPayload, Events } from '../../../lib/types/Events';
Expand All @@ -11,16 +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 result = await command.chatInputRun(interaction, context);
this.container.client.emit(Events.ChatInputCommandSuccess, { ...payload, result });
});

const { duration } = stopwatch.stop();
if (isErr(result)) {
this.container.client.emit(Events.ChatInputCommandError, result.error, { ...payload });
this.container.client.emit(Events.ChatInputCommandError, result.error, { ...payload, duration });
}

this.container.client.emit(Events.ChatInputCommandFinish, interaction, command, { ...payload });
this.container.client.emit(Events.ChatInputCommandFinish, interaction, command, { ...payload, duration });
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { PieceContext } from '@sapphire/pieces';
import { Stopwatch } from '@sapphire/stopwatch';
import { fromAsync, isErr } from '../../../lib/parsers/Result';
import { Listener } from '../../../lib/structures/Listener';
import { ContextMenuCommandAcceptedPayload, Events } from '../../../lib/types/Events';
Expand All @@ -11,16 +12,18 @@ 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 result = await command.contextMenuRun(interaction, context);
this.container.client.emit(Events.ContextMenuCommandSuccess, { ...payload, result });
});

const { duration } = stopwatch.stop();
if (isErr(result)) {
this.container.client.emit(Events.ContextMenuCommandError, result.error, { ...payload });
this.container.client.emit(Events.ContextMenuCommandError, result.error, { ...payload, duration });
}

this.container.client.emit(Events.ContextMenuCommandFinish, interaction, command, { ...payload });
this.container.client.emit(Events.ContextMenuCommandFinish, interaction, command, { ...payload, duration });
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { PieceContext } from '@sapphire/pieces';
import { Stopwatch } from '@sapphire/stopwatch';
import { fromAsync, isErr } from '../../lib/parsers/Result';
import { Listener } from '../../lib/structures/Listener';
import { MessageCommandAcceptedPayload, Events } from '../../lib/types/Events';
import { isErr, fromAsync } from '../../lib/parsers/Result';
import { Events, MessageCommandAcceptedPayload } from '../../lib/types/Events';

export class CoreListener extends Listener<typeof Events.MessageCommandAccepted> {
public constructor(context: PieceContext) {
Expand All @@ -11,16 +12,19 @@ export class CoreListener extends Listener<typeof Events.MessageCommandAccepted>
public async run(payload: MessageCommandAcceptedPayload) {
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 result = await command.messageRun(message, args, context);
message.client.emit(Events.MessageCommandSuccess, { ...payload, args, result });
});

const { duration } = stopwatch.stop();
if (isErr(result)) {
message.client.emit(Events.MessageCommandError, result.error, { ...payload, args });
message.client.emit(Events.MessageCommandError, result.error, { ...payload, args, duration });
}

message.client.emit(Events.MessageCommandFinish, message, command, { ...payload, args });
message.client.emit(Events.MessageCommandFinish, message, command, { ...payload, args, duration });
}
}
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,7 @@ __metadata:
"@sapphire/pieces": ^3.2.0
"@sapphire/prettier-config": ^1.2.7
"@sapphire/ratelimits": ^2.1.10
"@sapphire/stopwatch": ^1.2.0
"@sapphire/ts-config": ^3.1.6
"@sapphire/utilities": ^3.1.0
"@types/jest": ^27.4.0
Expand Down Expand Up @@ -1103,6 +1104,15 @@ __metadata:
languageName: node
linkType: hard

"@sapphire/stopwatch@npm:^1.2.0":
version: 1.2.4
resolution: "@sapphire/stopwatch@npm:1.2.4"
dependencies:
tslib: ^2.3.1
checksum: 0841a5a728a06c556e85470d1d5bb57646829b266ef8ab3c46ac998665edad1dba4eca0aaf689716f91ed4abca58882a93cde2ca354ca51f9c05b568d9be1308
languageName: node
linkType: hard

"@sapphire/time-utilities@npm:^1.5.0":
version: 1.5.0
resolution: "@sapphire/time-utilities@npm:1.5.0"
Expand Down

0 comments on commit 506fd58

Please sign in to comment.