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

discord.py头像叠加命令@其他用户时失效,如何修复?

修复Discord机器人获取提及用户头像的问题

你的代码核心问题是:虽然正确接收了user参数,但实际获取头像时仍固定使用ctx.author(命令发送者)的头像,没有使用传入的user变量。

只需修改获取头像的那一行代码,把ctx.author.avatar替换为user.avatar即可:

intents = discord.Intents.all()

intents.members = True
bot = commands.Bot(command_prefix= '$', intents=intents)

@bot.command()
async def avatartest(ctx, user: discord.Member = None):
    if user == None:
        user = ctx.author

    overlay = Image.open('the.png')

    # 替换为user的头像
    asset = user.avatar
    data = BytesIO(await asset.read())
    pfp = Image.open(data)

    pfp = pfp.resize((177,177))

    overlay.paste(pfp, (120,212))

    overlay.save("profile.png")

    await ctx.send(file = discord.File("profile.png"))

bot.run(tokenbot)    

修改后,当你执行$avatartest @NotSoBot#9555时,机器人就会获取被提及用户的头像进行叠加;如果不提及用户,仍会默认使用命令发送者的头像,符合你的需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 13:30:42