From de46344ccfd5ee8d982de05b74c492f22c07b3f4 Mon Sep 17 00:00:00 2001 From: Slincnik Date: Wed, 8 Jan 2025 13:13:18 +0300 Subject: [PATCH] fix: fixing handlers --- src/handlers/command-handler/index.js | 68 ++++++++++++------------- src/handlers/event-handler/index.js | 71 ++++++++++++++------------- src/handlers/index.js | 18 +++++++ src/structures/client.js | 8 ++- 4 files changed, 93 insertions(+), 72 deletions(-) create mode 100644 src/handlers/index.js diff --git a/src/handlers/command-handler/index.js b/src/handlers/command-handler/index.js index 53b6271f..95401739 100644 --- a/src/handlers/command-handler/index.js +++ b/src/handlers/command-handler/index.js @@ -1,40 +1,42 @@ import { resolve } from "node:path"; import logger from "../../helpers/logger.js"; -import useClient from "../../utils/use-client.js"; import { getFilePaths } from "../../utils/index.js"; import { toFileURL } from "../../utils/resolve-file.js"; import registerCommands from "./functions/registerCommands.js"; -export const commands = []; - -export const init = async () => { - const client = useClient(); - await buildCommands(); - - await registerCommands({ - client, - commands, - }); -}; - -const buildCommands = async () => { - const client = useClient(); - const cmdPath = resolve(client.configService.get("paths.commands")); - const commandFilePaths = (await getFilePaths(cmdPath, true)).filter(path => path.endsWith(".js")); - - for (const cmdFilePath of commandFilePaths) { - const { data, run } = await import(toFileURL(cmdFilePath)); - - if (!data || !data.name) { - logger.warn(`Command ${cmdFilePath} does not have a data object or name`); - continue; - } - - if (typeof run !== "function") { - logger.warn(`Command ${cmdFilePath} does not have a run function or it is not a function`); - continue; - } - - commands.push({ data, run }); +export class CommandHandler { + constructor(client) { + this.client = client; + this.commands = []; } -}; + + async init() { + await this.#buildCommands(); + + await registerCommands({ + client: this.client, + commands: this.commands, + }); + } + + async #buildCommands() { + const cmdPath = resolve(this.client.configService.get("paths.commands")); + const commandFilePaths = (await getFilePaths(cmdPath, true)).filter(path => path.endsWith(".js")); + + for (const cmdFilePath of commandFilePaths) { + const { data, run } = await import(toFileURL(cmdFilePath)); + + if (!data || !data.name) { + logger.warn(`Command ${cmdFilePath} does not have a data object or name`); + continue; + } + + if (typeof run !== "function") { + logger.warn(`Command ${cmdFilePath} does not have a run function or it is not a function`); + continue; + } + + this.commands.push({ data, run }); + } + } +} diff --git a/src/handlers/event-handler/index.js b/src/handlers/event-handler/index.js index 0144bbba..9bb3b915 100644 --- a/src/handlers/event-handler/index.js +++ b/src/handlers/event-handler/index.js @@ -1,49 +1,52 @@ import { resolve } from "node:path"; import logger from "../../helpers/logger.js"; -import useClient from "../../utils/use-client.js"; import { getFilePaths } from "../../utils/index.js"; import { toFileURL } from "../../utils/resolve-file.js"; import { useMainPlayer } from "discord-player"; -export const events = []; +export class EventHandler { + constructor(client) { + this.events = []; + this.client = client; + } -export const init = async () => { - await buildEvents(); - registerEvents(); -}; + async init() { + await this.#buildEvents(); + this.$registerEvents(); + } -const buildEvents = async () => { - const client = useClient(); - try { - const eventPath = resolve(client.configService.get("paths.events")); - const eventFilePaths = (await getFilePaths(eventPath, true)).filter(path => path.endsWith(".js")); + async #buildEvents() { + try { + const eventPath = resolve(this.client.configService.get("paths.events")); + const eventFilePaths = (await getFilePaths(eventPath, true)).filter(path => path.endsWith(".js")); - for (const eventFilePath of eventFilePaths) { - const { data, run } = await import(toFileURL(eventFilePath)); + for (const eventFilePath of eventFilePaths) { + const { data, run } = await import(toFileURL(eventFilePath)); - if (!data || !data.name) { - logger.warn(`Event ${eventFilePath} does not have a data object or name`); - continue; + if (!data || !data.name) { + logger.warn(`Event ${eventFilePath} does not have a data object or name`); + continue; + } + + if (typeof run !== "function") { + logger.warn(`Event ${eventFilePath} does not have a run function or it is not a function`); + continue; + } + + this.events.push({ data, run }); } - - if (typeof run !== "function") { - logger.warn(`Event ${eventFilePath} does not have a run function or it is not a function`); - continue; - } - - events.push({ data, run }); + } catch (error) { + logger.error("Error build events: ", error); } - } catch (error) { - logger.error("Error build events: ", error); + logger.log("Events loaded"); } -}; -const registerEvents = async () => { - const client = useClient(); - const player = useMainPlayer(); - for (const { data, run } of events) { - if (data.player) player.events.on(data.name, run); - if (data.once) client.once(data.name, run); - else client.on(data.name, run); + $registerEvents() { + const player = useMainPlayer(); + for (const { data, run } of this.events) { + if (data.player) player.events.on(data.name, run); + if (data.once) this.client.once(data.name, run); + else this.client.on(data.name, run); + } } -}; +} diff --git a/src/handlers/index.js b/src/handlers/index.js new file mode 100644 index 00000000..7d0d4bcd --- /dev/null +++ b/src/handlers/index.js @@ -0,0 +1,18 @@ +import { CommandHandler } from "./command-handler/index.js"; +import { EventHandler } from "./event-handler/index.js"; + +export class Handlers { + constructor(client) { + this.client = client; + + this.#init(); + } + + async #init() { + const eventHandler = new EventHandler(this.client); + await eventHandler.init(); + + const commandHandler = new CommandHandler(this.client); + await commandHandler.init(); + } +} diff --git a/src/structures/client.js b/src/structures/client.js index c1281c20..e13c1206 100644 --- a/src/structures/client.js +++ b/src/structures/client.js @@ -1,9 +1,8 @@ import { Client } from "discord.js"; import { Player } from "discord-player"; import MongooseAdapter from "../adapters/database/MongooseAdapter.js"; -import { init as initCommands } from "../handlers/command-handler/index.js"; -import { init as initEvents } from "../handlers/event-handler/index.js"; import logger from "../helpers/logger.js"; +import { Handlers } from "../handlers/index.js"; import ConfigService from "../services/config/index.js"; import InternationalizationService from "../services/languages/index.js"; import { SUPER_CONTEXT } from "../constants/index.js"; @@ -23,6 +22,7 @@ export class ExtendedClient extends Client { this.i18n = new InternationalizationService(this); this.cacheReminds = new Map(); new Player(this); + new Handlers(this); SUPER_CONTEXT.enterWith(this); } @@ -31,9 +31,7 @@ export class ExtendedClient extends Client { try { await this.adapter.connect(); - return this.login(this.configService.get("token")) - .then(async () => await Promise.all([initCommands(), initEvents()])) - .catch(console.error); + await this.login(this.configService.get("token")); } catch (error) { logger.error(error); }