我正在尝试使用PIL和discord.py制作等级卡

我正在尝试使用PIL和discord.py制作等级卡,discord.py,python-imaging-library,Discord.py,Python Imaging Library,我有一个升级系统,使用discord.py,我试图制作一张等级卡,比如mee6和奥术。我想得到的图像显示用户排名和xp 这是一个有效的命令,但我想把它变成一个图像 @bot.command(aliases = ['rank','lvl']) async def level(ctx,member: discord.Member = None): if not member: user = ctx.message.author with open('leve

我有一个升级系统,使用discord.py,我试图制作一张等级卡,比如mee6和奥术。我想得到的图像显示用户排名和xp

这是一个有效的命令,但我想把它变成一个图像

@bot.command(aliases = ['rank','lvl'])
async def level(ctx,member: discord.Member = None):

    if not member:
        user = ctx.message.author
        with open('level.json','r') as f:
            users = json.load(f)
        lvl = users[str(ctx.guild.id)][str(user.id)]['level']
        exp = users[str(ctx.guild.id)][str(user.id)]['experience']

        embed = discord.Embed(title = 'Level {}'.format(lvl), description = f"{exp} XP " ,color = discord.Color.green())
        embed.set_author(name = ctx.author, icon_url = ctx.author.avatar_url)
        await ctx.send(embed = embed)
    else:
      with open('level.json','r') as f:
          users = json.load(f)
      lvl = users[str(ctx.guild.id)][str(member.id)]['level']
      exp = users[str(ctx.guild.id)][str(member.id)]['experience']
      embed = discord.Embed(title = 'Level {}'.format(lvl), description = f"{exp} XP" ,color = discord.Color.green())
      embed.set_author(name = member, icon_url = member.avatar_url)

      await ctx.send(embed = embed)
这是我的烂点心

@bot.command()
async def text(ctx):

    text = [f"level {lvl}, exp {exp}"]
    user = ctx.message.author
    img = Image.open("rank.jpg")
    with open('level.json','r') as f:
        users = json.load(f)
    lvl = users[str(ctx.guild.id)][str(user.id)]['level']
    exp = users[str(ctx.guild.id)][str(user.id)]['experience']
    text = ['Level {}'.format(lvl)]

    draw = ImageDraw.Draw(img)
    font = ImageFont.truetype("font.ttf", 150)

    draw.text((480,445), text, (0, 0, 0,), font=font)

    img.save("rankcard.jpg")

    await ctx.send(file = discord.File("rankcard.jpg"))
这里是控制台错误,我怀疑我的代码只有一个错误

Ignoring exception in command text:
Traceback (most recent call last):
  File "C:\Users\Jack\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\Jack\Desktop\just some code\bot.py", line 33, in text
    text = [f"level {lvl}, exp {exp}"]
UnboundLocalError: local variable 'lvl' referenced before assignment

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Jack\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Jack\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Jack\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: UnboundLocalError: local variable 'lvl' referenced before assignment
如果你能帮我修改一下密码。我会非常高兴的。 -谢谢

更新:我现在已经解决了这些问题,但仍在努力解决这个问题,我的代码:

@bot.command()
async def test(ctx,member: discord.Member = None):
    global lvl, exp

    if not member:
        user = ctx.message.author
        with open('level.json','r') as f:
            users = json.load(f)
    img = Image.open("rank.jpg")
    lvl = users[str(ctx.guild.id)][str(user.id)]['level']
    exp = users[str(ctx.guild.id)][str(user.id)]['experience']
    text = [f"level {lvl}, exp {exp}"]

    draw = ImageDraw.Draw(img)
    font = ImageFont.truetype("font.ttf", 150)

    draw.text((480,445), text, (0, 0, 0,), font=font)

    img.save("rankcard.jpg")

    await ctx.send(file = discord.File("rankcard.jpg"))
我的错误是:

Traceback (most recent call last):
  File "C:\Users\Jack\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Jack\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Jack\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: expected string

您必须首先在文本函数或
global
中定义
lvl
exp
,因为找不到它

@bot.command()
异步定义文本(ctx):
lvl=。。。
exp=。。。
text=[f“level{lvl},exp{exp}”]
user=ctx.message.author

@bot.command(别名=['rank','lvl'])
异步def级别(ctx,成员:discord.member=None):
全球lvl,exp
如果不是成员:
user=ctx.message.author
将open('level.json','r')作为f:

Define
text
在从json文件获取lvl和xp值后。我已经编辑了这个东西,但它仍然不起作用。请检查我的原始问题