diff --git a/apps/bot/src/lib/ErrorHandler.ts b/apps/bot/src/lib/ErrorHandler.ts new file mode 100644 index 00000000..0d6369c1 --- /dev/null +++ b/apps/bot/src/lib/ErrorHandler.ts @@ -0,0 +1,30 @@ +import { ErrorHandler as IgloErrorHandler, InteractionHandlerError } from "@snowcrystals/iglo"; +import { bold, underline } from "colorette"; +import { DiscordAPIError, type Interaction } from "discord.js"; + +export class ErrorHandler extends IgloErrorHandler { + /** + * Logs the error and makes sure the user is aware of the situation. This function is overridable for customisability. + * @param error The error that was emitted + * @param interaction The interaction from the user that used the bot + */ + public override async handleError(error: Error, interaction?: Interaction): Promise { + if (error instanceof InteractionHandlerError) { + let fatal = false; + if (["InvalidDirectory", "noConstructorOptions"].includes(error.type)) fatal = true; + + const message = `${bold(underline(`InteractionHandlerError(${error.type})`))}: ${error.message}`; + this.client.logger[fatal ? "fatal" : "error"](message); + } else if (error instanceof DiscordAPIError && interaction && !this.isSilencedError(interaction.channelId ?? "", interaction.guildId, error)) + this.client.logger.error(`${bold(underline(`DiscordAPIError(${error.name})`))}: ${error.message}`); + else this.client.logger.error(`${bold(underline(`Error(${error.name})`))}: ${error.message} --- raw=`, error); + + if (interaction && interaction.isRepliable()) + await interaction + .followUp({ + content: + "Welcome to our corner of errors, a place you shouldn't come to too often. It is probably not your fault though, something on our side brought you here. Stay safe out there, if this happens again make sure to contact the support team." + }) + .catch(() => void 0); + } +} diff --git a/apps/bot/src/lib/GitcordClient.ts b/apps/bot/src/lib/GitcordClient.ts index fc45ce3f..2396034e 100644 --- a/apps/bot/src/lib/GitcordClient.ts +++ b/apps/bot/src/lib/GitcordClient.ts @@ -3,13 +3,15 @@ import { IgloClient, LogLevel } from "@snowcrystals/iglo"; import { env } from "@/shared/env.js"; import { BOT_COMMANDS_DIR, BOT_LISTENER_DIR } from "./constants.js"; +import { ErrorHandler } from "./ErrorHandler.js"; export default class GitCordClient extends IgloClient { public constructor() { super({ client: { intents: ["GuildWebhooks", "Guilds"], allowedMentions: { repliedUser: true, roles: [], users: [] } }, paths: { commands: BOT_COMMANDS_DIR, events: BOT_LISTENER_DIR }, - logger: { level: process.env.NODE_ENV === "development" ? LogLevel.Debug : LogLevel.Info, parser: { depth: 2 } } + logger: { level: process.env.NODE_ENV === "development" ? LogLevel.Debug : LogLevel.Info, parser: { depth: 2 } }, + errorHandler: ErrorHandler }); } diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index ac38f2ff..82285ea0 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -4,8 +4,8 @@ services: image: ghcr.io/ijskoud/gitcord:api restart: always - volumes: - - ~/gitcord/.env:/gitcord/.env.local + env_file: + - ~/gitcord/.env ports: - 3001:3000 @@ -14,5 +14,5 @@ services: image: ghcr.io/ijskoud/gitcord:bot restart: always - volumes: - - ~/gitcord/.env:/gitcord/.env.local + env_file: + - ~/gitcord/.env