После нескольких поисков я не нашел ничего, что подходило бы мне. Я бы хотел, чтобы пользователи получали XP каждые 30 секунд, но я не знаю как. Если указанный пользователь напишет сообщение, он получит опыт, но если он напишет снова в течение следующих 30 секунд после другого сообщения, он не получит никакого опыта. Это будет происходить до истечения 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
РЕДАКТИРОВАТЬ: Мне абсолютно нужны ответы, которые вы не можете найти, или те, которые вы находите, устарели!