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

Discord机器人报错:'Member'对象无'avatar_url_as'属性求助

问题描述

开发Discord机器人的wanted命令时,代码运行报错,错误提示AttributeError: 'Member' object has no attribute 'avatar_url_as'。

原代码

import discord
from discord.ext import commands
import datetime
import asyncio
import random
from config import settings

from PIL import Image
from io import BytesIO


client = commands.Bot(command_prefix = settings['PREFIX'], intents=discord.Intents.all())
client.remove_command('help')

@client.event
async def on_ready():
    # status_task.start()
    print('Ready')

@client.command()
async def wanted(ctx, user: discord.Member = None):
    if user == None:
        user = ctx.author
        
    wanted = Image.open('tester.jpg')

    asset = ctx.author.avatar_url_as(size = 128)
    data = BytesIO(await asset.read())
    pfp = Image.opem(data)

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

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

    wanted.save("profile.jpg")

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

报错信息

Traceback (most recent call last):
  File "C:\Users\Астик\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\bot.py", line 360, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Астик\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 927, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Астик\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 190, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Member' object has no attribute 'avatar_url_as'
解决方案

你的代码存在两个关键问题:

  • avatar_url_as方法已被移除
    discord.py v2.x版本中,Member对象的avatar_url_as方法被移除。现在需要通过以下方式获取用户头像:

    • 用户有自定义头像时,使用user.avatar获取Avatar对象,再调用with_size(尺寸)指定大小
    • 用户没有自定义头像时,回退到默认头像user.default_avatar
  • 拼写错误
    代码中Image.opem(data)是笔误,正确写法为Image.open(data)

修复后的完整代码:

import discord
from discord.ext import commands
import datetime
import asyncio
import random
from config import settings

from PIL import Image
from io import BytesIO


client = commands.Bot(command_prefix = settings['PREFIX'], intents=discord.Intents.all())
client.remove_command('help')

@client.event
async def on_ready():
    # status_task.start()
    print('Ready')

@client.command()
async def wanted(ctx, user: discord.Member = None):
    if user is None:
        user = ctx.author
        
    wanted = Image.open('tester.jpg')

    # 优先获取自定义头像,无自定义头像则用默认头像
    avatar = user.avatar or user.default_avatar
    # 读取指定尺寸的头像数据
    data = BytesIO(await avatar.with_size(128).read())
    pfp = Image.open(data)

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

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

    wanted.save("profile.jpg")

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

额外提示:

  • 建议使用user is None代替user == None,这是Python的规范写法
  • 确保tester.jpg文件位于脚本的同目录下,否则需要填写完整文件路径

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 08:22:50