【问题标题】:set a time limit to rank discord.py设置时间限制来排名 discord.py
【发布时间】:2021-01-25 15:37:09
【问题描述】:

经过多次搜索,我没有找到任何适合我的东西。我想让用户每 30 秒收到一次 xp,但我不知道怎么做。如果所述用户写了一条消息,他们将获得 xp,但如果他们在另一条消息的接下来 30 秒内再次写,他们将不会收到任何 xp。这将持续到 30 秒结束。

@client.event
async def on_message(message):
    if message.author.bot:
        print("Io sono il bot e non posso livellare :(")
        return
    if message.guild.id not in client.msg_dict:
        #print(message.guild.id)
        client.msg_dict[message.guild.id] = {}
        #print(client.msg_dict[message.guild.id])
    #print(message.author.id, client.msg_dict[message.guild.id])
    if message.author.id in client.msg_dict[message.guild.id]:
        #print("Test2")
        if not (time.time() - client.msg_dict[message.guild.id][message.author.id]) > 30:
            #print("Utente bloccato")
            return  # current time - last msg sent time is not > 30
    xp = generateXP()
    print(f"{message.author.name} ha ricevuto {str(xp)} xp")
    cursor = levelsystem_db.cursor()
    cursor.execute(f"SELECT user_xp FROM users WHERE client_id = {str(message.author.id)}")
    result = cursor.fetchall()
    print(result)
    print(len(result))
    if (len(result) == 0):
        print("L'utente non è stato aggiunto al database.")
        cursor.execute(f"INSERT INTO users VALUES({str(message.author.id)} ,{str(xp)} , 0)")
        levelsystem_db.commit()
        print("Aggiunta completata")
        await level_up(cursor, xp, message.author, message)
    else:
        newXP = result[0][0] + xp
        print(f"Gli xp di {message.author.name} sono aggiornati a {newXP}")
        cursor.execute(f"UPDATE users SET user_xp = {str(newXP)} WHERE client_id = {str(message.author.id)}")
        levelsystem_db.commit()
        print(f"Aggiornamento degli xs di {message.author.name} completato.")
        await level_up(cursor, newXP, message.author, message)
def generateXP():
    return random.randint(5,10)

async def level_up(cursor, NewXP, user, message):
    cursor.execute(f"SELECT user_level FROM users WHERE client_id = {str(message.author.id)}")
    level = cursor.fetchall()
    lvl_start = level[0][0]
    lvl_end = int(NewXP ** (1/4))
    print(str(lvl_start))
    print(str(lvl_end))
    if (str(lvl_start) < str(lvl_end)):
        await message.channel.send(f"{user.mention} è salito al livello {lvl_end}")
        print(f"Il livello di {message.author.name} si sta aggiornando al livello {lvl_end}")
        cursor.execute(f"UPDATE users SET user_level = {str(lvl_end)} WHERE client_id = {str(message.author.id)}")
        levelsystem_db.commit()
        print(f"Aggiornamento del livello di {message.author.name} completato.")
    else:
        print("Non è abbastanza!")
        pass

编辑:我绝对需要一些你找不到的答案,或者你找到的答案已经过时了!

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    制作一个包含发送消息的用户 ID 的字典,并在 30 秒后删除这些 ID,并在 on_message 中使用它进行检查

    import time
    
    @client.event
    async def on_ready():
        print("Ready")
        client.msg_dict = {}
    
    @client.event
    async def on_message(message):
        if message.author.bot:
            print("Io sono il bot e non posso livellare :(")
            return
        if message.guild.id not in client.msg_dict:
            client.msg_dict[message.guild.id] = {}
        if message.author.id in client.msg_dict[message.guild.id]:
            if not (time.time() - client.msg_dict[message.guild.id][message.author.id]) > 30:
                return # current time - last msg sent time is not > 30
        client.msg_dict[message.guild.id][message.author.id] = time.time() # change last msg sent time with current time as the user sent a msg now
        xp = generateXP()
        print(f"{message.author.name} ha ricevuto {str(xp)} xp")
        cursor = levelsystem_db.cursor()
        cursor.execute(f"SELECT user_xp FROM users WHERE client_id = {str(message.author.id)}")
        result = cursor.fetchall()
        print(result)
        print(len(result))
        if (len(result) == 0):
            print("L'utente non è stato aggiunto al database.")
            cursor.execute(f"INSERT INTO users VALUES({str(message.author.id)} ,{str(xp)} , 0)")
            levelsystem_db.commit()
            print("Aggiunta completata")
            await level_up(cursor, xp, message.author, message)
        else:
            newXP = result[0][0] + xp
            print(f"Gli xp di {message.author.name} sono aggiornati a {newXP}")
            cursor.execute(f"UPDATE users SET user_xp = {str(newXP)} WHERE client_id = {str(message.author.id)}")
            levelsystem_db.commit()
            print(f"Aggiornamento degli xs di {message.author.name} completato.")
            await level_up(cursor, newXP, message.author, message)
    

    【讨论】:

    • 我试过了。但它具有相反的效果:它在开始时阻止你,然后当 30 秒过去时它不再阻止你并且问题再次出现。
    • 再次尝试,这次没有通过其他两个 if。我注意到它并没有给client.msg_dict[message.guild.id]添加任何东西,结果只是{}
    【解决方案2】:

    我终于找到了使用 MySQL 和 asyncio 库的解决方案。 这是我根据需要应用的脚本

    @client.event
    async def on_message(message):
        if message.author.bot:
            return
        xp = 5
        cursor = levelsystem_db.cursor()
        cursor.execute(f"SELECT message_id FROM anti_spam WHERE client_id = {str(message.author.id)}")
        check_message = cursor.fetchall()
        if (len(check_message) == 0):
            cursor.execute(f"INSERT INTO anti_spam VALUES({str(message.id)}, {str(message.author.id)}, 1)")
            levelsystem_db.commit()
        else:
            print("Non puoi livellare!")
            return
        
        #In between create your ranking system, giving user experience etc.
        
        await asyncio.sleep(60)
        cursor.execute(f"DELETE FROM anti_spam WHERE message_id = {str(message.id)}")
        levelsystem_db.commit()
        await client.process_commands(message)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-03
      • 1970-01-01
      • 2013-04-03
      • 2021-07-06
      • 2011-12-02
      • 2017-03-03
      • 2012-05-08
      • 2011-02-25
      相关资源
      最近更新 更多