diff --git a/src/client.ts b/src/client.ts index b599a55..2c5d1b9 100644 --- a/src/client.ts +++ b/src/client.ts @@ -52,6 +52,10 @@ export class ArtemisClient extends Client { Promise.all([this.loadCommands(), this.registerEvents()]); } + async getOwner() { + return this.users.fetch(this.ownerId); + } + async loadCommands() { const commandsDir = path.join(import.meta.dir, "commands"); const categories = await fs diff --git a/src/commands/language/translate.ts b/src/commands/language/translate.ts index caa1d84..7f2b97b 100644 --- a/src/commands/language/translate.ts +++ b/src/commands/language/translate.ts @@ -13,7 +13,7 @@ import { isTargetLanguage, translate as translateDeepl, } from "../../utils/deepl"; -import { abort, notifyError } from "../../utils/error"; +import { abort, sendErrorAlert } from "../../utils/error"; import type { OCRResult } from "../../types/ocr"; import { capitalize, languageCodeToName } from "../../utils/functions"; import { translate as translateGoogle } from "../../utils/gtrans"; @@ -52,7 +52,7 @@ export async function translateImpl( target ).catch(err => { logger.error(err, "DeepL error, falling back to Google Translate"); - notifyError(err); + sendErrorAlert(err); return translateGoogle(text, "auto", "en"); }); diff --git a/src/commands/ocr/ocr.ts b/src/commands/ocr/ocr.ts index 306e99e..453275a 100644 --- a/src/commands/ocr/ocr.ts +++ b/src/commands/ocr/ocr.ts @@ -6,7 +6,7 @@ import { } from "discord.js"; import { defineCommand } from ".."; import { downloadFile } from "../../utils/http"; -import { abort, notifyError } from "../../utils/error"; +import { abort, sendErrorAlert } from "../../utils/error"; import { yandexOcr } from "../../utils/yandex"; import sharp from "sharp"; import { @@ -88,7 +88,7 @@ export async function ocrImpl(url: string) { const result = await lensOcr(compressed) .catch(err => { logger.error(err, "Google Lens error, falling back to Yandex"); - notifyError(err); + sendErrorAlert(err); return yandexOcr(compressed, type.mime); }) .catch(() => abort("Failed to OCR the image")); diff --git a/src/events/interactionCreate.ts b/src/events/interactionCreate.ts index e918c3d..cb3e74b 100644 --- a/src/events/interactionCreate.ts +++ b/src/events/interactionCreate.ts @@ -12,7 +12,7 @@ import { import { client } from "../client"; import { logger } from "../utils/logger"; import { defineEvent } from "."; -import { isExplicitCommandError, notifyError } from "../utils/error"; +import { isExplicitCommandError, sendErrorAlert } from "../utils/error"; import { nanoid } from "../utils/functions"; import type { Command } from "../types/command"; @@ -75,7 +75,7 @@ async function handleChatInputCommand( const trace = nanoid(); content += `\ntrace: ${inlineCode(trace)}`; logger.error({ trace, err }); - notifyError(err, trace); + sendErrorAlert(err, trace); } await interaction[ diff --git a/src/utils/error.ts b/src/utils/error.ts index 39bdc26..ba68e72 100644 --- a/src/utils/error.ts +++ b/src/utils/error.ts @@ -1,6 +1,5 @@ -import { codeBlock, type TextChannel } from "discord.js"; +import { codeBlock } from "discord.js"; import { client } from "../client"; -import { env } from "../env"; export class ExplicitCommandError extends Error {} @@ -14,15 +13,17 @@ export function isExplicitCommandError( return error instanceof ExplicitCommandError; } -export async function notifyError(error: any, trace?: string) { - return (client.channels.cache.get(env.DEV_CHANNEL_ID) as TextChannel).send({ - content: trace, - embeds: [ - { - title: "Unhandled Error", - description: codeBlock("js", error.stack ?? error.message), - color: 0xff0000, - }, - ], - }); +export async function sendErrorAlert(error: any, trace?: string) { + return client.getOwner().then(owner => + owner.send({ + content: trace, + embeds: [ + { + title: "Unhandled Error", + description: codeBlock("js", error.stack ?? error.message), + color: 0xff0000, + }, + ], + }) + ); }