不和.py使用json的调平系统。我怎样才能在代码中添加这些东西呢?

2024-05-14 06:46:15 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有一个不和谐调平系统的代码,每次用户在服务器上发送一个消息,我的机器人在其中,他们获得随机数量的xp在5到10之间。代码如下:

@bot.event
async def on_member_join(member):
    with open("users.json", "r") as f:
        users = json.load(f)

        await update_data(users, member)

        with open("users.json", "w") as f:
            json.dump(users, f)

@bot.event
async def on_message(message):
    with open("users.json", "r") as f:
        users = json.load(f)

        if message.author.bot:
            return
        else:
            await update_data(users, message.author)
            number = random.randint(5,10)
            await add_experience(users, message.author, number)
            await level_up(users, message.author, message.channel)

        with open("users.json", "w") as f:
            json.dump(users, f)
    await bot.process_commands(message)

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(channel, f":tada: Congrats {user.mention}, you levelled up to level {lvl_end}!")
        users[user.id]["level"] = lvl_end

这很好用,但是我想添加/更改/修改几点来改进它。具体如下:

a)我只希望用户每分钟或每30秒从邮件中获取xp,以防止垃圾邮件水平上升。在

b)我希望它也将服务器放入json文件中,这样每个用户对每台服务器都有一个唯一的级别,因为目前,他们的xp在他们和bot共享的所有服务器上都得到了实现。在

c)目前,如果用户dm是bot,bot还会给用户xp,这显然是我不想要的。如果dm是bot,有没有办法阻止用户获得xp,我不能让bot忽略dm,因为对于某些命令,用户和bot都需要使用它们。在

如有任何帮助,我们将不胜感激。即使你只能回答一个问题,或者给出一个想法或意见,那也太好了。在

谢谢

酋长

编辑:我现在已经回答了我所有的问题,这里是代码,希望这有帮助(请不要复制我的代码,谢谢):

^{pr2}$

Tags: 代码用户idjsonmessageasyncdefbot