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

使用discord.py rewrite实现斜杠命令发送Embed时提示应用未响应

问题原因及修复方案

你的代码存在两处关键错误,直接导致了“Application did not respond”的提示:

1. 错误调用ctx.message

斜杠命令的上下文对象是discord.Interaction,它没有message属性,你试图通过ctx.message.author获取请求用户的操作会直接抛出异常,导致机器人无法完成回复流程,Discord因此判定应用未响应。

应改用ctx.user获取发起命令的用户:

# 原错误代码
embed.set_footer(text=f"Requested by {ctx.message.author.name}", icon_url=ctx.message.author.avatar)

# 修复后
embed.set_footer(text=f"Requested by {ctx.user.name}", icon_url=ctx.user.avatar.url)

2. Avatar对象未转换为URL

member.avatar和ctx.user.avatar返回的是Avatar对象,而Embed的thumbnail、footer的icon_url需要的是图片直接URL字符串,必须调用.url属性获取正确链接:

# 原错误代码
embed.set_thumbnail(url=member.avatar)

# 修复后
embed.set_thumbnail(url=member.avatar.url)

完整修复后的代码

@bot.tree.command(name="avatar", description="avatar show lol")
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.avatar.url)
    embed.set_footer(text=f"Requested by {ctx.user.name}", icon_url=ctx.user.avatar.url)
    await ctx.response.send_message(embeds=[embed])

@bot.event
async def on_ready():
    try:
        synced = await bot.tree.sync()
        print(f"Synced {len(synced)} commands")
    except Exception as e:
        print(e)
    await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="what"))
    print(f"Bot account: {bot.user}")
    print(f"{discord.__version__} is lib ver")

asyncio.run(main())

额外提示

建议给命令添加异常捕获,避免单个命令报错导致机器人无响应:

@bot.tree.command(name="avatar", description="avatar show lol")
async def avatar(ctx: discord.Interaction, member: discord.Member):
    try:
        embed = discord.Embed(title="Avatar", description=f"{member.mention}'s avatar!", color=discord.Color.random())
        embed.set_thumbnail(url=member.avatar.url)
        embed.set_footer(text=f"Requested by {ctx.user.name}", icon_url=ctx.user.avatar.url)
        await ctx.response.send_message(embeds=[embed])
    except Exception as e:
        print(f"Error in avatar command: {e}")
        await ctx.response.send_message("获取头像时出现错误!", ephemeral=True)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 20:57:25