Skip to content

Commit

Permalink
feat: events
Browse files Browse the repository at this point in the history
  • Loading branch information
xhyrom committed Jan 29, 2022
1 parent 65e0409 commit f7c7e3c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/handlers/AutocompleteHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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}`,
);
Expand Down
4 changes: 3 additions & 1 deletion src/handlers/ComponentHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Collection<string, number>>();

Expand Down Expand Up @@ -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 = () =>
Expand All @@ -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}`);
});
Expand Down
4 changes: 3 additions & 1 deletion src/handlers/InteractionCommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Collection<string, number>>();

Expand Down Expand Up @@ -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 = () =>
Expand All @@ -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}`);
});
Expand Down
4 changes: 3 additions & 1 deletion src/handlers/MessageCommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Collection<string, number>>();

Expand Down Expand Up @@ -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 = () =>
Expand All @@ -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}`);
});
}
23 changes: 23 additions & 0 deletions src/lib/structures/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<U extends keyof LoggerEventsInterface>(
event: U, listener: LoggerEventsInterface[U]
): this;

emit<U extends keyof LoggerEventsInterface>(
event: U, ...args: Parameters<LoggerEventsInterface[U]>
): boolean;
}

export class LoggerClass extends EventEmitter implements GlobalLogger {
TRACE: ILogLevel;
Expand Down

0 comments on commit f7c7e3c

Please sign in to comment.