【发布时间】: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