使用interactions库的Discord Bot发送Embed报错Invalid embeds求解决
问题:使用interactions库发送Discord Embed时提示无效
我正在用interactions库开发Discord交互机器人,但无法发送Embed消息,代码片段和导入语句如下:
import interactions import discord from interactions import ComponentContext, GuildChannel, OptionType, StringSelectMenu, ActionRow, slash_command, SlashContext, slash_option, Button, ButtonStyle, component_callback, spread_to_rows from interactions.api.events import Component bot = interactions.Client() @component_callback("orderSelect") async def callbackOrderSelect(ctx: ComponentContext): embed1 = discord.Embed(title="title") embed1.add_field(name="embed") await ctx.defer(ephemeral=True) await ctx.send(embed=embed1)
机器人发送消息时抛出错误:
ValueError: Invalid embeds: <discord.embeds.Embed object at 0x000001F69DD063B0>
完整错误堆栈:
Ignoring exception in Component Callback for orderSelect: Traceback (most recent call last): File "C:\Users\HP\AppData\Local\Programs\Python\Python311\Lib\site-packages\interactions\client\client.py", line 1818, in __dispatch_interaction response = await callback ^^^^^^^^^^^^^^ File "C:\Users\HP\AppData\Local\Programs\Python\Python311\Lib\site-packages\interactions\models\internal\command.py", line 132, in __call__ await self.call_callback(self.callback, context) File "C:\Users\HP\AppData\Local\Programs\Python\Python311\Lib\site-packages\interactions\models\internal\command.py", line 198, in call_callback await self.call_with_binding(callback, context, **context.kwargs) # type: ignore ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\HP\AppData\Local\Programs\Python\Python311\Lib\site-packages\interactions\models\internal\callback.py", line 43, in call_with_binding return await callback(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users\HP\Desktop\Ruslaninator\kuznya\main_v2.py", line 85, in callbackOrderSelect await ctx.send(embed=discord.Embed(title="title")) File "C:\Users\HP\AppData\Local\Programs\Python\Python311\Lib\site-packages\interactions\models\internal\context.py", line 498, in send return await super().send( ^^^^^^^^^^^^^^^^^^^ File "C:\Users\HP\AppData\Local\Programs\Python\Python311\Lib\site-packages\interactions\client\mixins\send.py", line 85, in send message_payload = models.discord.message.process_message_payload( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\HP\AppData\Local\Programs\Python\Python311\Lib\site-packages\interactions\models\discord\message.py", line 933, in process_message_payload embeds = process_embeds(embeds) ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\HP\AppData\Local\Programs\Python\Python311\Lib\site-packages\interactions\models\discord\embed.py", line 489, in process_embeds raise ValueError(f"Invalid embeds: {embeds}") ValueError: Invalid embeds: <discord.embeds.Embed object at 0x000001EF02295A20>
问题原因
你混用了discord.py和interactions两个库的Embed类。interactions库有自己独立的Embed实现,它无法识别discord.py的discord.Embed对象,因此判定为无效。
你的另一个项目能正常工作,是因为那个项目基于纯discord.py开发,自然兼容discord.Embed。
解决方法
- 调整导入语句:移除
import discord(如果没有用到discord.py的其他功能),并从interactions库导入Embed类 - 替换Embed实例:将代码中的
discord.Embed全部替换为interactions.Embed
修改后的代码示例:
import interactions from interactions import ComponentContext, GuildChannel, OptionType, StringSelectMenu, ActionRow, slash_command, SlashContext, slash_option, Button, ButtonStyle, component_callback, spread_to_rows, Embed # 新增Embed导入 from interactions.api.events import Component bot = interactions.Client() @component_callback("orderSelect") async def callbackOrderSelect(ctx: ComponentContext): embed1 = interactions.Embed(title="title") embed1.add_field(name="embed", value="字段内容") # 注意:add_field需要name和value参数,原代码缺少value会报错 await ctx.defer(ephemeral=True) await ctx.send(embed=embed1)
额外注意:interactions的add_field方法要求必须传入name和value两个参数,原代码只传了name,即使解决Embed兼容问题后也会触发新错误,所以需要补充value参数。
内容的提问来源于stack exchange,提问作者Nktz_incor
相关产品推荐
相关产品推荐

