From f7c7e3c6a4932e1081db4746d0df60024c195f63 Mon Sep 17 00:00:00 2001 From: xhyrom Date: Sat, 29 Jan 2022 13:58:14 +0100 Subject: [PATCH] feat: events --- src/handlers/AutocompleteHandler.ts | 4 +++- src/handlers/ComponentHandler.ts | 4 +++- src/handlers/InteractionCommandHandler.ts | 4 +++- src/handlers/MessageCommandHandler.ts | 4 +++- src/lib/structures/Logger.ts | 23 +++++++++++++++++++++++ 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/handlers/AutocompleteHandler.ts b/src/handlers/AutocompleteHandler.ts index e98ded421..27ef5d649 100644 --- a/src/handlers/AutocompleteHandler.ts +++ b/src/handlers/AutocompleteHandler.ts @@ -2,7 +2,7 @@ import type { AutocompleteInteraction } from 'discord.js'; import { AutocompleteContext } from '../lib/structures/contexts/AutocompleteContext'; import type { Argument } from '../lib/structures/Argument'; import { Commands } from '../lib/managers/CommandManager'; -import { Logger } from '../lib/structures/Logger'; +import { Logger, LoggerEvents } from '../lib/structures/Logger'; import type { GClient } from '../lib/GClient'; export async function AutocompleteHandler(interaction: AutocompleteInteraction) { @@ -40,10 +40,12 @@ export async function AutocompleteHandler(interaction: AutocompleteInteraction) await Promise.resolve(argument.run(ctx)) .catch((error) => { + Logger.emit(LoggerEvents.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.debug( `Successfully ran autocomplete (${argument.name} -> ${command.name}) for ${interaction.user.username}`, ); diff --git a/src/handlers/ComponentHandler.ts b/src/handlers/ComponentHandler.ts index 3671cc348..81ad57765 100644 --- a/src/handlers/ComponentHandler.ts +++ b/src/handlers/ComponentHandler.ts @@ -5,7 +5,7 @@ import { ComponentContext } from '../lib/structures/contexts/ComponentContext'; import { Components } from '../lib/managers/ComponentManager'; import { Handlers } from '../lib/managers/HandlerManager'; import { setTimeout } from 'node:timers'; -import { Logger } from '../lib/structures/Logger'; +import { Logger, LoggerEvents } from '../lib/structures/Logger'; const cooldowns = new Collection>(); @@ -71,6 +71,7 @@ export async function ComponentHandler(interaction: MessageComponentInteraction) await Promise.resolve(component.run(ctx)) .catch(async (error) => { + Logger.emit(LoggerEvents.HANDLER_ERROR, ctx, error); Logger.error(typeof error.code !== 'undefined' ? error.code : '', error.message); if (error.stack) Logger.trace(error.stack); const errorReply = () => @@ -84,6 +85,7 @@ export async function ComponentHandler(interaction: MessageComponentInteraction) else await errorReply(); }) .then(() => { + Logger.emit(LoggerEvents.HANDLER_RUN, ctx); if (autoDeferTimeout) clearTimeout(autoDeferTimeout); Logger.debug(`Successfully ran component (${component.name}) for ${interaction.user.username}`); }); diff --git a/src/handlers/InteractionCommandHandler.ts b/src/handlers/InteractionCommandHandler.ts index 834e68ac9..e89a11b3f 100644 --- a/src/handlers/InteractionCommandHandler.ts +++ b/src/handlers/InteractionCommandHandler.ts @@ -4,7 +4,7 @@ import { CommandContext } from '../lib/structures/contexts/CommandContext'; import { Handlers } from '../lib/managers/HandlerManager'; import { Commands } from '../lib/managers/CommandManager'; import { setTimeout } from 'node:timers'; -import { Logger } from '../lib/structures/Logger'; +import { Logger, LoggerEvents } from '../lib/structures/Logger'; const cooldowns = new Collection>(); @@ -62,6 +62,7 @@ export async function InteractionCommandHandler(interaction: CommandInteraction await Promise.resolve(command.run(ctx)) .catch(async (error) => { + Logger.emit(LoggerEvents.HANDLER_ERROR, ctx, error); Logger.error(typeof error.code !== 'undefined' ? error.code : '', error.message); if (error.stack) Logger.trace(error.stack); const errorReply = () => @@ -76,6 +77,7 @@ export async function InteractionCommandHandler(interaction: CommandInteraction else await errorReply(); }) .then(() => { + Logger.emit(LoggerEvents.HANDLER_RUN, ctx); if (autoDeferTimeout) clearTimeout(autoDeferTimeout); Logger.debug(`Successfully ran command (${command.name}) for ${interaction.user.username}`); }); diff --git a/src/handlers/MessageCommandHandler.ts b/src/handlers/MessageCommandHandler.ts index 8db324c75..4d7aec3e1 100644 --- a/src/handlers/MessageCommandHandler.ts +++ b/src/handlers/MessageCommandHandler.ts @@ -5,7 +5,7 @@ import { CommandType } from '../lib/structures/Command'; import { ArgumentType } from '../lib/structures/Argument'; import { Commands } from '../lib/managers/CommandManager'; import { Handlers } from '../lib/managers/HandlerManager'; -import { Logger } from '../lib/structures/Logger'; +import { Logger, LoggerEvents } from '../lib/structures/Logger'; const cooldowns = new Collection>(); @@ -92,6 +92,7 @@ export async function MessageCommandHandler( if (!(await command.inhibit(ctx))) return; await Promise.resolve(command.run(ctx)) .catch(async (error) => { + Logger.emit(LoggerEvents.HANDLER_ERROR, ctx, error); Logger.error(typeof error.code !== 'undefined' ? error.code : '', error.message); if (error.stack) Logger.trace(error.stack); const errorReply = () => @@ -104,6 +105,7 @@ export async function MessageCommandHandler( else await errorReply(); }) .then(() => { + Logger.emit(LoggerEvents.HANDLER_RUN, ctx); Logger.debug(`Successfully ran command (${command.name}) for ${message.author.username}`); }); } diff --git a/src/lib/structures/Logger.ts b/src/lib/structures/Logger.ts index 26d5c8bbb..54dc1387b 100644 --- a/src/lib/structures/Logger.ts +++ b/src/lib/structures/Logger.ts @@ -3,6 +3,29 @@ import EventEmitter from 'events'; import JSLogger, { ILogger, ILoggerOpts, ILogHandler, ILogLevel } from 'js-logger'; import type { GlobalLogger } from 'js-logger'; +import type { AutocompleteContext } from './contexts/AutocompleteContext'; +import type { CommandContext } from './contexts/CommandContext'; +import type { ComponentContext } from './contexts/ComponentContext'; + +export enum LoggerEvents { + 'HANDLER_RUN' = 'handlerRun', + 'HANDLER_ERROR' = 'handlerError', +} + +export interface LoggerEventsInterface { + 'handlerRun': (ctx: AutocompleteContext | CommandContext | ComponentContext) => void; + 'handlerError': (ctx: AutocompleteContext | CommandContext | ComponentContext, error: any) => void; +} + +export declare interface LoggerClass { + on( + event: U, listener: LoggerEventsInterface[U] + ): this; + + emit( + event: U, ...args: Parameters + ): boolean; +} export class LoggerClass extends EventEmitter implements GlobalLogger { TRACE: ILogLevel;