mirror of
https://github.com/artiemis/artemis.js.git
synced 2026-02-14 02:11:55 +00:00
improve errors
This commit is contained in:
parent
a59dd8a32a
commit
1568439a42
@ -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
|
||||
|
||||
@ -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");
|
||||
});
|
||||
|
||||
|
||||
@ -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"));
|
||||
|
||||
@ -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[
|
||||
|
||||
@ -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,17 +13,31 @@ export function isExplicitCommandError(
|
||||
return error instanceof ExplicitCommandError;
|
||||
}
|
||||
|
||||
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,
|
||||
},
|
||||
],
|
||||
})
|
||||
);
|
||||
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",
|
||||
description: codeBlock("js", error.stack ?? error.message),
|
||||
color: 0xff0000,
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user