discord.errors.HTTPException: 400 Bad Request (error code: 5



我试图测试是否可以复制发送到test_copy_channel的所有消息并粘贴到test_paste_channel
虽然bot正在执行命令并正确记录嵌入,但我一直得到一个错误。

这是我使用的代码:

import discord
import os
from discord.ext import commands

intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix=',', intents=intents)

@bot.event
async def on_ready():
global test_paste_channel, test_copy_channel
test_paste_channel = bot.get_channel(868816978293452841)
test_copy_channel = bot.get_channel(808734570283139162)
print('bot is ready')

@bot.event
async def on_message(message):
# if message.author == bot.user:
#   return
if message.channel == test_copy_channel:
await test_paste_channel.send(message.content)
print(message.channel)
if message.content.startswith('!test'):
embed_var = discord.Embed(
title= '''title''', 
description= '''description''', 
color= discord.Color.red()
)

embed_var.set_footer(text='footer')
await message.channel.send(embed=embed_var)

bot.run(os.getenv('TOKEN'))
所以我要做的是将!test发送到test_copy_channel所以bot发送嵌入然后试图复制我的消息嵌入
我的消息通过了但是当bot试图复制嵌入时我得到了这个错误:
Ignoring exception in on_message
Traceback (most recent call last):
⠀⠀File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
⠀⠀⠀⠀await coro(*args, **kwargs)
⠀⠀File "main.py", line 25, in on_message
⠀⠀⠀⠀await test_channel.send(message.content)
⠀⠀File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/abc.py", line 1065, in send data = await state.http.send_message(channel.id, content, tts=tts, embed=embed,
⠀⠀File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/http.py", line 254, in request
⠀⠀⠀⠀raise HTTPException(r, data)
discord.errors.HTTPException: 400 Bad Request (error code: 50006): Cannot send an empty message

它似乎没有阻止命令执行,代码似乎工作正常。
据我所知,当它试图复制机器人发送的嵌入消息时,会触发错误。
我只是想知道为什么会触发这个错误。

好的,我认为发生的事情是当你写:!test!test在另一个通道中触发,反过来触发嵌入通过另一个通道,因为你有if message.content.startswith('!test')行,这不是通道特定的

然而,正在发生的问题是,当嵌入被发送时,on_message事件函数被调用。嵌入没有内容,所以当您尝试在await test_channel.send(message.content)行发送此内容时,错误会作为消息发生。内容为空(因为嵌入不是内容)。

修复此问题的作弊方法只是在await test_channel.send(message.content)上方添加if message.content:行,因为嵌入无论如何都要发送,因为!test在另一个通道中发送。

否则,你应该阅读这篇文章,看看如何从消息中获得嵌入信息(简而言之,它的embed_content_in_dict = message.embeds[0].to_dict())

希望这是有意义的:).

最新更新