如何在不限制速率的情况下发出角色删除命令? [英] How to make a role remove command without getting rate limited?

查看:55
本文介绍了如何在不限制速率的情况下发出角色删除命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (!message.member.hasPermission("MANAGE_ROLES")) return message.channel.send("Nice try, but you can't")
        if (!message.guild.me.hasPermission("MANAGE_ROLES")) return message.channel.send('I don\'t have permissions.')
  const role = message.mentions.roles.first() 
    if (!role)  {
 return message.channel.send("Please mention a role");
    }else {
   await message.guild.members.fetch()
  const targets = message.guild.members.cache.filter(m => !m.roles.cache.has(role))


   await targets.forEach(member => member.roles.remove(role))
message.channel.send(`Done`)
}}};

这是用于从所有服务器成员中删除角色的命令.如何使其不达到速率限制?这是我尝试过的一些无效的解决方案.

This is a command for removing roles from all the server members. How can it be made such that it doesn't hit the rate limit? Here are some solutions I tried which didn't work.

targets.forEach((member, i) => { setTimeout(() => { member.roles.remove(role)) }, i*1000); });

var index = 0;
var myInterval = setInterval(function() {
   if (index < targets.length - 1)
      targets[index].roles.remove(role);
   else
     clearInterval(myInterval);

   index++;
}, 1000);

P•S当我在有80个成员的小型测试服务器上运行它时,它可以工作.但是在更大的服务器上却没有.那是因为速率限制(我认为).我怎么能不达到速率限制并完成呢?

P•S When I run it on my small test server with 80 members it works. But in bigger servers it doesn't. That's because of rate limit (I think). How can I not hit the rate limit and get it done?

推荐答案

您只会触发多个异步调用,如果它们遇到错误,则会返回错误.

Your just fire off multiple asynchronous calls, that return an error if they run into an error.

尝试使用地图代替,您可以等待通过Promise.all获得的诺言数组:

Try to use a map instead, and you can await the array of promises that you'll get with Promise.all:

  await Promise.all(targets.map(async (member) => {
   await member.roles.remove(role)
  }));

答案2

此函数允许我们执行任何API请求,如果受到速率限制,它将创建超时,并返回请求的时间.

This Function allows us to do any API request and if it's being rate limited it will create a timeout with the time that request returned.

const fetch = require("node-fetch")
function discordRetryHandler(input, init) {
    return new Promise((resolve, reject) => {
        fetch(input, init)
            .then(res => {
            if (res.headers.get('content-type') === 'application/json') {
                return res.json();
            }
            else {
                return { retry_after: undefined };
            }
        })
            .then(res => {
            if (typeof res.retry_after === 'number') {
                setTimeout(() => resolve(discordRetryHandler(input, init)), res.retry_after);
            }
            else {
                resolve(res);
            }
        })
            .catch(reject);
    });
}

如何使用?在下面的示例中,我将使用删除请求删除公会成员角色.

How to use it? In the example below I will use a delete request Remove Guild Member Role.

    const requestOptions = {
        method: 'DELETE',
        headers: {
            Authorization: `${client.user.bot ? "Bot " : ""}${client.token}`
        }
    };
   await discordRetryHandler(`https://discord.com/api/v7/guilds/${guild?.id}/members/${member?.id}/roles/${role.id}`, requestOptions);

这篇关于如何在不限制速率的情况下发出角色删除命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆