improve errors

This commit is contained in:
artie 2025-03-27 17:43:40 +01:00
parent a59dd8a32a
commit 1568439a42
5 changed files with 50 additions and 17 deletions

View File

@ -5,7 +5,9 @@ import {
Collection,
GatewayIntentBits,
InteractionContextType,
lazy,
Partials,
TextChannel,
} from "discord.js";
import { REST } from "@discordjs/rest";
import { env } from "./env";
@ -56,6 +58,19 @@ export class ArtemisClient extends Client {
return this.users.fetch(this.ownerId);
}
getDevWebhook = lazy(async () => {
const channel = (await client.channels.fetch(
env.DEV_CHANNEL_ID
)) as TextChannel;
return channel
.fetchWebhooks()
.then(
webhooks =>
webhooks.find(wh => wh.applicationId === env.APPLICATION_ID) ??
channel.createWebhook({ name: "artemis" })
);
});
async loadCommands() {
const commandsDir = path.join(import.meta.dir, "commands");
const categories = await fs

View File

@ -52,7 +52,7 @@ export async function translateImpl(
target
).catch(err => {
logger.error(err, "DeepL error, falling back to Google Translate");
sendErrorAlert(err);
sendErrorAlert(err, { source, target, ocrModel });
return translateGoogle(text, "auto", "en");
});

View File

@ -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");
sendErrorAlert(err);
sendErrorAlert(err, { mime: type.mime });
return yandexOcr(compressed, type.mime);
})
.catch(() => abort("Failed to OCR the image"));

View File

@ -75,7 +75,11 @@ async function handleChatInputCommand(
const trace = nanoid();
content += `\ntrace: ${inlineCode(trace)}`;
logger.error({ trace, err });
sendErrorAlert(err, trace);
sendErrorAlert(err, {
trace,
command: command.data.name,
user: `${interaction.user.id} (${interaction.user.tag})`,
});
}
await interaction[

View File

@ -1,4 +1,4 @@
import { codeBlock } from "discord.js";
import { bold, codeBlock } from "discord.js";
import { client } from "../client";
export class ExplicitCommandError extends Error {}
@ -13,10 +13,25 @@ export function isExplicitCommandError(
return error instanceof ExplicitCommandError;
}
export async function sendErrorAlert(error: any, trace?: string) {
return client.getOwner().then(owner =>
owner.send({
content: trace,
export async function sendErrorAlert(
error: any,
meta?: Record<string, string | null | undefined>
) {
const owner = await client.getOwner();
const webhook = await client.getDevWebhook();
return webhook.send({
username: "artemis error",
avatarURL: "https://files.catbox.moe/g52ano.png",
allowedMentions: { users: [owner.id] },
content:
`${owner}\n` +
(meta
? Object.entries(meta)
.filter(([_, value]) => value)
.map(([key, value]) => `${bold(key)}: ${value}`)
.join("\n")
: ""),
embeds: [
{
title: "Unhandled Error",
@ -24,6 +39,5 @@ export async function sendErrorAlert(error: any, trace?: string) {
color: 0xff0000,
},
],
})
);
});
}