generated from nhcarrigan/template
feat: error handling
All checks were successful
Node.js CI / Lint and Test (pull_request) Successful in 37s
All checks were successful
Node.js CI / Lint and Test (pull_request) Successful in 37s
This commit is contained in:
parent
c63003bb75
commit
7191aeead5
16
src/index.ts
16
src/index.ts
@ -28,6 +28,22 @@ const commands: Record<
|
|||||||
"summarise": summarise,
|
"summarise": summarise,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
process.on("unhandledRejection", (error) => {
|
||||||
|
if (error instanceof Error) {
|
||||||
|
void logger.error("Unhandled Rejection", error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void logger.error("unhandled rejection", new Error(String(error)));
|
||||||
|
});
|
||||||
|
|
||||||
|
process.on("uncaughtException", (error) => {
|
||||||
|
if (error instanceof Error) {
|
||||||
|
void logger.error("Uncaught Exception", error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void logger.error("uncaught exception", new Error(String(error)));
|
||||||
|
});
|
||||||
|
|
||||||
const client = new Client({
|
const client = new Client({
|
||||||
intents: [],
|
intents: [],
|
||||||
});
|
});
|
||||||
|
@ -13,14 +13,18 @@ import {
|
|||||||
MessageFlags,
|
MessageFlags,
|
||||||
type ChatInputCommandInteraction,
|
type ChatInputCommandInteraction,
|
||||||
} from "discord.js";
|
} from "discord.js";
|
||||||
|
import { logger } from "../utils/logger.js";
|
||||||
|
import { replyToError } from "../utils/replyToError.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Responds with information about the bot.
|
* Responds with information about the bot.
|
||||||
* @param interaction -- The interaction payload from Discord.
|
* @param interaction -- The interaction payload from Discord.
|
||||||
*/
|
*/
|
||||||
|
// eslint-disable-next-line max-lines-per-function -- Refactor at a later time.
|
||||||
export const about = async(
|
export const about = async(
|
||||||
interaction: ChatInputCommandInteraction,
|
interaction: ChatInputCommandInteraction,
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
|
try {
|
||||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||||
|
|
||||||
const version = process.env.npm_package_version ?? "Unknown";
|
const version = process.env.npm_package_version ?? "Unknown";
|
||||||
@ -65,4 +69,10 @@ export const about = async(
|
|||||||
components: [ row ],
|
components: [ row ],
|
||||||
embeds: [ embed ],
|
embeds: [ embed ],
|
||||||
});
|
});
|
||||||
|
} catch (error) {
|
||||||
|
await replyToError(interaction);
|
||||||
|
if (error instanceof Error) {
|
||||||
|
await logger.error("about command", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
@ -8,6 +8,8 @@ import { personality } from "../config/personality.js";
|
|||||||
import { ai } from "../utils/ai.js";
|
import { ai } from "../utils/ai.js";
|
||||||
import { calculateCost } from "../utils/calculateCost.js";
|
import { calculateCost } from "../utils/calculateCost.js";
|
||||||
import { isSubscribed } from "../utils/isSubscribed.js";
|
import { isSubscribed } from "../utils/isSubscribed.js";
|
||||||
|
import { logger } from "../utils/logger.js";
|
||||||
|
import { replyToError } from "../utils/replyToError.js";
|
||||||
import type { ImageBlockParam } from "@anthropic-ai/sdk/resources/index.js";
|
import type { ImageBlockParam } from "@anthropic-ai/sdk/resources/index.js";
|
||||||
|
|
||||||
const isValidContentType = (
|
const isValidContentType = (
|
||||||
@ -31,6 +33,7 @@ const isValidContentType = (
|
|||||||
export const alt = async(
|
export const alt = async(
|
||||||
interaction: ChatInputCommandInteraction,
|
interaction: ChatInputCommandInteraction,
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
|
try {
|
||||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||||
const sub = await isSubscribed(interaction);
|
const sub = await isSubscribed(interaction);
|
||||||
if (!sub) {
|
if (!sub) {
|
||||||
@ -113,4 +116,10 @@ export const alt = async(
|
|||||||
|
|
||||||
const { usage } = messages;
|
const { usage } = messages;
|
||||||
await calculateCost(usage, interaction.user.username, "alt-text");
|
await calculateCost(usage, interaction.user.username, "alt-text");
|
||||||
|
} catch (error) {
|
||||||
|
await replyToError(interaction);
|
||||||
|
if (error instanceof Error) {
|
||||||
|
await logger.error("alt-text command", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
@ -8,6 +8,8 @@ import { personality } from "../config/personality.js";
|
|||||||
import { ai } from "../utils/ai.js";
|
import { ai } from "../utils/ai.js";
|
||||||
import { calculateCost } from "../utils/calculateCost.js";
|
import { calculateCost } from "../utils/calculateCost.js";
|
||||||
import { isSubscribed } from "../utils/isSubscribed.js";
|
import { isSubscribed } from "../utils/isSubscribed.js";
|
||||||
|
import { logger } from "../utils/logger.js";
|
||||||
|
import { replyToError } from "../utils/replyToError.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Accepts an arbitrary code snippet from the user, then sends
|
* Accepts an arbitrary code snippet from the user, then sends
|
||||||
@ -17,6 +19,7 @@ import { isSubscribed } from "../utils/isSubscribed.js";
|
|||||||
export const evaluate = async(
|
export const evaluate = async(
|
||||||
interaction: ChatInputCommandInteraction,
|
interaction: ChatInputCommandInteraction,
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
|
try {
|
||||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||||
const sub = await isSubscribed(interaction);
|
const sub = await isSubscribed(interaction);
|
||||||
if (!sub) {
|
if (!sub) {
|
||||||
@ -44,4 +47,10 @@ export const evaluate = async(
|
|||||||
|
|
||||||
const { usage } = messages;
|
const { usage } = messages;
|
||||||
await calculateCost(usage, interaction.user.username, "evaluate");
|
await calculateCost(usage, interaction.user.username, "evaluate");
|
||||||
|
} catch (error) {
|
||||||
|
await replyToError(interaction);
|
||||||
|
if (error instanceof Error) {
|
||||||
|
await logger.error("evaluate command", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
@ -8,6 +8,8 @@ import { personality } from "../config/personality.js";
|
|||||||
import { ai } from "../utils/ai.js";
|
import { ai } from "../utils/ai.js";
|
||||||
import { calculateCost } from "../utils/calculateCost.js";
|
import { calculateCost } from "../utils/calculateCost.js";
|
||||||
import { isSubscribed } from "../utils/isSubscribed.js";
|
import { isSubscribed } from "../utils/isSubscribed.js";
|
||||||
|
import { logger } from "../utils/logger.js";
|
||||||
|
import { replyToError } from "../utils/replyToError.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Accepts a text snippet from the user. Submits it to Anthropic
|
* Accepts a text snippet from the user. Submits it to Anthropic
|
||||||
@ -17,6 +19,7 @@ import { isSubscribed } from "../utils/isSubscribed.js";
|
|||||||
export const mood = async(
|
export const mood = async(
|
||||||
interaction: ChatInputCommandInteraction,
|
interaction: ChatInputCommandInteraction,
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
|
try {
|
||||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||||
const sub = await isSubscribed(interaction);
|
const sub = await isSubscribed(interaction);
|
||||||
if (!sub) {
|
if (!sub) {
|
||||||
@ -45,4 +48,10 @@ export const mood = async(
|
|||||||
|
|
||||||
const { usage } = messages;
|
const { usage } = messages;
|
||||||
await calculateCost(usage, interaction.user.username, "mood");
|
await calculateCost(usage, interaction.user.username, "mood");
|
||||||
|
} catch (error) {
|
||||||
|
await replyToError(interaction);
|
||||||
|
if (error instanceof Error) {
|
||||||
|
await logger.error("mood command", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
@ -8,6 +8,8 @@ import { personality } from "../config/personality.js";
|
|||||||
import { ai } from "../utils/ai.js";
|
import { ai } from "../utils/ai.js";
|
||||||
import { calculateCost } from "../utils/calculateCost.js";
|
import { calculateCost } from "../utils/calculateCost.js";
|
||||||
import { isSubscribed } from "../utils/isSubscribed.js";
|
import { isSubscribed } from "../utils/isSubscribed.js";
|
||||||
|
import { logger } from "../utils/logger.js";
|
||||||
|
import { replyToError } from "../utils/replyToError.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Accepts a text snippet from the user. Submits it to Anthropic
|
* Accepts a text snippet from the user. Submits it to Anthropic
|
||||||
@ -17,6 +19,7 @@ import { isSubscribed } from "../utils/isSubscribed.js";
|
|||||||
export const proofread = async(
|
export const proofread = async(
|
||||||
interaction: ChatInputCommandInteraction,
|
interaction: ChatInputCommandInteraction,
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
|
try {
|
||||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||||
const sub = await isSubscribed(interaction);
|
const sub = await isSubscribed(interaction);
|
||||||
if (!sub) {
|
if (!sub) {
|
||||||
@ -45,4 +48,10 @@ export const proofread = async(
|
|||||||
|
|
||||||
const { usage } = messages;
|
const { usage } = messages;
|
||||||
await calculateCost(usage, interaction.user.username, "proofread");
|
await calculateCost(usage, interaction.user.username, "proofread");
|
||||||
|
} catch (error) {
|
||||||
|
await replyToError(interaction);
|
||||||
|
if (error instanceof Error) {
|
||||||
|
await logger.error("proofread command", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
@ -8,6 +8,8 @@ import { personality } from "../config/personality.js";
|
|||||||
import { ai } from "../utils/ai.js";
|
import { ai } from "../utils/ai.js";
|
||||||
import { calculateCost } from "../utils/calculateCost.js";
|
import { calculateCost } from "../utils/calculateCost.js";
|
||||||
import { isSubscribed } from "../utils/isSubscribed.js";
|
import { isSubscribed } from "../utils/isSubscribed.js";
|
||||||
|
import { logger } from "../utils/logger.js";
|
||||||
|
import { replyToError } from "../utils/replyToError.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Accepts an arbitrary question from the user, then sends it to Anthropic
|
* Accepts an arbitrary question from the user, then sends it to Anthropic
|
||||||
@ -17,6 +19,7 @@ import { isSubscribed } from "../utils/isSubscribed.js";
|
|||||||
export const query = async(
|
export const query = async(
|
||||||
interaction: ChatInputCommandInteraction,
|
interaction: ChatInputCommandInteraction,
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
|
try {
|
||||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||||
const sub = await isSubscribed(interaction);
|
const sub = await isSubscribed(interaction);
|
||||||
if (!sub) {
|
if (!sub) {
|
||||||
@ -45,4 +48,10 @@ export const query = async(
|
|||||||
|
|
||||||
const { usage } = messages;
|
const { usage } = messages;
|
||||||
await calculateCost(usage, interaction.user.username, "query");
|
await calculateCost(usage, interaction.user.username, "query");
|
||||||
|
} catch (error) {
|
||||||
|
await replyToError(interaction);
|
||||||
|
if (error instanceof Error) {
|
||||||
|
await logger.error("query command", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
@ -8,6 +8,8 @@ import { personality } from "../config/personality.js";
|
|||||||
import { ai } from "../utils/ai.js";
|
import { ai } from "../utils/ai.js";
|
||||||
import { calculateCost } from "../utils/calculateCost.js";
|
import { calculateCost } from "../utils/calculateCost.js";
|
||||||
import { isSubscribed } from "../utils/isSubscribed.js";
|
import { isSubscribed } from "../utils/isSubscribed.js";
|
||||||
|
import { logger } from "../utils/logger.js";
|
||||||
|
import { replyToError } from "../utils/replyToError.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Accepts a text snippet from the user. Submits it to Anthropic
|
* Accepts a text snippet from the user. Submits it to Anthropic
|
||||||
@ -17,6 +19,7 @@ import { isSubscribed } from "../utils/isSubscribed.js";
|
|||||||
export const summarise = async(
|
export const summarise = async(
|
||||||
interaction: ChatInputCommandInteraction,
|
interaction: ChatInputCommandInteraction,
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
|
try {
|
||||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||||
const sub = await isSubscribed(interaction);
|
const sub = await isSubscribed(interaction);
|
||||||
if (!sub) {
|
if (!sub) {
|
||||||
@ -45,4 +48,10 @@ export const summarise = async(
|
|||||||
|
|
||||||
const { usage } = messages;
|
const { usage } = messages;
|
||||||
await calculateCost(usage, interaction.user.username, "summarise");
|
await calculateCost(usage, interaction.user.username, "summarise");
|
||||||
|
} catch (error) {
|
||||||
|
await replyToError(interaction);
|
||||||
|
if (error instanceof Error) {
|
||||||
|
await logger.error("summarise command", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
38
src/utils/replyToError.ts
Normal file
38
src/utils/replyToError.ts
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
/**
|
||||||
|
* @copyright nhcarrigan
|
||||||
|
* @license Naomi's Public License
|
||||||
|
* @author Naomi Carrigan
|
||||||
|
*/
|
||||||
|
import {
|
||||||
|
ActionRowBuilder,
|
||||||
|
ButtonBuilder,
|
||||||
|
ButtonStyle,
|
||||||
|
type ChatInputCommandInteraction,
|
||||||
|
type MessageContextMenuCommandInteraction,
|
||||||
|
} from "discord.js";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Responds to an interaction with a generic error message.
|
||||||
|
* @param interaction -- The interaction payload from Discord.
|
||||||
|
*/
|
||||||
|
export const replyToError = async(
|
||||||
|
interaction:
|
||||||
|
| ChatInputCommandInteraction
|
||||||
|
| MessageContextMenuCommandInteraction,
|
||||||
|
): Promise<void> => {
|
||||||
|
const button = new ButtonBuilder().setLabel("Need help?").
|
||||||
|
setStyle(ButtonStyle.Link).
|
||||||
|
setURL("https://chat.nhcarrigan.com");
|
||||||
|
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(button);
|
||||||
|
if (interaction.deferred || interaction.replied) {
|
||||||
|
await interaction.editReply({
|
||||||
|
components: [ row ],
|
||||||
|
content: "An error occurred while running this command.",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await interaction.reply({
|
||||||
|
components: [ row ],
|
||||||
|
content: "An error occurred while running this command.",
|
||||||
|
});
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user