【问题标题】:Accessing a member's banner in discord.py在 discord.py 中访问成员的横幅
【发布时间】:2021-10-11 20:00:29
【问题描述】:

我正在制作图像配置文件命令。我想为此访问会员的横幅。有什么方法可以在 discord.py 中做到这一点?

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

【问题讨论】:

    标签: python image-processing discord discord.py


    【解决方案1】:

    在 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之前,有一种hacky方式可以直接使用API​​获取banner

    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
    

    【讨论】:

    • 你能告诉我如何升级和检查我的 discord,py 版本吗?
    • pip install -U git+https://github.com/Rapptz/discord.py
    • 谢谢帮助,但我想做的是如果一个人没有横幅,那么我想获得默认颜色。反正我能做到吗?
    • 据我所知没有
    • Hey @NishilSheth enhanced-discord.pypycordnextcord 以及更多支持 User.accent_color 现在获得默认颜色之一。如果您正在使用任何叉子,您可能想要使用它
    【解决方案2】:

    完整的命令如下:

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

    【讨论】:

      【解决方案3】:
      @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但分配了强调色,则将强调色显示为显示的banner。

      如果没有指定强调色或横幅,该命令将向频道发送一条消息,说明用户没有指定的横幅/颜色。

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 2021-05-10
      • 2021-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-06
      • 2019-10-21
      • 1970-01-01
      相关资源
      最近更新 更多