【问题标题】:In Discord.Py, how to solve the error for TooManyRequests?在 Discord.Py 中,如何解决 TooManyRequests 的错误?
【发布时间】:2022-03-07 20:11:53
【问题描述】:

我真的是编程新手,因此决定观看教程以了解如何使用 Python 创建 Discord 机器人。

本教程进行得很顺利,但随后弹出此错误,我似乎无法找到摆脱它的方法:

"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): "

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

这是参考代码(它在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'))

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

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

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

    from time import sleep
    from os import system
    sleep(7)
    system("python main.py")
    

    然后在你的主脚本中:

    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')
    

    【讨论】:

      【解决方案2】:

      您不应该将 repl.it 用于不和谐的机器人托管,建议购买 discord bot host,即使它很便宜。但是,您可以将其托管在您的计算机上。这是因为 IP 已达到不和谐的速率限制。您可以阅读速率限制here

      如果您仍想使用 repl.it,那么使用此代码会告诉您何时可以再次开始使用它。

      import requests
      
      r = requests.head(url="https://discord.com/api/v1")
      try:
          print(f"Rate limit {int(r.headers['Retry-After']) / 60} minutes left")
      except:
          print("No rate limit")
      

      【讨论】:

      • 由于我是初学者,我还不想在这上面花钱。感谢您提供备用资源!
      • 为什么不使用 Replit 进行托管?它明确支持托管:replit.com/site/hosting
      • @AmjadMasad 大多数 repl.it 机器人使用相同的 ip,因此一旦访问 api 并发出无效请求,它将影响所有机器人。然后从我不久前使用的内容来看,它没有附带 mysql 或 mongodb 数据库。 Sqlite 数据库已损坏。
      • 作为我理解的证据@AmjadMasad this 会起作用以及为什么我不会使用它
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-23
      • 1970-01-01
      • 2017-06-05
      • 1970-01-01
      • 1970-01-01
      • 2020-11-29
      • 2017-10-06
      相关资源
      最近更新 更多