Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

83
Vistas
Discord.js Bot Roles Cache Out Out Of Sync

I just started having a problem with my discord.js role management bot. The bot will successfully add a role upon reaction event. If a user then removes the reaction, the role should disappear.

I have a helper method that early returns if the user doesn't have the role (to avoid unnecessary work, and keep logging clean). This method is preventing the role from being removed on the first reaction removal event. The sequence is as follows:

  1. User adds a reaction, the bot gives the user a new role
  2. User removes reaction, bot early returns because guild.members.cache.get(user.id).roles.cache.has(role.id) returns false.
  3. User adds reaction again, the bot early returns, but now the role appears in the user's role cache.
  4. User removes reaction again, the bot removes the role from the user.

The role properly appears in discord correctly the first time, however I did some sleuthing, and added the following code to the bot:

 guild.members.cache.get(member.id).roles.add(role.id)
    .then(() => guild.members.cache.get(author.id))
    .then(user => console.log(user._roles));

The output doesn't include the new role, despite the user being promoted in the client, so it seems the cache is not updated immediately. How can I make sure I'm fetching the most up to date information?

Edit: I ended up working around this by creating my own cache to store roles in, but for posterity, here's some additional code to try to isolate the cause:

let guild;
const messageId = //some value
const roleId = //some value
const channelId = //some value
const emojiName = //some value

client.on('messageReactionAdd', async (reaction, user) => {
  handleReactionAdded(reaction, user)
});

client.on('messageReactionRemove', async (reaction, user) => {
  handleReactionRemoval(reaction, user)
});

const handleReactionAdded = async (reaction, user) => {
  if (reaction.message.partial) await reaction.message.fetch();
  if (reaction.partial) await reaction.fetch();
  if (user.bot || !reaction.message.guild) return;
  if (reaction.message.id == messageId && reaction.message.channel.id == channelId && reaction.emoji.name === emojiName) {

    guild = reaction.message.guild
    if (_hasRole(user)) return;
    guild.members.cache
        .get(user.id)
        .roles
        .add(roleId)
}

const handleReactionAdded = async (reaction, user) => {
  if (reaction.message.partial) await reaction.message.fetch();
  if (reaction.partial) await reaction.fetch();
  if (user.bot || !reaction.message.guild) return;
  if (reaction.message.id == messageId && reaction.message.channel.id == channelId && reaction.emoji.name === emojiName) {

    guild = reaction.message.guild
    if (!_hasRole(user)) return;
    guild.members.cache
        .get(user.id)
        .roles
        .remove(roleId)
}

const _hasRole = user => {
  return guild.members.cache.get(user.id).roles.has(roleId);
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If it's an issue with the cache being out of date, force fetching the member from the API after you add the role might resolve it, since the roles manager doesn't seem to have its own fetch method, but I'm not certain.

await guild.members.fetch({user, force: true})
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda