Skip to content

Commit 0deb606

Browse files
authored
feat: handle message fetch, when it could have been deleted (#491)
1 parent 07a2e11 commit 0deb606

File tree

4 files changed

+24
-26
lines changed

4 files changed

+24
-26
lines changed

src/features/autothread.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { CHANNELS } from "../constants/channels.js";
44
import {
55
constructDiscordLink,
66
fetchReactionMembers,
7+
getMessage,
78
isHelpful,
89
isStaff,
910
} from "../helpers/discord.js";
@@ -58,7 +59,14 @@ const autoThread: ChannelHandlers = {
5859
return;
5960
}
6061

61-
const { channel: thread, author, guild } = await reaction.message.fetch();
62+
const reactionMsg = await getMessage(reaction.message);
63+
64+
if (!reactionMsg) {
65+
return;
66+
}
67+
68+
const { channel: thread, author, guild } = reactionMsg;
69+
6270
const starter = thread.isThread()
6371
? await thread.fetchStarterMessage()
6472
: undefined;

src/features/commands.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from "discord.js";
1212
import cooldown from "./cooldown.js";
1313
import type { ChannelHandlers } from "../types/index.d.ts";
14-
import { isStaff } from "../helpers/discord.js";
14+
import { getMessage, isStaff } from "../helpers/discord.js";
1515
import {
1616
extractSearchKey,
1717
getReactDocsContent,
@@ -1359,7 +1359,11 @@ const commands: ChannelHandlers = {
13591359
return;
13601360
}
13611361

1362-
const msg = await maybeMessage.fetch();
1362+
const msg = await getMessage(maybeMessage);
1363+
1364+
if (!msg) {
1365+
return;
1366+
}
13631367

13641368
commandsList.forEach((command) => {
13651369
const keyword = command.words.find((word) => {

src/helpers/discord.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ export const isStaffOrHelpful = (member: GuildMember) => {
5151
export const constructDiscordLink = (message: Message | PartialMessage) =>
5252
`https://discord.com/channels/${message.guild?.id}/${message.channel.id}/${message.id}`;
5353

54+
export const getMessage = async (message: Pick<Message, "fetch">) => {
55+
try {
56+
return await message.fetch();
57+
} catch {
58+
return null;
59+
}
60+
};
61+
5462
export const fetchReactionMembers = async (
5563
guild: Guild,
5664
reaction: MessageReaction | PartialMessageReaction,

src/index.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { recommendBookCommand } from "./features/book-list.js";
4242
import { mdnSearch } from "./features/mdn.js";
4343
import "./server.js";
4444
import { jobScanner } from "./features/job-scanner.js";
45+
import { getMessage } from "./helpers/discord.js";
4546

4647
export const bot = new Client({
4748
intents: [
@@ -134,29 +135,6 @@ const addHandler = (
134135
});
135136
};
136137

137-
const getMessage = async (message: Message) => {
138-
try {
139-
return await message.fetch();
140-
} catch (e: unknown) {
141-
logger.log(
142-
"ERROR",
143-
`Failed to fetch message: ${JSON.stringify({
144-
error: e,
145-
messageId: message.id,
146-
partial: message.partial,
147-
channelId: message.channelId,
148-
content: message.content,
149-
authorUsername: message.author?.username,
150-
authorSystem: message.author?.system,
151-
authorBot: message.author?.bot,
152-
system: message.system,
153-
})}`,
154-
);
155-
156-
return null;
157-
}
158-
};
159-
160138
const handleMessage = async (message: Message) => {
161139
if (message.system) {
162140
return;

0 commit comments

Comments
 (0)