如何使用Discord Bot复制并重新发送Embed消息?
Discord Bot转发Embed消息报错解决方法
问题描述
- 需求:通过自有Discord Bot转发指定ID的Discohook Embed消息,已知来源频道ID、目标频道ID和消息ID
- 尝试代码:
def runDiscordBot(): TOKEN = "" client = commands.Bot(command_prefix="$", intents = discord.Intents.all()) @client.event async def on_ready(): channel = client.get_channel(1094649315810611202) message = await channel.fetch_message(1096173976688001134) await channel.send(message.content) print(f'{client.user} is now running!') client.run(TOKEN)
- 报错信息:
2023-04-13 23:30:58 ERROR discord.client Ignoring exception in on_ready Traceback (most recent call last): File "C:\Users\birea\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\client.py", line 441, in _run_event await coro(*args, **kwargs) File "c:\Users\birea\Desktop\Programi\FARKBot\tempCodeRunnerFile.python", line 12, in on_ready await channel.send(message.content) File "C:\Users\birea\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\abc.py", line 1561, in send data = await state.http.send_message(channel.id, params=params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\birea\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\http.py", line 744, in request raise HTTPException(response, data) discord.errors.HTTPException: 400 Bad Request (error code: 50006): Cannot send an empty message
- 额外信息:打印
message.content为空行,打印message输出:
<Message id=1096173976688001134 channel=<TextChannel id=1094649315810611202 name='general' position=0 nsfw=False news=False category_id=1094649315810611200> type=<MessageType.default: 0> author=<User id=1096121542745788426 name='FARKBot' discriminator='0000' bot=True> flags=<MessageFlags value=0>>
问题原因
Discohook发送的Embed消息没有普通文本内容,message.content自然为空。Embed数据存储在message.embeds属性中(这是一个包含所有Embed对象的列表),直接发送空内容会触发Discord API的"不能发送空消息"错误。
解决方法
修改代码,在发送消息时传入embeds参数传递原消息的Embed列表,如需转发附件也可一并处理。
修改后的代码
def runDiscordBot(): TOKEN = "" client = commands.Bot(command_prefix="$", intents = discord.Intents.all()) @client.event async def on_ready(): # 获取来源频道 source_channel = client.get_channel(1094649315810611202) # 获取目标频道(需转发到其他频道时替换为对应ID) target_channel = client.get_channel(1094649315810611202) # 获取指定消息 message = await source_channel.fetch_message(1096173976688001134) # 转发消息,包含文本、Embed和附件 await target_channel.send( content=message.content, embeds=message.embeds, files=[await attachment.to_file() for attachment in message.attachments] ) print(f'{client.user} 已启动运行!') client.run(TOKEN)
代码说明
embeds=message.embeds:将原消息中的所有Embed对象传递给发送方法,确保转发内容包含原Embedfiles参数:通过列表推导式将原消息的附件转换为可发送的文件对象,实现附件同步转发- 来源/目标频道分离:如需转发到不同频道,单独修改
target_channel的ID即可
内容的提问来源于stack exchange,提问作者Ryoschin Lot
相关产品推荐
相关产品推荐

