1

使用此命令,我的代码偶尔会收到 400 bad request 错误:

@owner_only
async def cmd_bootybomb(self, user_mentions, channel):
    userid = (str(user_mentions))
    await self.send_message(discord.Object(id = userid), bootybomb) 

该变量bootybomb已从另一个文件导入,该文件只是一组设置为一个变量的链接。

我得到的错误是:

Traceback (most recent call last):
  File "C:\Users\pc\Desktop\Gamefolders\BootyBot\BootyBot\musicbot\bot.py", line 1289, in on_message
    response = await handler(**handler_kwargs)
  File "C:\Users\pc\Desktop\Gamefolders\BootyBot\BootyBot\musicbot\bot.py", line 115, in wrapper
    return await func(self, *args, **kwargs)
  File "C:\Users\pc\Desktop\Gamefolders\BootyBot\BootyBot\musicbot\bot.py", line 877, in cmd_bootybomb
    await self.send_message(discord.Object(id = userid), bootybomb)
  File "C:\Users\pc\AppData\Local\Programs\Python\Python35\lib\site-packages\discord\client.py", line 831, in send_message
    data = yield from self.http.send_message(channel_id, content, guild_id=guild_id, tts=tts)
  File "C:\Users\pc\AppData\Local\Programs\Python\Python35\lib\site-packages\discord\http.py", line 137, in request
    raise HTTPException(r, data)
discord.errors.HTTPException: BAD REQUEST (status code: 400)

为什么会发生这种情况以及如何解决?

4

1 回答 1

3

由于您使用的是 discord api,如果您阅读 send_message 的描述,如果您发送的消息超过 2000 chrs,discord 会引发 400 请求错误。因为 Discord 的字符限制是 2000。如您所见,它实际上并不是一个真正的错误,discord.errors.HTTPException: BAD REQUEST (status code: 400)。这是不和谐 API 造成的自定义错误。

client.send_message:引发:HTTPException – 发送消息失败。

并且我前段时间也问过支持,关于这个问题,这是他们给我的解释。基本上,您必须将消息的字符保持在 2000 以下。

这是此问题的最常见原因。但是,如果您的问题仍然存在,解决此问题的最佳选择是加入 discord.py API 的支持服务器,并告诉他们您的问题。因为它主要是 API 问题,而不是 python 问题。

于 2017-03-18T15:31:57.267 回答