Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

85
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda