Skip to content

Commit

Permalink
fix: loading errors
Browse files Browse the repository at this point in the history
  • Loading branch information
virtual-designer committed Jan 28, 2024
1 parent 7062fdd commit 14dec5a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"description": "A Discord bot for moderation purposes.",
"version": "7.25.0",
"main": "build/index.js",
"type": "module",
"license": "GPL-3.0-or-later",
"keywords": [
"bot",
Expand Down
4 changes: 3 additions & 1 deletion scripts/deploy-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ function makeContextMenuCommandBuilder(command) {
intents: []
});

await client.loadCommands();
await client.boot({
events: false
});

for (const [name, command] of client.commands) {
if (name.includes("__") || client.commands.get(name)?.name !== name) continue;
Expand Down
23 changes: 22 additions & 1 deletion src/core/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { PrismaClient } from "@prisma/client";
import { Collection, ClientEvents as DiscordClientEvents, Client as DiscordJSClient } from "discord.js";
import { Collection, ClientEvents as DiscordClientEvents, Client as DiscordJSClient, GuildEmoji } from "discord.js";
import path from "node:path";
import Server from "../api/Server";
import type AIAutoModService from "../automod/AIAutoModService";
Expand Down Expand Up @@ -67,6 +67,7 @@ import ServiceManager from "./ServiceManager";
class Client<R extends boolean = boolean> extends DiscordJSClient<R> {
private readonly eventListeners = new Map<string, Function[]>();
public readonly commands = new Collection<string, Command>();
public readonly emojiMap = new Map<string, GuildEmoji>();

public readonly prisma = new PrismaClient({
errorFormat: "pretty",
Expand Down Expand Up @@ -173,6 +174,10 @@ class Client<R extends boolean = boolean> extends DiscordJSClient<R> {
}
}

async getHomeGuild() {
return this.guilds.cache.get(process.env.HOME_GUILD_ID) ?? (await this.guilds.fetch(process.env.HOME_GUILD_ID));
}

addEventListener<K extends keyof ClientEvents>(name: K, listener: (...args: ClientEvents[K]) => unknown) {
const handlers = this.eventListeners.get(name) ?? [];

Expand Down Expand Up @@ -201,6 +206,22 @@ class Client<R extends boolean = boolean> extends DiscordJSClient<R> {
const handler = handlers.splice(index, 1)[0];
this.off(name as keyof DiscordClientEvents, handler as (...args: DiscordClientEvents[keyof DiscordClientEvents]) => void);
}

emitWaitLocal<K extends keyof ClientEvents>(name: K, ...args: ClientEvents[K]) {
return new Promise<void>(async (resolve, reject) => {
for (const listener of this.eventListeners.get(name) ?? []) {
await listener(...args);
}

resolve();
});
}

emitWait<K extends keyof ClientEvents>(name: K, ...args: ClientEvents[K]) {
for (const listener of this.eventListeners.get(name) ?? []) {
listener(...args);
}
}
}

export default Client;
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ const promise = (async () => {

await client.boot();

// if (process.env.SERVER_ONLY_MODE) {
// await client.server.boot();
// await client.server.start();
// } else {
// await client.login(process.env.TOKEN);
// }
if (process.env.SERVER_ONLY_MODE) {
await client.server.boot();
await client.server.start();
} else {
await client.login(process.env.TOKEN);
}
})();

export default promise;
2 changes: 1 addition & 1 deletion src/services/CommandManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export default class CommandManager extends Service {
}
};

await this.client.emitWait("command", command.name, handlerObject, command, message, context);
await this.client.emitWaitLocal("command", command.name, handlerObject, command, message, context);
await Promise.resolve();

if (handlerObject._stopped) {
Expand Down

0 comments on commit 14dec5a

Please sign in to comment.