mirror of
https://github.com/JonnyBro/JaBa.git
synced 2025-01-20 01:13: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 { resolve } from "node:path";
|
||||||
import logger from "../../helpers/logger.js";
|
import logger from "../../helpers/logger.js";
|
||||||
import useClient from "../../utils/use-client.js";
|
|
||||||
import { getFilePaths } from "../../utils/index.js";
|
import { getFilePaths } from "../../utils/index.js";
|
||||||
import { toFileURL } from "../../utils/resolve-file.js";
|
import { toFileURL } from "../../utils/resolve-file.js";
|
||||||
import registerCommands from "./functions/registerCommands.js";
|
import registerCommands from "./functions/registerCommands.js";
|
||||||
|
|
||||||
export const commands = [];
|
export class CommandHandler {
|
||||||
|
constructor(client) {
|
||||||
|
this.client = client;
|
||||||
|
this.commands = [];
|
||||||
|
}
|
||||||
|
|
||||||
export const init = async () => {
|
async init() {
|
||||||
const client = useClient();
|
await this.#buildCommands();
|
||||||
await buildCommands();
|
|
||||||
|
|
||||||
await registerCommands({
|
await registerCommands({
|
||||||
client,
|
client: this.client,
|
||||||
commands,
|
commands: this.commands,
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
const buildCommands = async () => {
|
async #buildCommands() {
|
||||||
const client = useClient();
|
const cmdPath = resolve(this.client.configService.get("paths.commands"));
|
||||||
const cmdPath = resolve(client.configService.get("paths.commands"));
|
|
||||||
const commandFilePaths = (await getFilePaths(cmdPath, true)).filter(path => path.endsWith(".js"));
|
const commandFilePaths = (await getFilePaths(cmdPath, true)).filter(path => path.endsWith(".js"));
|
||||||
|
|
||||||
for (const cmdFilePath of commandFilePaths) {
|
for (const cmdFilePath of commandFilePaths) {
|
||||||
|
@ -35,6 +36,7 @@ const buildCommands = async () => {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
commands.push({ data, run });
|
this.commands.push({ data, run });
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,21 +1,23 @@
|
||||||
import { resolve } from "node:path";
|
import { resolve } from "node:path";
|
||||||
import logger from "../../helpers/logger.js";
|
import logger from "../../helpers/logger.js";
|
||||||
import useClient from "../../utils/use-client.js";
|
|
||||||
import { getFilePaths } from "../../utils/index.js";
|
import { getFilePaths } from "../../utils/index.js";
|
||||||
import { toFileURL } from "../../utils/resolve-file.js";
|
import { toFileURL } from "../../utils/resolve-file.js";
|
||||||
import { useMainPlayer } from "discord-player";
|
import { useMainPlayer } from "discord-player";
|
||||||
|
|
||||||
export const events = [];
|
export class EventHandler {
|
||||||
|
constructor(client) {
|
||||||
|
this.events = [];
|
||||||
|
this.client = client;
|
||||||
|
}
|
||||||
|
|
||||||
export const init = async () => {
|
async init() {
|
||||||
await buildEvents();
|
await this.#buildEvents();
|
||||||
registerEvents();
|
this.$registerEvents();
|
||||||
};
|
}
|
||||||
|
|
||||||
const buildEvents = async () => {
|
async #buildEvents() {
|
||||||
const client = useClient();
|
|
||||||
try {
|
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"));
|
const eventFilePaths = (await getFilePaths(eventPath, true)).filter(path => path.endsWith(".js"));
|
||||||
|
|
||||||
for (const eventFilePath of eventFilePaths) {
|
for (const eventFilePath of eventFilePaths) {
|
||||||
|
@ -31,19 +33,20 @@ const buildEvents = async () => {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
events.push({ data, run });
|
this.events.push({ data, run });
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error("Error build events: ", 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
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 { Client } from "discord.js";
|
||||||
import { Player } from "discord-player";
|
import { Player } from "discord-player";
|
||||||
import MongooseAdapter from "../adapters/database/MongooseAdapter.js";
|
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 logger from "../helpers/logger.js";
|
||||||
|
import { Handlers } from "../handlers/index.js";
|
||||||
import ConfigService from "../services/config/index.js";
|
import ConfigService from "../services/config/index.js";
|
||||||
import InternationalizationService from "../services/languages/index.js";
|
import InternationalizationService from "../services/languages/index.js";
|
||||||
import { SUPER_CONTEXT } from "../constants/index.js";
|
import { SUPER_CONTEXT } from "../constants/index.js";
|
||||||
|
@ -23,6 +22,7 @@ export class ExtendedClient extends Client {
|
||||||
this.i18n = new InternationalizationService(this);
|
this.i18n = new InternationalizationService(this);
|
||||||
this.cacheReminds = new Map();
|
this.cacheReminds = new Map();
|
||||||
new Player(this);
|
new Player(this);
|
||||||
|
new Handlers(this);
|
||||||
|
|
||||||
SUPER_CONTEXT.enterWith(this);
|
SUPER_CONTEXT.enterWith(this);
|
||||||
}
|
}
|
||||||
|
@ -31,9 +31,7 @@ export class ExtendedClient extends Client {
|
||||||
try {
|
try {
|
||||||
await this.adapter.connect();
|
await this.adapter.connect();
|
||||||
|
|
||||||
return this.login(this.configService.get("token"))
|
await this.login(this.configService.get("token"));
|
||||||
.then(async () => await Promise.all([initCommands(), initEvents()]))
|
|
||||||
.catch(console.error);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(error);
|
logger.error(error);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue