如何通过Slash Command创建Embed?解决ctx类型注解报错问题
Discord斜杠命令报错解决与交互对象说明
报错原因与修复
你遇到的TypeError是因为类型注解使用错误:
- 代码里的
interactions.CommandContext并非discord.py库的标准类型,discord.py的斜杠命令(App Command)回调参数必须使用discord.Interaction作为类型注解。 - 同时,斜杠命令不能用前缀命令的
ctx.send方法发送消息,必须通过Interaction对象的响应方法。
修正后的完整代码:
import discord from discord import app_commands # 确保bot已正确初始化(示例) # bot = discord.Bot(intents=discord.Intents.default()) @bot.tree.command(name="유저2") async def information2(interaction: discord.Interaction): embed = discord.Embed(title="title", description='description', color=discord.Color.green()) # 斜杠命令必须用interaction的响应方法发送消息 await interaction.response.send_message(embed=embed)
Interaction与Ctx的区别及用法
commands.Context(简称ctx):仅用于前缀命令(比如!유저2这类带前缀的命令),是传统命令的上下文对象,包含命令触发者、频道、服务器等信息。discord.Interaction:仅用于斜杠命令/交互组件(比如按钮、选择菜单),是Discord交互系统的核心对象,负责处理用户与机器人的交互请求,发送响应必须通过它的专属方法(如response.send_message、followup.send)。- 两者不能混用:斜杠命令的回调函数必须接收
Interaction参数,前缀命令必须接收Context参数,否则会触发类型错误或功能异常。
内容的提问来源于stack exchange,提问作者뮤직마크
相关产品推荐
相关产品推荐

