我最近编写了一个水准系统代码,但对它的完成方式不满意,所以我坐下来想到了一个等级命令。如果用户执行; rank,则会显示@user,您的级别为[level]。好吧,我开始对命令进行编码,并得出一个结论:我不知道如何制作一个可以返回用户级别的函数
在这里调平系统的代码
with open('users.json', 'r') as f:
users = json.load(f)
await update_data(users, message.author)
await add_experience(users, message.author, 5)
await level_up(users, message.author, message.channel)
with open('users.json', 'w') as f:
json.dump(users, f)
async def update_data(users, user):
if not user.id in users:
users[user.id] = {}
users[user.id]['experience'] = 0
users[user.id]['level'] = 1
async def add_experience(users, user, exp):
users[user.id]['experience'] += exp
async def level_up(users, user, channel):
experience = users[user.id]['experience']
lvl_start = users[user.id]['level']
lvl_end = int(experience ** (1/4))
if lvl_start < lvl_end:
await bot.send_message(bot.get_channel("493185116554788894"), '{} have leveled up to level {}'.format(user.mention, lvl_end))
users[user.id]['level'] = lvl_end
这是我为等级命令编写的内容
@bot.command(pass_context=True)
async def rank(ctx):
await bot.say(f"{ctx.message.author.mention} Your rank is {lvl_end}.")
我需要一个功能来恢复水平,但是我不知道该怎么做。我希望有人可以帮助我,而且我想它比我认为容易得多。
答案 0 :(得分:1)
在这里,我认为这是您要尝试做的事情:
@bot.command(pass_context=True)
async def rank(ctx):
with open('users.json', 'r') as f:
users = json.load(f)
lvl_end = users[ctx.message.author.id]['rank']
await bot.say(f"{ctx.message.author.mention} Your rank is {lvl_end}.")