summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMathias Magnusson <mathias@magnusson.space>2026-01-22 14:30:07 +0100
committerMathias Magnusson <mathias@magnusson.space>2026-01-22 14:30:07 +0100
commit0ea60aa35c22ccf8d46ef6349a866ad7d8c569fe (patch)
tree9e5e6995010e3b5dc90debb1b8d089fdd9fdf2d7
parentcd6026c284d3d8ee2fbe6de7b20fcde9628d36cb (diff)
downloadchalle-anka-0ea60aa35c22ccf8d46ef6349a866ad7d8c569fe.tar.gz
error mesgmaster
-rw-r--r--index.ts39
1 files changed, 37 insertions, 2 deletions
diff --git a/index.ts b/index.ts
index 2a89958..09e6357 100644
--- a/index.ts
+++ b/index.ts
@@ -1,5 +1,6 @@
import {
Client,
+ DiscordAPIError,
Events,
GatewayIntentBits,
PermissionFlagsBits,
@@ -17,6 +18,39 @@ function requireEnv(key: string): string {
return value;
}
+function getInteractionErrorMessage(error: unknown): string {
+ let fallback = "Something went wrong! :(";
+ if (error instanceof DiscordAPIError) {
+ let rawMessage = "";
+ if (error.rawError &&
+ typeof error.rawError === "object" &&
+ "message" in error.rawError &&
+ typeof (error.rawError as { message?: unknown }).message === "string"
+ ) {
+ rawMessage = error.rawError.message.trim();
+ } else if (error.message) {
+ rawMessage = error.message.trim();
+ }
+ if (rawMessage && error.code) {
+ return `Something went wrong: ${rawMessage} (Discord code ${error.code})`;
+ }
+ if (rawMessage) {
+ return `Something went wrong: ${rawMessage}`;
+ }
+ if (error.status) {
+ return `Something went wrong: Discord API responded with status ${error.status}`;
+ }
+ return fallback;
+ }
+ if (error instanceof Error && error.message.trim()) {
+ return `Something went wrong: ${error.message.trim()}`;
+ }
+ if (typeof error === "string" && error.trim()) {
+ return `Something went wrong: ${error.trim()}`;
+ }
+ return fallback;
+}
+
let clientId = requireEnv("DISCORD_CLIENT_ID");
let token = requireEnv("DISCORD_TOKEN");
let activity = requireEnv("ACTIVITY");
@@ -74,15 +108,16 @@ client.on(Events.InteractionCreate, async (interaction) => {
} catch (error) {
if (error === "done") return;
console.error("Error while handling interaction", error);
+ let userMessage = getInteractionErrorMessage(error);
if (interaction.isChatInputCommand() || interaction.isButton()) {
if (interaction.deferred || interaction.replied) {
await interaction.followUp({
- content: "Something went wrong! :(",
+ content: userMessage,
flags: "Ephemeral",
});
} else {
await interaction.reply({
- content: "Something went wrong! :(",
+ content: userMessage,
flags: "Ephemeral",
});
}