首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >获取错误:'str‘对象没有属性'url’discord.py

获取错误:'str‘对象没有属性'url’discord.py
EN

Stack Overflow用户
提问于 2022-09-22 13:45:49
回答 1查看 69关注 0票数 0

因此,我试图制造一个不和谐的机器人,它用定制的图像向进入服务器的用户发送欢迎信息。我试着在网上搜索,但没有找到任何答案。如果你知道解决这个问题的方法,请写。

这是我得到的错误:

代码语言:javascript
运行
复制
Message='str' object has no attribute 'url'
  Source="(filepath)"
  StackTrace:
  File "(filepath)", line 32, in on_member_join (Current frame)
    profile_image = await load_image_async(str(member.avatar.url))
  File "(filepath)", line 93, in <module>
    bot.run('(bot_token)')

这是我的代码:

代码语言:javascript
运行
复制
import discord
from discord.ext import commands
from easy_pil import Editor, load_image_async, Font



bot = commands.Bot(command_prefix='!', intents = discord.Intents.all())
@bot.event
async def on_ready():
    print('logged in problemless as {0.user}'.format(bot))


@bot.event
async def on_member_join(member):
    channel = member.guild.system_channel
    background = Editor("background.jpg")
    profile_image = await load_image_async(str(member.avatar.url))

    profile = Editor(profile_image).resize((150,150)).circle_image()
    poppins = Font.poppins(size=50, variant="bold")

    poppins_small = Font.poppins(size=20, variant ="light")
    
    background.paste(profile, (325,90))
    background.ellipse((325, 90), 150, 150, outline="white",stroke_width=5)

    background.text((400, 260), f"Welcome {member.guild.name}", color="white", font=poppins, align="center")
    background.text((400,325), f"{member.name}#{member.discriminator}", color="white", font=poppins_small,align="center")

    file = discord.File(fp=background.image_bytes, filename="background.jpg")
    await channel.send(f"Hello {member.mention}! Welcome To **{member.guild.name}**")
    await channel.send(file=file)
bot.run('(token)')
EN

Stack Overflow用户

发布于 2022-09-26 21:15:53

代码中的member.avatar.url部分没有什么问题,只是缺少了一个if the user does not have an avatar, do:的条件,这就是错误报告的内容。

您可以通过包含如下条件的if条件来解决这个问题:

代码语言:javascript
运行
复制
default_avatar = 'https://cdn.discordapp.com/embed/avatars/1.png'

profile_image = await load_image_async(member.avatar.url) if member.avatar else await load_image_async(default_avatar)

代码语言:javascript
运行
复制
default_avatar = 'https://cdn.discordapp.com/embed/avatars/1.png'

if member.avatar:
    profile_image = await load_image_async(member.avatar.url)
else:
    profile_image = await load_image_async(default_avatar)

至于代码的图像编辑部分,我无法验证。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73815783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档