AttributeError: 'Context' object has no attribute 'guild_icon'

AttributeError: 'Context' object has no attribute 'guild_icon'

我正在尝试设置一个命令来打印机器人所在服务器的信息。下面的代码给出了错误 AttributeError: 'Context' object has no attribute 'guild_icon'

@commands.command()
async def serverinfo(self,ctx):
    name = str(ctx.guild.name)
    description = str(ctx.guild.description)

    owner = str(ctx.guild.owner)
    id = str(ctx.guild.id)
    region = str(ctx.guild.region)
    memberCount = str(ctx.guild.member_count)


    embed = discord.Embed(
        title=name + " Server Information",
        description=description,
        color=discord.Color.blue()
    )
    embed.set_thumbnail(ctx.guild_icon)
    embed.add_field(name="Owner", value=owner, inline=True)
    embed.add_field(name="Server ID", value=id, inline=True)
    embed.add_field(name="Region", value=region, inline=True)
    embed.add_field(name="Member Count", value=memberCount, inline=True)

    await ctx.send(embed=embed)

在寻找解决方案时,我尝试将代码更改为

@commands.command()
async def serverinfo(self,ctx):
    name = str(ctx.guild.name)
    description = str(ctx.guild.description)

    owner = str(ctx.guild.owner)
    id = str(ctx.guild.id)
    region = str(ctx.guild.region)
    memberCount = str(ctx.guild.member_count)


    embed = discord.Embed(
        title=name + " Server Information",
        description=description,
        color=discord.Color.blue()
    )
    embed.set_thumbnail(ctx.guild.icon)
    embed.add_field(name="Owner", value=owner, inline=True)
    embed.add_field(name="Server ID", value=id, inline=True)
    embed.add_field(name="Region", value=region, inline=True)
    embed.add_field(name="Member Count", value=memberCount, inline=True)

    await ctx.send(embed=embed)

但是我得到了错误discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: set_thumbnail() takes 1 positional argument but 2 were given

有人可以告诉我我做错了什么并指出正确的方向吗?

问题 1

ctx.guild_icon 不存在,您在第二个代码中使用 ctx.guild.icon

修复此问题

问题 2

discord.Embed.set_thumbnail 接受图像的命名参数。

事实上,它根本不接收图像,而是 url

解决方案

要解决您的问题,您应该使用以下方法:

embed.set_thumbnail(url=ctx.guild.icon.url)

更多信息

discord.ext.commands.Context

discord.Embed.set_thumbnail