You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.py中Slash命令调用的Embed无法添加作者等元素求助

Discord.py Slash命令Embed元素不生效的解决方法

你的问题核心是错误调用了类方法而非实例方法,导致Embed的作者、URL等配置没有应用到实际要发送的对象上。以下是修正后的代码及说明:

修正后的完整代码

# commands
@tree.command(name="name", description="description", guild=discord.Object(id=guild_id))
async def twitter(interaction):
    # 初始化Embed对象,可直接指定标题、描述、颜色和跳转URL
    emb = discord.Embed(
        title="自定义标题",
        description="自定义描述内容",
        color=discord.Color.blue(),
        url="https://example.com"  # 点击标题跳转的链接
    )
    
    # 设置作者信息(调用实例的set_author方法)
    emb.set_author(
        name="作者昵称",
        icon_url="https://example.com/avatar.png",  # 作者头像链接
        url="https://example.com/author-page"  # 点击作者名称跳转的链接
    )
    
    # 添加缩略图
    emb.set_thumbnail(url="https://example.com/thumbnail.png")
    
    # 设置页脚
    emb.set_footer(
        text="页脚提示文本",
        icon_url="https://example.com/footer-icon.png"  # 页脚左侧小图标
    )
    
    await interaction.response.send_message(embed=emb, ephemeral=True)

关键修正点说明

  • 所有Embed配置方法(set_author、set_thumbnail、set_footer等)必须调用你创建的Embed实例(如代码中的emb)的方法,而非直接调用discord.Embed类方法。
  • 颜色参数建议使用discord.Color提供的预设方法(如discord.Color.blue()),避免因自定义blue变量未定义引发报错。
  • Embed的标题跳转URL可在初始化时通过url参数直接设置,无需额外方法。
  • 各配置方法的参数(如icon_url、url)均为可选,可根据实际需求选择性传入。

内容的提问来源于stack exchange,提问作者Wishrito

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 07:42:36