Issue catching unreact event after bot has restarted

When I react to a message then restart the bot then unreact it doesnt catch the unreaction event. Function: https://codeshare.io/pAe73x
// Process existing reactions
for (const [_, reaction] of roleMessage.reactions.cache) {
const users = await reaction.users.fetch();
users.forEach(async (user: User) => {
if (user.bot) return;

try {
const member = await guild.members.fetch(user.id);

const emoji = reaction._emoji.name;
const reactionText = emoji.length < 4 ? emoji.slice(0, 2) : emoji;

for (let i = 0; i < icons.length; i++) {
if (icons[i].trim() === reactionText.trim()) {
await member.roles.add(roleIds[i]);
break;
}
}
} catch (error: any) {
if (error.code === 10007) {
// Member not found (likely left the guild)
} else {
console.error("Error fetching member or adding role:", error);
}
}
});
}
// Process existing reactions
for (const [_, reaction] of roleMessage.reactions.cache) {
const users = await reaction.users.fetch();
users.forEach(async (user: User) => {
if (user.bot) return;

try {
const member = await guild.members.fetch(user.id);

const emoji = reaction._emoji.name;
const reactionText = emoji.length < 4 ? emoji.slice(0, 2) : emoji;

for (let i = 0; i < icons.length; i++) {
if (icons[i].trim() === reactionText.trim()) {
await member.roles.add(roleIds[i]);
break;
}
}
} catch (error: any) {
if (error.code === 10007) {
// Member not found (likely left the guild)
} else {
console.error("Error fetching member or adding role:", error);
}
}
});
}
if I log reactionText it logs my reaction successfully after the restart. If I then wait for
console.log("Finished processing existing reactions.");
console.log("Finished processing existing reactions.");
5 Replies
d.js toolkit
d.js toolkit3mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Axe
AxeOP3mo ago
then unreact, the following function doesnt catch it:
roleCollector.on('remove', async (clickedReaction: Reaction, user: User) => {
const member = await guild.members.fetch(user.id);
const emoji = clickedReaction._emoji.name;
const reaction = emoji.length < 4 ? emoji.slice(0, 2) : emoji;

for (let i = 0; i < icons.length; i++) {
if (icons[i].trim() === reaction.trim()) {
await member.roles.remove(roleIds[i]);
break;
}
}
});
roleCollector.on('remove', async (clickedReaction: Reaction, user: User) => {
const member = await guild.members.fetch(user.id);
const emoji = clickedReaction._emoji.name;
const reaction = emoji.length < 4 ? emoji.slice(0, 2) : emoji;

for (let i = 0; i < icons.length; i++) {
if (icons[i].trim() === reaction.trim()) {
await member.roles.remove(roleIds[i]);
break;
}
}
});
However if I unreact then re-react then unreact again, the above function does catch the 2nd unreaction. npm list discord.js: [email protected] node -v: node: v22.7.0
d.js docs
d.js docs3mo ago
:guide: Popular Topics: Partial Structures read more
Axe
AxeOP3mo ago
Didnt see your message till now, but thanks, I realized that just before I saw your message, but it still doesnt work. I have done like so:
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildModeration
],
partials: [
Partials.Message,
Partials.Channel,
Partials.Reaction,
Partials.User
],
}) as any
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildModeration
],
partials: [
Partials.Message,
Partials.Channel,
Partials.Reaction,
Partials.User
],
}) as any
client.once(Events.ClientReady, async () => {
for (const role of roles) {
try {
const { message, channelID } = role

// Fetch channel and message
const channel = await client.channels.fetch(channelID)
if (!channel) {
return console.log(`Channel with ID ${channelID} not found.`)
}

const roleMessage = await (channel as any).messages.fetch(message)
if (!roleMessage) {
return console.log(`Message with ID ${message} not found.`)
}

// Fetches missing partial data for the message
if (roleMessage.partial) {
console.log("it is partial")
try {
await roleMessage.fetch();
console.log("fetched")
} catch (error) {
console.error(`Something went wrong when fetching role message partial: ${error}`);
return
}
} else {
console.log(`Message ${roleMessage.id} is not partial.`)
}

// Extract guild, roles, and icons
const guild = client.guilds.cache.get(roleMessage.guildId)
const content = roleMessage.embeds[0].data.fields[0].value
if (!guild) {
return console.log(`Guild ${roleMessage.guildId} does not exist.`)
}

const roleRegex = /<@&(\d+)>/g
const messageRoles = content.match(roleRegex) || []
const roleIds = messageRoles.map((match: string) => match.slice(3, -1))

const icons = content.split('\n').map((icon: string) =>
icon[1] === ':' ? icon.split(':')[1] : icon.substring(0, 2)
)
client.once(Events.ClientReady, async () => {
for (const role of roles) {
try {
const { message, channelID } = role

// Fetch channel and message
const channel = await client.channels.fetch(channelID)
if (!channel) {
return console.log(`Channel with ID ${channelID} not found.`)
}

const roleMessage = await (channel as any).messages.fetch(message)
if (!roleMessage) {
return console.log(`Message with ID ${message} not found.`)
}

// Fetches missing partial data for the message
if (roleMessage.partial) {
console.log("it is partial")
try {
await roleMessage.fetch();
console.log("fetched")
} catch (error) {
console.error(`Something went wrong when fetching role message partial: ${error}`);
return
}
} else {
console.log(`Message ${roleMessage.id} is not partial.`)
}

// Extract guild, roles, and icons
const guild = client.guilds.cache.get(roleMessage.guildId)
const content = roleMessage.embeds[0].data.fields[0].value
if (!guild) {
return console.log(`Guild ${roleMessage.guildId} does not exist.`)
}

const roleRegex = /<@&(\d+)>/g
const messageRoles = content.match(roleRegex) || []
const roleIds = messageRoles.map((match: string) => match.slice(3, -1))

const icons = content.split('\n').map((icon: string) =>
icon[1] === ':' ? icon.split(':')[1] : icon.substring(0, 2)
)
// Create a reaction collector
const roleCollector = roleMessage.createReactionCollector({
filter: (reaction: Reaction, user: User) => !user.bot,
dispose: true,
})

addRole({ collector: roleCollector, guild, roles: roleIds, icons})
removeRole({ collector: roleCollector, guild, roles: roleIds, icons})
} catch (error: any) {
console.error("Error processing roles:", error)
}
}

autoCreateMeetings(client)

console.log("Ready!")
})
// Create a reaction collector
const roleCollector = roleMessage.createReactionCollector({
filter: (reaction: Reaction, user: User) => !user.bot,
dispose: true,
})

addRole({ collector: roleCollector, guild, roles: roleIds, icons})
removeRole({ collector: roleCollector, guild, roles: roleIds, icons})
} catch (error: any) {
console.error("Error processing roles:", error)
}
}

autoCreateMeetings(client)

console.log("Ready!")
})
Successfully reloaded 14 application (/) commands.
Message ... is not partial.
Message ... is not partial.
Message ... is not partial.
Ready!
Successfully reloaded 14 application (/) commands.
Message ... is not partial.
Message ... is not partial.
Message ... is not partial.
Ready!
it says none of them are partial, but they have hundreds of reactions?? please ping btw otherwise I wont see it
Axe
AxeOP3mo ago
nvm I found the cause and a workaround, I explained it in my comment to https://github.com/discordjs/discord.js/issues/10487, it was closed as invalid but after further debugging I found that the problem was at least not what it was closed as
GitHub
Issues · discordjs/discord.js
A powerful JavaScript library for interacting with the Discord API - Issues · discordjs/discord.js
Want results from more Discord servers?
Add your server