Accessing a member's banner in discord.py

Viewed 3879

I am making an image profile command. I would like to access the member's banner for this. Is there any way we can do that in discord.py?

Banner

If it is not clear what i mean by banner then, the image with the blue background is the banner. I would like to access that.

3 Answers

In discord.py v2.0 you can use

# You may have to re-fetch the user for the banner to work properly
user = await bot.fetch_user(user.id)
banner_url = user.banner.url # The URL of the banner

Before v2.0, there is a hacky way to get the banner by directly using the API

req = await bot.http.request(discord.http.Route("GET", "/users/{uid}", uid=user.id))
banner_id = req["banner"]
if banner_id:
    banner_url = f"https://cdn.discordapp.com/banners/{user.id}/{banner_id}?size=1024"
else:
    # The user doesn't have a banner, do what you want
    # In many forks, User.accent_color exists so you
    # may want to check if your library supports that first
    pass

To install discord.py v2.0 you should run

pip install -U git+https://github.com/Rapptz/discord.py

The full command would be like:

@bot.command()
async def banner(ctx, user:discord.Member):
    if user == None:
        user = ctx.author
    req = await bot.http.request(discord.http.Route("GET", "/users/{uid}", uid=user.id))
    banner_id = req["banner"]
    # If statement because the user may not have a banner
    if banner_id:
        banner_url = f"https://cdn.discordapp.com/banners/{user.id}/{banner_id}?size=1024"
    await ctx.send(f"{banner_url}")
@bot.command(pass_context=True, aliases= ['bn'])
async def banner(ctx, *, user:commands.UserConverter=None):
    if user == None:
        member = ctx.author
    else:
        member = user
    usr = await bot.fetch_user(member.id)
    if usr.banner:
        banner = usr.banner.url
        bannerEmbed=nextcord.Embed(
            title='',
            description=f"**[banner]({banner})**",
            color=0x000001
        )
        bannerEmbed.set_author(name=f'{usr.name}#{usr.discriminator}', icon_url=usr.display_avatar.url)
        bannerEmbed.set_image(url=banner)
        await ctx.send(embed=bannerEmbed)
    elif usr.accent_color:
        
        uc = str(usr.accent_color).format(hex).strip('#')
        
        colorEmbed=nextcord.Embed(
            title='',
            description=f"**[banner]({f'https://singlecolorimage.com/get/{uc}/400x100'})**",
            color=0x000001
        )
        
        colorEmbed.set_author(name=f'{usr.name}#{usr.discriminator}', icon_url=usr.display_avatar.url)
        colorEmbed.set_image(url=f'https://singlecolorimage.com/get/{uc}/400x100')
        await ctx.send(embed=colorEmbed)
    else:
        bnerrEmbed=nextcord.Embed(
            title='',
            description='banner/color not assigned',
            color=0x000001
        )
        bnerrEmbed.set_author(name=f'{usr.name}#{usr.discriminator}', icon_url=usr.display_avatar.url)
        await ctx.send(embed=bnerrEmbed)
@banner.error
async def need_mention(ctx, error):
    if isinstance(error, UserNotFound):
        await ctx.send("user not found")
    else:
        print(error)

Here is the second edit for the banner command that works with: nextcord API Wrapper | nextcord's Github

This command will access the user's banner (if applicable) and display it:

If there is no banner but an accent color is assigned, it will display the accent color as the displayed banner.

If no accent color or banner is assigned, the command will send a message to the channel stating that the user has no assigned banner/color.

Related