python - 由于带有 discord.py 的 "BAD REQUEST",消息未发送

标签 python discord python-asyncio discord.py

我正在使用 Discord.py 制作一个机器人,但在尝试发送带有嵌入的消息时,我总是收到错误消息。

这是我得到的错误:

Traceback (most recent call last):
  File "C:\Users\pc\Documents\Storage\python\NanoBot\bot.py", line 101, in on_message
    await client.send_message(message.channel, embed=embed)
  File "C:\Users\pc\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\client.py", line 1152, in send_message
data = yield from self.http.send_message(channel_id, content, guild_id=guild_id, tts=tts, embed=embed)
  File "C:\Users\pc\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\http.py", line 198, in request
raise HTTPException(r, data)
discord.errors.HTTPException: BAD REQUEST (status code: 400)

我的代码:

embed = discord.Embed(color=target.color)
embed.set_thumbnail(url=target.avatar_url)
embed.set_author(name=str(target.name), url="Playing " + str(target.game))
embed.set_footer(text="!!userinfo command")
embed.add_field(name="Status", value=str(target.status))
embed.add_field(name="Nickname", value=str(target.nick))
embed.add_field(name="Account Created", value=str(target.created_at))
embed.add_field(name="Roles", value=str(roles))
embed.add_field(name="Joined at", value=str(target.joined_at))
await client.send_message(message.channel, embed=embed)

最佳答案

由于您使用的是 discord api,如果您阅读了 client.send_message 的描述,如果您在 embed 中发送的消息超过 2000 个字符,discord将引发 400 请求错误。 Discord 的字符数限制为 2000。

如您所见,它实际上并不是真正的错误,discord.errors.HTTPException: BAD REQUEST (status code: 400)。这是 discord API 造成的自定义错误。要更正它,您可以将消息拆分为少于 2000 个字符的嵌入并单独发送。需要明确的是,这不是因为服务器已关闭,而是因为服务器拒绝发送您的消息,因为它太长了。

关于python - 由于带有 discord.py 的 "BAD REQUEST",消息未发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43016830/

相关文章:

javascript - 如何检查和打印不一致的时间戳 react ?

python - 将协程传递给 AbstractEventLoop.call_later

python - 获取 ServerDisconnectedError 异常,Connection.release() 会帮助解决这个问题吗?

python - 在 PyQt 布局之间添加间隔线

javascript - 公会加入日期显示为今天的日期,而不是?

python - R 中的 Collat​​z 猜想

python - 尝试创建一个 Discord webhook,在代码中分配了头像和用户名

python - python aiohttp和asyncio没有循环事件

python - 如何在 Python 中不返回或中断的情况下中断函数

python - lldb python API 访问获取和设置断点命令(非 python 类型)