在 discord.py 中访问成员的横幅

Accessing a member's banner in discord.py

我正在制作图像配置文件命令。我想为此访问会员的横幅。在 discord.py?

中,我们有什么办法可以做到这一点吗?

如果不清楚我所说的横幅是什么意思,蓝色背景的图片就是横幅。我想访问它。

在 discord.py v2.0 中你可以使用

# 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

在 v2.0 之前,有一种直接使用 API

获取横幅的 hacky 方法
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

要安装 discord.py v2.0,您应该 运行

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

完整的命令如下:

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

这是横幅命令的第二次编辑,适用于:nextcord API Wrapper | nextcord's Github

此命令将访问用户的横幅(如果适用)并显示它:

如果没有横幅但分配了强调色,它将显示强调色作为显示的横幅。

如果没有分配强调色或横幅,该命令将向频道发送一条消息,说明用户没有分配 banner/color。