嵌入的set_image()函数的discord.py怪异问题

时间:2019-02-22 05:29:10

标签: python discord discord.py

一段时间以来,我一直在使用discord.py开发Discord机器人,发现自己经常使用embeds并使用set_image()函数设置其图像来访问各个站点的图像数据。例如: embed.set_image(url='https://img.pokemondb.net/artwork/large/{}.jpg'.format(pokemon.name.lower())) 将图像设置为所请求的神奇宝贝的图像。

我最近的努力是创建一个命令,以显示Minecraft玩家的用户名/皮肤。我的代码:

@bot.group(pass_context = True, aliases = ['minecraft'])
async def mc(ctx):
    if ctx.invoked_subcommand is None:
        embed = discord.Embed(
        title = 'Error!', 
        description = ':x: Please specify a subcommand!',
        color = 10891826
        )
        await bot.say(embed = embed)






@mc.command()
async def head(username = None):
    if username is None:
        embed = discord.Embed(
        title = 'Error!', 
        description = ':x: Head not found! Did you spell the username right? It is case-sensitive.',
        color = 10891826
        )
        await bot.say(embed = embed)
    else:
        http_conn = http.client.HTTPSConnection("api.mojang.com")
        http_conn.request("GET", "/users/profiles/minecraft/" + username,
            headers={'User-Agent':'Minecraft Username -> UUID', 'Content-Type':'application/json'})
        response = http_conn.getresponse().read().decode("utf-8")

        if (not response): 
            return ""

        json_data = json.loads(response)
        try:
            uuid = json_data['id']
        except KeyError as e:
            print("KeyError raised:", e)

        embed = discord.Embed(
        title = 'Head of {}'.format(username),
        color = 53380
        )
        embed.set_image(url = 'https://crafatar.com/avatars/{}.jpg'.format(uuid))
        await bot.say(embed = embed)





@mc.command()
async def skin(username = None):
    if username is None:
        embed = discord.Embed(
        title = 'Error!', 
        description = ':x: Skin not found! Did you spell the username right? It is case-sensitive.',
        color = 10891826
        )
        await bot.say(embed = embed)
    else:

        http_conn = http.client.HTTPSConnection("api.mojang.com")
        http_conn.request("GET", "/users/profiles/minecraft/" + username,
            headers={'User-Agent':'Minecraft Username -> UUID', 'Content-Type':'application/json'})
        response = http_conn.getresponse().read().decode("utf-8")

        if (not response): 
            return ""

        json_data = json.loads(response)
        try:
            uuid = json_data['id']
        except KeyError as e:
            print("KeyError raised:", e)

        embed = discord.Embed(
        title = 'Skin of {}'.format(username),
        color = 53380
        )
        embed.aet_image(url = 'https://crafatar.com/renders/body/{}.jpg'.format(uuid))
        await bot.say(embed = embed)

对于某些用户名,一切正常,但是对于其他用户名,图像根本不显示。有些奇怪的情况甚至对head命令有效,但对skin命令无效(反之亦然)。经过大量实验后,我注意到将扩展名从.jpg替换为.png可以解决无法使用的扩展名的问题,并停止以前可以使用的扩展名。将值手动输入到crafatar站点(具有任何扩展名,甚至根本没有扩展名)都会产生有效的直接图像链接,但是由于某些原因,它不会加载到嵌入中。它不会给我任何错误或异常,因此我无法通过在except语句中将其更改为.png来解决此问题,这让我迷失了方向。如果有人知道为什么会发生这种情况,或者我能做些什么来解决它,我将非常感激。我可能只是个愚蠢的人,但是我花了太长时间了,我在网上挖的都是人们忘记使用kwarg(即set_image('url')而不是set_image(url='url'),这正在使我的大脑受伤了。

注意:我没有使用discord.py的重写版本,并且我不打算将整个bot移植到该版本,所以请不要建议

0 个答案:

没有答案