How can I remove a specific role from everyone that has it in Discord.js v13

Issue

I want my bot to remove a specific role from everyone that has it when a message is sent in a channel

client.on('messageCreate', async message => {

        if(message.channel.id === '954375143965216869'){ //counting
            message.guild.roles.get('944674567811645472').members.map(m=>m.member.id).delete();

            
        }
})

I’m using discord.js v13 and node.js v16.4.2

Solution

This should work, it’ll fetch all the members in the guild and for each member it will remove the role specified. Also note that you’ll need the proper gateway intents to run this and depending on the guild size, it may take multiple minutes. I wouldn’t recommend running it every time a message is sent as it’ll get you rate limited extremely fast.

message.guild.members.fetch().then(m => {
    m.forEach(member => {
        let role = message.guild.roles.cache.get("roleid");
        member.roles.remove(role)
    })
})

Also, if your guild is way too large that it gets too slow because of rate limits, try the code below so each change has a delay.

message.guild.members.fetch().then(m => {
    m.forEach(member => {
        let role = message.guild.roles.cache.get("roleid");
        setTimeout(() => {
            member.roles.remove(role)
        }, 1000);
    })
})

Answered By – SP73

Answer Checked By – Timothy Miller (AngularFixing Admin)

Leave a Reply

Your email address will not be published.