From 9221e88c86b42c3b5a6f6aacfa5e34e060724384 Mon Sep 17 00:00:00 2001 From: Manu Date: Mon, 4 Sep 2023 17:03:26 +0200 Subject: [PATCH] fix: fire event 'ready' on modules (#50) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Luca Montaigut Co-authored-by: Peïo Thibault --- src/core/checkUniqueSlashCommandNames.ts | 8 +++----- src/core/loadModules.ts | 3 +-- src/main.ts | 1 + 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/core/checkUniqueSlashCommandNames.ts b/src/core/checkUniqueSlashCommandNames.ts index 189647a..776927c 100644 --- a/src/core/checkUniqueSlashCommandNames.ts +++ b/src/core/checkUniqueSlashCommandNames.ts @@ -1,9 +1,7 @@ -import type { BotModule } from '../types/bot'; +import type { BotCommand } from '../types/bot'; -export const checkUniqueSlashCommandNames = (modulesToLoad: Record) => { - const slashCommandNames = Object.values(modulesToLoad) - .flatMap((module) => module.slashCommands ?? []) - .map((command) => command.schema.name); +export const checkUniqueSlashCommandNames = (botCommands: BotCommand[]) => { + const slashCommandNames = botCommands.map((command) => command.schema.name); const uniqueSlashCommandNames = new Set(slashCommandNames); if (uniqueSlashCommandNames.size !== slashCommandNames.length) { throw new Error('Found duplicate slash command names'); diff --git a/src/core/loadModules.ts b/src/core/loadModules.ts index 0100965..03d1a77 100644 --- a/src/core/loadModules.ts +++ b/src/core/loadModules.ts @@ -10,8 +10,7 @@ export const loadModules = async ( modulesToLoad: Record, ): Promise => { const botCommands = Object.values(modulesToLoad).flatMap((module) => module.slashCommands ?? []); - - checkUniqueSlashCommandNames(modulesToLoad); + checkUniqueSlashCommandNames(botCommands); routeCommands(client, botCommands); await pushCommands(botCommands.map((command) => command.schema)); diff --git a/src/main.ts b/src/main.ts index a9eb77d..c0a4e63 100644 --- a/src/main.ts +++ b/src/main.ts @@ -12,6 +12,7 @@ const client = new Client({ await client.login(discord.token); await new Promise((resolve) => { client.on('ready', () => { + Object.values(modules).map((module) => module.eventHandlers?.ready?.(client)); resolve(); }); });