我正在尝试使用 PIL 和 discord.py 更新排名
Posted
技术标签:
【中文标题】我正在尝试使用 PIL 和 discord.py 更新排名【英文标题】:I am trying to make a rank card using PIL and discord.py UPDATED 【发布时间】:2021-07-12 18:01:39 【问题描述】:我有一个等级系统,使用 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
【问题讨论】:
从json文件中获取lvl和xp值后定义text
。
【参考方案1】:
您必须首先在文本函数中定义lvl
和exp
或global
,因为找不到。
@bot.command()
async def text(ctx):
lvl = ...
exp = ...
text = [f"level lvl, exp exp"]
user = ctx.message.author
或
@bot.command(aliases = ['rank','lvl'])
async def level(ctx,member: discord.Member = None):
global lvl, exp
if not member:
user = ctx.message.author
with open('level.json','r') as f:
【讨论】:
我已经编辑了这个东西,但它仍然无法正常工作。请检查我的原始问题以上是关于我正在尝试使用 PIL 和 discord.py 更新排名的主要内容,如果未能解决你的问题,请参考以下文章