gpt4 book ai didi

python - 如何制作一个返回用户级别的函数?

转载 作者:行者123 更新时间:2023-12-01 09:00:54 25 4
gpt4 key购买 nike

我最近编写了一个等级系统,但我对它的完成方式并不满意,所以我坐下来思考等级命令。如果用户进行 ;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}.")

我需要一个函数来返回关卡,但我不知道该怎么做。我希望有人能帮助我,我想这比我想象的要容易得多,但我不知道。

最佳答案

在这里,我认为这就是您想要做的:

@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}.")

关于python - 如何制作一个返回用户级别的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52461963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com