mirror of
https://github.com/JonnyBro/JaBa.git
synced 2025-01-19 17:03:47 +05:00
fix: fixing handlers
This commit is contained in:
parent
32a4ca1c01
commit
de46344ccf
4 changed files with 93 additions and 72 deletions
|
@ -1,25 +1,26 @@
|
|||
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 class CommandHandler {
|
||||
constructor(client) {
|
||||
this.client = client;
|
||||
this.commands = [];
|
||||
}
|
||||
|
||||
export const init = async () => {
|
||||
const client = useClient();
|
||||
await buildCommands();
|
||||
async init() {
|
||||
await this.#buildCommands();
|
||||
|
||||
await registerCommands({
|
||||
client,
|
||||
commands,
|
||||
client: this.client,
|
||||
commands: this.commands,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
const buildCommands = async () => {
|
||||
const client = useClient();
|
||||
const cmdPath = resolve(client.configService.get("paths.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) {
|
||||
|
@ -35,6 +36,7 @@ const buildCommands = async () => {
|
|||
continue;
|
||||
}
|
||||
|
||||
commands.push({ data, run });
|
||||
this.commands.push({ data, run });
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,21 +1,23 @@
|
|||
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();
|
||||
async #buildEvents() {
|
||||
try {
|
||||
const eventPath = resolve(client.configService.get("paths.events"));
|
||||
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) {
|
||||
|
@ -31,19 +33,20 @@ const buildEvents = async () => {
|
|||
continue;
|
||||
}
|
||||
|
||||
events.push({ data, run });
|
||||
this.events.push({ data, run });
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error("Error build events: ", error);
|
||||
}
|
||||
};
|
||||
|
||||
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);
|
||||
logger.log("Events loaded");
|
||||
}
|
||||
};
|
||||
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
18
src/handlers/index.js
Normal file
18
src/handlers/index.js
Normal file
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue