Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python编程Discord机器人-有没有避免超过速率限制的技巧?_Python_Discord.py - Fatal编程技术网

用Python编程Discord机器人-有没有避免超过速率限制的技巧?

用Python编程Discord机器人-有没有避免超过速率限制的技巧?,python,discord.py,Python,Discord.py,我正在使用Discord bot,出现以下错误: discord.errors.HTTPException: 429 Too Many Requests (error code: 0): You are being blocked from accessing our API temporarily due to exceeding our rate limits frequently. Please read our docs at https://discord.com/developers

我正在使用Discord bot,出现以下错误:

discord.errors.HTTPException: 429 Too Many Requests (error code: 0): You are being blocked from accessing our API temporarily due to exceeding our rate limits frequently. Please read our docs at https://discord.com/developers/docs/topics/rate-limits to prevent this moving forward.

很明显,我的机器人试图一次做太多的事情。我不确定将来采取什么措施来避免这种情况,有什么建议/提示吗?像Dank Memer和carl bot这样的大联盟机器人如何在不受费率限制的情况下完成如此多的工作?

错误消息中显示的URL详细介绍了如何避免费率限制。当API告诉您没有剩余请求时,您需要在bot中实现代码以停止调用API。

文档说明您收到了响应标题中剩余的请求量:

此时,假设您正在使用库,如
请求
,您可以在每个请求之后访问响应头,如:

response = requests.get(your_url)
remaining_requests = response.headers.get('X-RateLimit-Remaining')
如果
剩余请求==0
,则将该值合并到控制流中,并等待
X-RateLimit-Reset
epoch时间再次使用bot

API还准确地告诉您需要等待多长时间,因此您可以立即使用该值:

< HTTP/1.1 429 TOO MANY REQUESTS
< Content-Type: application/json
< Retry-After: 65
< X-RateLimit-Limit: 10
< X-RateLimit-Remaining: 0
< X-RateLimit-Reset: 1470173023.123
< X-RateLimit-Reset-After: 64.57
< X-RateLimit-Bucket: abcd1234
{
  "message": "You are being rate limited.",
  "retry_after": 64.57, # <==== Use this value
  "global": false
}
“retry_after”:64.57,#尽量不要利用你的方法来解决这个问题,因为这会使你被禁止。你每x时间只能做5个请求
< HTTP/1.1 429 TOO MANY REQUESTS
< Content-Type: application/json
< Retry-After: 65
< X-RateLimit-Limit: 10
< X-RateLimit-Remaining: 0
< X-RateLimit-Reset: 1470173023.123
< X-RateLimit-Reset-After: 64.57
< X-RateLimit-Bucket: abcd1234
{
  "message": "You are being rate limited.",
  "retry_after": 64.57, # <==== Use this value
  "global": false
}