登录
首页 >  文章 >  python教程

Discord.pyEmbed发送失败解决方法

时间:2026-03-03 18:24:46 310浏览 收藏

Discord.py 2.0+ 中 slash 命令发送 embed 失败导致“Application did not respond”错误,往往只因一个关键细节:误将 `embed=embed` 写成 `embeds=[embed]`——因为 `send_message()` 接收的是单个 embed 对象而非列表;本文直击这一高频陷阱,同步厘清 ctx.message 不可用、必须改用 `ctx.user.display_avatar.url`、命令需主动同步、超时前须 `defer()` 等核心实践要点,并附可落地的代码示例与异常处理方案,助你快速排除故障、构建稳定专业的交互式嵌入响应。

Discord.py Slash 命令中 Embed 发送失败的解决方案

Discord.py 2.0+ 中使用 slash 命令发送 embed 时,若误用 `embeds=[embed]`(列表形式)而非 `embed=embed`(单个对象),将导致“Application did not respond”错误;本文详解正确用法、常见陷阱及完整实践要点。

在 Discord.py 2.0+(即 discord.py rewrite 分支,现为官方主版本)中,slash 命令通过 Interaction.response.send_message() 返回响应。与传统消息发送不同,send_message() 方法接收的是单个 embed 参数,而非 embeds(复数)列表——这是引发 “Application did not respond” 错误的最常见原因。

该错误本质是:Discord API 在收到无效参数后拒绝处理请求,而 discord.py 未及时捕获或重试,导致交互超时(默认 3 秒内无响应即显示此提示)。

✅ 正确写法如下(关键修正已加粗):

@bot.tree.command(name="avatar", description="Show a member's avatar")
async def avatar(ctx: discord.Interaction, member: discord.Member):
    embed = discord.Embed(
        title="Avatar",
        description=f"{member.mention}'s avatar!",
        color=discord.Color.random()
    )
    embed.set_thumbnail(url=member.display_avatar.url)  # ✅ 使用 display_avatar 更健壮
    embed.set_footer(
        text=f"Requested by {ctx.user.name}", 
        icon_url=ctx.user.display_avatar.url
    )
    await ctx.response.send_message(embed=embed)  # ✅ 注意:embed=embed(非 embeds=[embed])

⚠️ 同时需注意以下关键细节:

  • ctx.message 在 slash 命令中不可用:ctx.message.author 和 ctx.message.author.avatar 会引发 AttributeError。应改用 ctx.user(代表触发命令的用户),并推荐使用 .display_avatar.url(兼容用户/服务器头像,且始终返回有效 URL)。
  • member.avatar 已弃用:请改用 member.display_avatar.url,避免因用户未设置头像导致 None 引发异常。
  • 确保命令已同步:你的 on_ready 中调用了 bot.tree.sync(),这是必需的,但建议添加 guild= 参数进行测试环境定向同步,避免全量同步延迟影响开发调试。
  • 响应必须在 3 秒内发出:若逻辑复杂(如网络请求),应先调用 await ctx.response.defer(),再执行耗时操作,最后用 await ctx.followup.send(...) 补充 embed。

? 进阶建议:为提升健壮性,可添加异常处理:

@avatar.error
async def avatar_error(ctx: discord.Interaction, error: Exception):
    if isinstance(error, discord.app_commands.MissingPermissions):
        await ctx.response.send_message("❌ 没有权限获取该成员信息。", ephemeral=True)
    else:
        await ctx.response.send_message("❌ 发生错误,请稍后重试。", ephemeral=True)
    print(f"[ERROR] /avatar: {error}")

总结:解决 “Application did not respond”,核心是——用 embed=... 而非 embeds=[...],弃用 ctx.message 改用 ctx.user,优先调用 .display_avatar.url,并在必要时合理使用 defer()。遵循这些规范,即可稳定、专业地构建 slash 命令嵌入式响应。

好了,本文到此结束,带大家了解了《Discord.pyEmbed发送失败解决方法》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多文章知识!

资料下载
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>