Search code examples
discordpycord

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


I am trying to set up a command to print the information of the server the bot is in. The code below gives the error 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)

When trying to find the solution I tried to change the code to be

@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)

But then I get the error discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: set_thumbnail() takes 1 positional argument but 2 were given

Could someone tell me what I have done wrong and point me in the right direction?


Solution

  • Problem 1

    ctx.guild_icon does not exist, you fix this in your second code by using ctx.guild.icon

    Problem 2

    discord.Embed.set_thumbnail takes in a named argument for the image.

    In fact, it doesn't take in an image at all, but rather a url

    Solution

    To solve your issue, you should use the following:

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

    More info

    discord.ext.commands.Context

    discord.Embed.set_thumbnail