Skip to content

Commit

Permalink
feat: new events
Browse files Browse the repository at this point in the history
  • Loading branch information
xhyrom committed Feb 2, 2022
1 parent 555d054 commit 8153582
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/handlers/AutocompleteHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ export async function AutocompleteHandler(interaction: AutocompleteInteraction)
await Promise.resolve(argument.run(ctx))
.catch((error) => {
Logger.emit(LoggerEvents.HANDLER_ERROR, ctx, error);
Logger.emit(LoggerEvents.AUTOCOMPLETE_HANDLER_ERROR, ctx, error);
Logger.error(typeof error.code !== 'undefined' ? error.code : '', error.message);
if (error.stack) Logger.trace(error.stack);
})
.then(() => {
Logger.emit(LoggerEvents.HANDLER_RUN, ctx);
Logger.emit(LoggerEvents.AUTOCOMPLETE_HANDLER_RUN, ctx);
Logger.debug(
`Successfully ran autocomplete (${argument.name} -> ${command.name}) for ${interaction.user.username}`,
);
Expand Down
2 changes: 2 additions & 0 deletions src/handlers/ComponentHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export async function ComponentHandler(interaction: MessageComponentInteraction)
await Promise.resolve(component.run(ctx))
.catch(async (error) => {
Logger.emit(LoggerEvents.HANDLER_ERROR, ctx, error);
Logger.emit(LoggerEvents.COMPONENT_HANDLER_ERROR, ctx, error);
Logger.error(typeof error.code !== 'undefined' ? error.code : '', error.message);
if (error.stack) Logger.trace(error.stack);
const errorReply = () =>
Expand All @@ -86,6 +87,7 @@ export async function ComponentHandler(interaction: MessageComponentInteraction)
})
.then(() => {
Logger.emit(LoggerEvents.HANDLER_RUN, ctx);
Logger.emit(LoggerEvents.COMPONENT_HANDLER_RUN, ctx);
if (autoDeferTimeout) clearTimeout(autoDeferTimeout);
Logger.debug(`Successfully ran component (${component.name}) for ${interaction.user.username}`);
});
Expand Down
2 changes: 2 additions & 0 deletions src/handlers/InteractionCommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export async function InteractionCommandHandler(interaction: CommandInteraction
await Promise.resolve(command.run(ctx))
.catch(async (error) => {
Logger.emit(LoggerEvents.HANDLER_ERROR, ctx, error);
Logger.emit(LoggerEvents.COMMAND_HANDLER_ERROR, ctx, error);
Logger.error(typeof error.code !== 'undefined' ? error.code : '', error.message);
if (error.stack) Logger.trace(error.stack);
const errorReply = () =>
Expand All @@ -78,6 +79,7 @@ export async function InteractionCommandHandler(interaction: CommandInteraction
})
.then(() => {
Logger.emit(LoggerEvents.HANDLER_RUN, ctx);
Logger.emit(LoggerEvents.COMMAND_HANDLER_RUN, ctx);
if (autoDeferTimeout) clearTimeout(autoDeferTimeout);
Logger.debug(`Successfully ran command (${command.name}) for ${interaction.user.username}`);
});
Expand Down
2 changes: 2 additions & 0 deletions src/handlers/MessageCommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export async function MessageCommandHandler(
await Promise.resolve(command.run(ctx))
.catch(async (error) => {
Logger.emit(LoggerEvents.HANDLER_ERROR, ctx, error);
Logger.emit(LoggerEvents.COMMAND_HANDLER_ERROR, ctx, error);
Logger.error(typeof error.code !== 'undefined' ? error.code : '', error.message);
if (error.stack) Logger.trace(error.stack);
const errorReply = () =>
Expand All @@ -106,6 +107,7 @@ export async function MessageCommandHandler(
})
.then(() => {
Logger.emit(LoggerEvents.HANDLER_RUN, ctx);
Logger.emit(LoggerEvents.COMMAND_HANDLER_RUN, ctx);
Logger.debug(`Successfully ran command (${command.name}) for ${message.author.username}`);
});
}
12 changes: 12 additions & 0 deletions src/lib/util/logger/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import type { Plugin } from '../../structures/Plugin';
export enum LoggerEvents {
'HANDLER_RUN' = 'handlerRun',
'HANDLER_ERROR' = 'handlerError',
'COMMAND_HANDLER_RUN' = 'commandHandlerRun',
'COMMAND_HANDLER_ERROR' = 'commandHandlerError',
'AUTOCOMPLETE_HANDLER_RUN' = 'autoCompleteHandlerRun',
'AUTOCOMPLETE_HANDLER_ERROR' = 'autoCompleteHandlerError',
'COMPONENT_HANDLER_RUN' = 'componentHandlerRun',
'COMPONENT_HANDLER_ERROR' = 'componentHandlerError',
'COMMAND_REGISTERED' = 'commandRegistered',
'COMMAND_UNREGISTERED' = 'commandUnregistered',
'COMPONENT_REGISTERED' = 'componentRegistered',
Expand All @@ -27,6 +33,12 @@ export enum LoggerEvents {
export interface LoggerEventsInterface {
'handlerRun': (ctx: AutocompleteContext | CommandContext | ComponentContext) => void;
'handlerError': (ctx: AutocompleteContext | CommandContext | ComponentContext, error: any) => void;
'commandHandlerRun': (ctx: CommandContext) => void;
'commandHandlerError': (ctx: CommandContext, error: any) => void;
'autoCompleteHandlerRun': (ctx: AutocompleteContext) => void;
'autoCompleteHandlerError': (ctx: AutocompleteContext, error: any) => void;
'componentHandlerRun': (ctx: ComponentContext) => void;
'componentHandlerError': (ctx: ComponentContext, error: any) => void;
'commandRegistered': (command: Command) => void;
'commandUnregistered': (command: Command) => void;
'componentRegistered': (component: Component) => void;
Expand Down

0 comments on commit 8153582

Please sign in to comment.