简体   繁体   English

如何从 Discord.js v13 中拥有特定角色的每个人中删除特定角色

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

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我正在使用 discord.js v13 和 node.js v16.4.2

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);
    })
})

As SP73 said, putting this on your messageCreate event will get you ratelimited fast.正如 SP73 所说,将其放在您的 messageCreate 事件中将使您快速限制速率。 Try putting this on your ready event instead.试着把它放在你准备好的事件上。 This will get every member of the provided server on the cache (I'm sure you don't have a verified bot so discord.js would most likely save it on the cache for you) and only select those who have the role, then for each one, at a delay of 1500 millisecondes (1 and half second), remove the role from them.这会将所提供服务器的每个成员都保存在缓存中(我确定您没有经过验证的机器人,因此 discord.js 很可能会为您将其保存在缓存中)并且只选择那些具有该角色的人,然后对于每一个,以 1500 毫秒(1 秒半)的延迟,从它们中删除角色。

client.on('ready',() =>{
  let n = 0;
  let { members } = client.guilds.cache.get("GUILD_ID");
  members.filter(m => m.roles.cache.has("ROLE_ID")).forEach(member => {
    setTimeout(() => member.roles.remove("ROLE_ID"), (++n) * 1500);
  })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM