Home > front end >  Discord bot js v13, is not removing the specific role and adding a specific role
Discord bot js v13, is not removing the specific role and adding a specific role

Time:06-09

My code is not working, I need your help. It does not show any it only adds position in some bots.

client.on('messageCreate', async(message) => {
   
   // Membros role id = 871442068508123136
   // Manutenção role id = 978322456328687656
   

       let args = message.content.replace(config.prefix, "").split(" ");
       let command = args.shift();
       
   if(message.content.startsWith(config.prefix)) {
       if(command === 'manutencao') {
           const cargo = message.guild.roles.cache.get("871442068508123136");
           message.guild.members.cache.forEach((member) => { 
           if (member.roles.cache.has(cargo)) { // Ativar manutenção
               member.roles.remove("871442068508123136"); // Tirar role membro
               member.roles.add("978322456328687656"); // Adicionar role Manutenção
               console.log(member)
               message.channel.send("Ativado") // Mensagem de aviso que funcionou
           }
           else if(!member.roles.cache.has(cargo)) { // Desativar a Manutenção
               member.roles.remove("978322456328687656"); // Tirar o role Manutenção
               member.roles.add("871442068508123136"); // Adiciona rrole membro
               console.log(member)
               message.channel.send("Desativado") // Mensagem de aviso que funcionou
           }
           });
       }   

   }

});      

What does bot have to do?

  1. Check the role I specify
  2. If members have such a role, the bot will have to remove that role and add a new one.
  3. if the members don't have that role, the bot will have to remove another specific role and add a new one.

PS: The bot has to do this on all members of the server, except the bots.

CodePudding user response:

Use guild.members.fetch() for fetching all members from a server.

Instead of using message.guild.roles.cache.get(role.id) in has(), you should use role.id directly.

if (member.user.bot) return;: return if member is bot.

And if you use the message.channel.send() inside the loop, it will send a message to the server for each member.

if (command === 'manutencao') {
    const cargo = "871442068508123136";
    (await guild.members.fetch()).forEach((member) => {
        if (member.user.bot) return;
        if (member.roles.cache.has(cargo)) { // Ativar manutenção
            member.roles.remove("871442068508123136"); // Tirar role membro
            member.roles.add("978322456328687656"); // Adicionar role Manutenção
            console.log(member.user.username)
            return message.channel.send("Ativado") // Mensagem de aviso que funcionou
        }
        else if(!member.roles.cache.has(cargo)) { // Desativar a Manutenção
            member.roles.remove("978322456328687656"); // Tirar o role Manutenção
            member.roles.add("871442068508123136"); // Adiciona rrole membro
            console.log(member.user.username)
            return message.channel.send("Desativado") // Mensagem de aviso que funcionou
        }
    });
}
  • Related