【发布时间】:2021-10-11 20:00:29
【问题描述】:
【问题讨论】:
标签: python image-processing discord discord.py
【问题讨论】:
标签: python image-processing discord 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之前,有一种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
【讨论】:
pip install -U git+https://github.com/Rapptz/discord.py
enhanced-discord.py、pycord、nextcord 以及更多支持 User.accent_color 现在获得默认颜色之一。如果您正在使用任何叉子,您可能想要使用它
完整的命令如下:
@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但分配了强调色,则将强调色显示为显示的banner。
如果没有指定强调色或横幅,该命令将向频道发送一条消息,说明用户没有指定的横幅/颜色。
【讨论】: