Get Guild And Member Icon discord.py

Viewed 464

so i have been trying to make a serverinfo commands but it always give me an error. My code:

@bot.command(name='ServerInfo', description='Shows Server Info')
async def serverinfo(ctx):
  member = len(ctx.guild.members)
  Roles = len(ctx.guild.roles)
  embed = nextcord.Embed(title='**Information:**')
  embed.add_field(name='Name:', value=ctx.guild.name, inline=False)
  embed.add_field(name='ID:', value=ctx.guild.id, inline=False)
  embed.add_field(name='Owner:', value=ctx.guild.owner_id, inline=False)
  embed.add_field(name='Created At:', value=ctx.guild.created_at.strftime('Day: %d/%m/%Y Hour: %H:%M:%S %p'), inline=False)
  embed.add_field(name='Region:', value=ctx.guild.region, inline=False)
  embed.add_field(name='Total Members:', value=f'`{member}`', inline=False)
  embed.add_field(name=f'Total Roles:', value=f'`{Roles}`', inline=False)
  embed.set_thumbnail(url=ctx.guild.icon_url)
  embed.set_author(name=ctx.author, icon_url=ctx.author.avatar_url)
  embed.timestamp = datetime.datetime.utcnow()
  await ctx.send(embed=embed)

Here is the error:

    raise CommandInvokeError(exc) from exc
nextcord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Guild' object has no attribute 'icon_url'

The same thing happens with ctx.author.avatar_url. Can anyone help? Thanks!

1 Answers

If you are on the new discord.py 2.0 alpha or any of the new forks, icon_url has changed to icon.url and avatar_url is now avatar.url.

So:

embed.set_thumbnail(url=ctx.guild.icon.url)
embed.set_author(name=ctx.author, icon_url=ctx.author.avatar.url)

Here's an overview of some more breaking changes: https://gist.github.com/apple502j/f75b4f24652f04de85c7084ffd73ec58

Related