在 repl.it 上运行discord.py bot时如何解决“429:太多请求”?

Jam*_*ier 6 python discord discord.py

我的代码(见下文)运行良好,但随后弹出此错误并且不会消失:

"http.py", line 293, in static_login
    data = await self.request(Route('GET', '/users/@me')
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/http.py", line 209, in request
    raise HTTPException(r, data)
discord.errors.HTTPException: 429 Too Many Requests (error code: 0): "
Run Code Online (Sandbox Code Playgroud)

我搜索了该错误,发现人们是通过在多台服务器上运行强大的程序,或者人们在一台服务器上运行相同的代码大量次来获得该错误的。但是,我只在一台服务器上运行它,并且代码非常简单。

这是供参考的代码(它在 repl.it 中运行)(os.getenv 用于隐藏机器人的令牌):

import discord
import os

client = discord.Client()

@client.event
async def on_ready():
  print ('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
  if message.author == client.user:
    return
  
  if message.content.startswith('$hello'):
    await message.channel.send('Hello!')

client.run(os.getenv('TOKEN'))
Run Code Online (Sandbox Code Playgroud)

小智 7

除了避免错误之外,还有一种方法可以解决它。如果discord.errors.HTTPException: 429出现在 replit 的控制台中,只需kill 1在 shell 中使用该命令即可。此命令完全退出脚本,当您再次单击运行时,它将从不同的 IP 地址运行,绕过 Discord 速率限制。

如果错误经常发生,这可能会很麻烦。一个自动解决方案是使用另一个名为“restarter.py”的文件,其中包含以下代码:

from time import sleep
from os import system
sleep(7)
system("python main.py")
Run Code Online (Sandbox Code Playgroud)

然后在你的主脚本中:

import discord
import os

client = discord.Client()

@client.event
async def on_ready():
  print ('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
  if message.author == client.user:
    return
  
  if message.content.startswith('$hello'):
    await message.channel.send('Hello!')

try:
    client.run(os.getenv('TOKEN'))
except discord.errors.HTTPException:
    print("\n\n\nBLOCKED BY RATE LIMITS\nRESTARTING NOW\n\n\n")
    system("python restarter.py")
    system('kill 1')
Run Code Online (Sandbox Code Playgroud)

  • 对于“HTTPException”,您应该在重新启动之前检查它是否是“429”。您不想因为“403”或“400”而重新启动机器人。 (5认同)