Skip to content

Commit 7dcf32d

Browse files
Merge pull request #183 from Pdzly/feature/improve_starboard
2 parents 278c3ce + 26a4322 commit 7dcf32d

File tree

1 file changed

+73
-19
lines changed

1 file changed

+73
-19
lines changed

src/modules/starboard/starboard.listener.ts

Lines changed: 73 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import type {
2-
Embed,
3-
GuildMember,
4-
Message,
5-
SendableChannels,
6-
Snowflake,
1+
import {
2+
DiscordAPIError,
3+
type Embed,
4+
type GuildMember,
5+
type Message,
6+
type SendableChannels,
7+
type Snowflake,
78
} from "discord.js";
89
import * as schedule from "node-schedule";
910
import { config } from "../../Config.js";
@@ -88,7 +89,7 @@ const getStarsFromEmbed: (embed: Embed) => number = (embed) => {
8889
if (split.length < 2) return 0;
8990
const stars = split[0]?.split(":")[1]?.trim();
9091
if (!stars) return 0;
91-
return parseInt(stars, 10);
92+
return Number.parseInt(stars, 10);
9293
};
9394

9495
export const StarboardListener: EventListener = {
@@ -141,7 +142,7 @@ export const StarboardListener: EventListener = {
141142
const channel = await guild.channels.fetch(
142143
dbStarboardMessage.originalMessageChannelId.toString(),
143144
);
144-
if (!channel || !channel.isTextBased()) return; // Channel is not available? ( Either we can hope it comes back or we can delete the entry from the database )
145+
if (!channel?.isTextBased()) return; // Channel is not available? ( Either we can hope it comes back or we can delete the entry from the database )
145146

146147
const starboardChannel = await guild.channels.fetch(
147148
config.starboard.channel,
@@ -156,10 +157,19 @@ export const StarboardListener: EventListener = {
156157
);
157158
return;
158159
}
160+
let message: Message | null = null;
161+
try {
162+
message = await channel.messages.fetch(
163+
dbStarboardMessage.originalMessageId.toString(),
164+
);
165+
} catch (e) {
166+
logger.error(
167+
"There was an error fetching the original Starboard message: ",
168+
e,
169+
);
170+
continue;
171+
}
159172

160-
const message = await channel.messages.fetch(
161-
dbStarboardMessage.originalMessageId.toString(),
162-
);
163173
const member = await getMember(message);
164174
if (!member) {
165175
logger.error(
@@ -172,12 +182,6 @@ export const StarboardListener: EventListener = {
172182
dbStarboardMessage.starboardMessageId.toString(),
173183
);
174184

175-
if (!message) {
176-
await starboardMessage.delete();
177-
await dbStarboardMessage.destroy();
178-
continue;
179-
}
180-
181185
if (!starboardMessage) {
182186
await dbStarboardMessage.destroy();
183187
continue;
@@ -218,8 +222,33 @@ export const StarboardListener: EventListener = {
218222
);
219223
},
220224
async messageReactionAdd(_, reaction) {
225+
if (reaction.partial) {
226+
// If the message this reaction belongs to was removed, the fetching might result in an API error which should be handled
227+
try {
228+
await reaction.fetch();
229+
} catch (error) {
230+
console.error(
231+
"Starboard: Something went wrong when fetching the reaction:",
232+
error,
233+
);
234+
// Return as `reaction.message.author` may be undefined/null
235+
return;
236+
}
237+
}
238+
221239
let message = reaction.message;
222-
if (message.partial) message = await message.fetch();
240+
if (message.partial) {
241+
// If the message this reaction belongs to was removed, the fetching might result in an API error which should be handled
242+
try {
243+
message = await message.fetch();
244+
} catch (error) {
245+
console.error(
246+
"Starboard: Something went wrong when fetching the message:",
247+
error,
248+
);
249+
return;
250+
}
251+
}
223252
if (
224253
!message.inGuild() ||
225254
message.author.bot ||
@@ -299,8 +328,33 @@ export const StarboardListener: EventListener = {
299328
},
300329

301330
async messageReactionRemove(_, reaction) {
331+
if (reaction.partial) {
332+
// If the message this reaction belongs to was removed, the fetching might result in an API error which should be handled
333+
try {
334+
await reaction.fetch();
335+
} catch (error) {
336+
console.error(
337+
"Starboard: Something went wrong when fetching the reaction:",
338+
error,
339+
);
340+
// Return as `reaction.message.author` may be undefined/null
341+
return;
342+
}
343+
}
344+
302345
let message = reaction.message;
303-
if (message.partial) message = await reaction.message.fetch();
346+
if (message.partial) {
347+
// If the message this reaction belongs to was removed, the fetching might result in an API error which should be handled
348+
try {
349+
message = await message.fetch();
350+
} catch (error) {
351+
console.error(
352+
"Starboard: Something went wrong when fetching the message:",
353+
error,
354+
);
355+
return;
356+
}
357+
}
304358
if (
305359
!message.inGuild() ||
306360
message.author.bot ||

0 commit comments

Comments
 (0)