如何使用interactions.py发送embed?传参报错如何解决
interactions.py发送Embed消息报错修复
问题现象
使用interactions.py开发Discord机器人时,照搬discord.py的Embed发送写法运行报错,报错信息为TypeError: _Context.send() got an unexpected keyword argument 'embed'。
问题代码
@client.command( name="embed", description="Test", scope=[993586870606905404], ) async def embed(ctx: interactions.CommandContext): embed = discord.Embed( title="your title", description="your description", color=discord.Color.random(), timestamp=datetime.datetime.now()) await ctx.send(embed=embed)
完整报错
Traceback (most recent call last): File "/Users/my.computer/bot.py/main.py", line 79, in embed await ctx.send(embed=embed) File "/Users/my.computer/venv/lib/python3.10/site-packages/interactions/client/context.py", line 445, in send payload = await super().send(content, **kwargs) TypeError: _Context.send() got an unexpected keyword argument 'embed'
错误原因
- interactions.py不是discord.py的直接重写版本,两者API设计完全不兼容,不能直接套用discord.py的写法
ctx.send()方法没有embed参数,接收embed的参数名为embeds,要求传入Embed对象组成的列表- 不能使用discord.py提供的
discord.Embed、discord.Color类,interactions.py内置了对应的实现,需要从interactions包导入
修复方案
- 移除代码中discord.py相关的Embed、Color类引用,从interactions包导入对应类
- 将
send()方法的embed参数改为embeds,传入包裹Embed实例的列表
修复后的可运行代码:
import datetime import interactions # 此处省略client初始化逻辑 @client.command( name="embed", description="Test", scope=[993586870606905404], ) async def embed(ctx: interactions.CommandContext): embed = interactions.Embed( title="your title", description="your description", color=interactions.Color.random(), timestamp=datetime.datetime.now() ) await ctx.send(embeds=[embed])
补充说明
- 单次调用
send()最多支持传入10个Embed对象,直接追加到embeds列表即可 - 不要在同一运行环境中同时安装discord.py和interactions.py,极易出现类导入冲突
- 上述写法兼容interactions.py v5稳定版,旧v4版本API存在差异,建议升级到最新版本使用
内容的提问来源于stack exchange,提问作者Ethan Armstrong
相关产品推荐
相关产品推荐

