Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
fix: refactored registering of event listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusbegby committed Aug 27, 2023
1 parent 9b02b21 commit 9757252
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const logger = loggerModule.child({

client.on('allShardsReady', async () => {
client.registerClientCommands = registerClientCommands;
registerEventListeners({ client, player, executionId });
registerClientCommands({ client, executionId });
await registerEventListeners({ client, player, executionId });
await registerClientCommands({ client, executionId });
client.emit('ready', client as Client);
});

Expand Down
9 changes: 9 additions & 0 deletions src/types/utilTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ export interface RegisterClientCommandsParams {
executionId: string;
}

export interface CustomEvent {
name: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
execute: (...args: any) => void;
once?: boolean;
isDebug?: boolean;
isPlayerEvent?: boolean;
}

export interface NotInVoiceChannelParams {
interaction: ChatInputCommandInteraction;
executionId: string;
Expand Down
67 changes: 40 additions & 27 deletions src/utils/registerEventListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,46 @@ import config from 'config';
import fs from 'node:fs';
import path from 'node:path';

import { GuildQueueEvents, Player, PlayerEvents } from 'discord-player';
import { Client } from 'discord.js';
import loggerModule from '../services/logger';
import { LoggerOptions } from '../types/configTypes';
import {
ClientEventArguments, PlayerEventArguments, ProcessEventArguments
} from '../types/eventTypes';
import { RegisterEventListenersParams } from '../types/utilTypes';
import { ClientEventArguments, PlayerEventArguments, ProcessEventArguments } from '../types/eventTypes';
import { CustomEvent, RegisterEventListenersParams } from '../types/utilTypes';

const loggerOptions: LoggerOptions = config.get('loggerOptions');

export const registerEventListeners = ({ client, player, executionId }: RegisterEventListenersParams) => {
const registerClientEventListeners = (client: Client, event: CustomEvent) => {
if (event.once) {
client.once(event.name, (...args: ClientEventArguments) => event.execute(...args));
} else {
if (!event.isDebug || process.env.MINIMUM_LOG_LEVEL === 'debug') {
client.on(event.name, (...args: ClientEventArguments) => event.execute(...args));
}
}
};

const registerInteractionEventListeners = (client: Client, event: CustomEvent) => {
client.on(event.name, (...args: ClientEventArguments) => event.execute(...args, { client }));
};

const registerProcessEventListeners = (event: CustomEvent) => {
process.on(event.name, (...args: ProcessEventArguments) => event.execute(...args));
};

const registerPlayerEventListeners = (player: Player, event: CustomEvent, loggerOptions: LoggerOptions) => {
if (!event.isDebug || (loggerOptions.minimumLogLevel === 'debug' && loggerOptions.discordPlayerDebug)) {
if (event.isPlayerEvent) {
player.events.on(event.name as keyof GuildQueueEvents, (...args: PlayerEventArguments) =>
event.execute(...args)
);
} else {
player.on(event.name as keyof PlayerEvents, (...args: PlayerEventArguments) => event.execute(...args));
}
}
};

export const registerEventListeners = async ({ client, player, executionId }: RegisterEventListenersParams) => {
const logger = loggerModule.child({
source: 'registerEventListeners.js',
module: 'register',
Expand All @@ -31,39 +61,22 @@ export const registerEventListeners = ({ client, player, executionId }: Register
.filter((file) => file.endsWith('.js'));

for (const file of eventFiles) {
/* eslint-disable @typescript-eslint/no-var-requires */
const event = require(`../events/${folder}/${file}`);
const event = await import(`../events/${folder}/${file}`);
switch (folder) {
case 'client':
if (event.once) {
client.once(event.name, (...args: ClientEventArguments) => event.execute(...args));
} else {
if (!event.isDebug || process.env.MINIMUM_LOG_LEVEL === 'debug') {
client.on(event.name, (...args: ClientEventArguments) => event.execute(...args));
}
}
registerClientEventListeners(client, event);
break;

case 'interactions':
client.on(event.name, (...args: ClientEventArguments) => event.execute(...args, { client }));
registerInteractionEventListeners(client, event);
break;

case 'process':
process.on(event.name, (...args: ProcessEventArguments) => event.execute(...args));
registerProcessEventListeners(event);
break;

case 'player':
if (
!event.isDebug ||
(loggerOptions.minimumLogLevel === 'debug' && loggerOptions.discordPlayerDebug)
) {
if (event.isPlayerEvent) {
player.events.on(event.name, (...args: PlayerEventArguments) => event.execute(...args));
break;
} else {
player.on(event.name, (...args: PlayerEventArguments) => event.execute(...args));
}
}
registerPlayerEventListeners(player, event, loggerOptions);
break;

default:
Expand Down

0 comments on commit 9757252

Please sign in to comment.