Skip to content
This repository was archived by the owner on Sep 1, 2023. It is now read-only.

Commit 3d8595a

Browse files
committed
"error handling"
1 parent 60bbc91 commit 3d8595a

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/index.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { defaultConfig } from "./constants.js";
77
import { handleGameResend, onButtonPress, onModalSubmit, onSelectMenu } from "./gameLogic/index.js";
88
import { patch } from "./patchContext.js";
99
import { Command, Config } from "./types.js";
10+
import { onMsgError } from "./utils.js";
1011

1112
declare global {
1213
interface Array<T> {
@@ -64,18 +65,28 @@ client.on("messageCreate", msg => {
6465
if (!msg.content.startsWith(prefix)) return;
6566
const args = msg.content.slice(prefix.length).split(/ +/);
6667
const command = args.shift();
67-
if (commands[command]) commands[command].execute(msg, args);
68+
if (commands[command]) {
69+
try {
70+
commands[command].execute(msg, args);
71+
} catch (e) {
72+
onMsgError(e, msg);
73+
}
74+
}
6875
});
6976

7077
client.on("interactionCreate", ctx => {
7178
patch(ctx);
7279

73-
if (ctx.type === InteractionTypes.MESSAGE_COMPONENT) {
74-
if (ctx.isButtonComponentInteraction()) onButtonPress(ctx);
75-
else onSelectMenu(ctx);
76-
}
77-
else if (ctx.type === InteractionTypes.MODAL_SUBMIT) {
78-
onModalSubmit(ctx);
80+
try {
81+
if (ctx.type === InteractionTypes.MESSAGE_COMPONENT) {
82+
if (ctx.isButtonComponentInteraction()) onButtonPress(ctx);
83+
else onSelectMenu(ctx);
84+
}
85+
else if (ctx.type === InteractionTypes.MODAL_SUBMIT) {
86+
onModalSubmit(ctx);
87+
}
88+
} catch (e) {
89+
onMsgError(e, ctx);
7990
}
8091
});
8192

src/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import { Card, PlayerStorage, UnoGame } from "./types.js";
88

99
export function onMsgError(e: Error, ctx: { channelID: string }) {
1010
client.rest.channels.createMessage<AnyTextableGuildChannel>(ctx.channelID, {
11-
content: `\`\`\`ts\n${e.toString().replace(/\/[\w]{25,}/gi, "/[REDACTED]")}\`\`\``
11+
content: `\`\`\`ts\n${(
12+
process.argv[2] === "--dev" ? e.stack : e.toString()
13+
)?.replace(/\/[\w]{25,}/gi, "/[REDACTED]")}\`\`\``
1214
}).catch(() => { });
1315
if (e.message.includes("Unknown ")) return;
1416
console.log(e);

0 commit comments

Comments
 (0)