Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.

Commit 46646e1

Browse files
committed
fix(reaction): fix bug with reacting with custom emojis
discord.js turns custom emojis to `<:_:[id]>` but it fails to parse it later, so I made a new function to parse it and pass in only the emoji ID to button and select menu
1 parent f4d1122 commit 46646e1

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

src/updater/ReactionUpdater.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
WebhookClient,
1818
time,
1919
} from 'discord.js';
20-
import { sortReactions } from '../utils/Utils.js';
20+
import { getEmojiId, sortReactions } from '../utils/Utils.js';
2121
import { HubSettingsBitField } from '../utils/BitFields.js';
2222
import BlacklistManager from '../managers/BlacklistManager.js';
2323
import { CustomID } from '../utils/CustomID.js';
@@ -67,7 +67,7 @@ export default class ReactionUpdater extends Factory {
6767
if (userBlacklisted || serverBlacklisted) return;
6868

6969
const reactedEmoji = reaction.emoji.toString();
70-
const dbReactions = originalMsg.reactions?.valueOf() as { [key: string]: string[] }; // eg. { '👍': 1, '👎': 2 }
70+
const dbReactions = (originalMsg.reactions?.valueOf() ?? {}) as { [key: string]: string[] }; // eg. { '👍': 1, '👎': 2 }
7171
const emojiAlreadyReacted = dbReactions[reactedEmoji] ?? [user.id];
7272

7373
// max 10 reactions
@@ -128,7 +128,7 @@ export default class ReactionUpdater extends Factory {
128128
// add user to cooldown list
129129
interaction.client.reactionCooldowns.set(interaction.user.id, Date.now() + 3000);
130130

131-
const dbReactions = messageInDb.originalMsg.reactions?.valueOf() as {
131+
const dbReactions = (messageInDb.originalMsg.reactions?.valueOf() ?? {}) as {
132132
[key: string]: Snowflake[];
133133
};
134134

@@ -169,7 +169,7 @@ export default class ReactionUpdater extends Factory {
169169
reactionMenu.components[0].addOptions({
170170
label: 'React/Unreact',
171171
value: r[0],
172-
emoji: r[0],
172+
emoji: getEmojiId(r[0]),
173173
});
174174
totalReactions++;
175175
reactionString += `- ${r[0]}: ${r[1].length}\n`;
@@ -287,13 +287,17 @@ export default class ReactionUpdater extends Factory {
287287
// sortedReactions[x] = emojiIds
288288
// sortedReactions[x][y] = arr of users
289289
const sortedReactions = sortReactions(reactions);
290+
console.log(getEmojiId(sortedReactions[0][0]));
290291
const reactionCount = sortedReactions[0][1].length;
291292
const mostReaction = sortedReactions[0][0];
293+
const mostReactionEmoji = getEmojiId(mostReaction);
294+
295+
if (!mostReactionEmoji) return;
292296

293297
const reactionBtn = new ActionRowBuilder<ButtonBuilder>().addComponents(
294298
new ButtonBuilder()
295299
.setCustomId(new CustomID().setIdentifier('reaction_', mostReaction).toString())
296-
.setEmoji(mostReaction)
300+
.setEmoji(mostReactionEmoji)
297301
.setStyle(ButtonStyle.Secondary)
298302
.setLabel(`${reactionCount}`),
299303
);
@@ -390,6 +394,11 @@ export default class ReactionUpdater extends Factory {
390394
userId: string,
391395
emoji: string,
392396
) {
397+
// if (reactionArr[emoji].length <= 1) {
398+
// delete reactionArr[emoji];
399+
// return;
400+
// }
401+
393402
const userIndex = reactionArr[emoji].indexOf(userId);
394403
reactionArr[emoji].splice(userIndex, 1);
395404
return reactionArr;

src/utils/Constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const REGEX = {
3333
/** matches slurs */
3434
SLURS: new RegExp(`\\b(${slurs.join('|')})\\b`, 'ig'),
3535
TENOR_LINKS: /https:\/\/tenor\.com\/view\/.*-(\d+)/,
36+
EMOJI: /<(a)?:([a-zA-Z0-9_]+):(\d+)>/,
3637
};
3738

3839
export const StaffIds = ['597265261665714186', '442653948630007808', '689082827979227160'];

src/utils/Utils.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,4 +327,17 @@ export const isDev = (userId: Snowflake) => {
327327

328328
export const escapeRegexChars = (input: string): string => {
329329
return input.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
330-
};
330+
};
331+
332+
export const parseEmoji = (emoji: string) => {
333+
const match = emoji.match(REGEX.EMOJI);
334+
if (!match) return null;
335+
336+
const [, animated, name, id] = match;
337+
return { animated: !!animated, name, id };
338+
};
339+
340+
export const getEmojiId = (emoji: string | undefined) => {
341+
const res = parseEmoji(emoji || '');
342+
return res?.id ?? emoji;
343+
};

0 commit comments

Comments
 (0)