因此,我遵循了有关如何为Discord机器人设置调平系统的教程,一切正常,直到遇到了如何为该调平系统增加冷却时间的问题。我该怎么办?
我看过很多不同的不和谐教程,并且尝试使用@ commands.cooldown功能,但似乎没有任何效果。重新格式化任何其他代码也有帮助。
@client.event
async def on_message(message):
if message.author != client.user:
db = sqlite3.connect('main.sqlite')
cursor = db.cursor()
cursor.execute(f"SELECT user_id FROM levels WHERE guild_id = '{message.author.guild.id}' and user_id = '{message.author.id}'")
result = cursor.fetchone()
if result is None:
sql = "INSERT INTO levels(guild_id, user_id, exp, lvl, texp, name) VALUES(?, ?, ?, ?, ?, ?)"
xp = random.randrange(15, 25)
val = (message.author.guild.id, message.author.id, xp, 0, xp, message.author.name)
cursor.execute(sql, val)
db.commit()
cursor.execute(
f"SELECT user_id, exp, lvl, texp FROM levels WHERE guild_id = '{message.author.guild.id}' and user_id = '{message.author.id}'")
result2 = cursor.fetchone()
global texptotal
texptotal = result2[3]
global xp_start
xp_start = int(result2[1])
global lvl_start
lvl_start = int(result2[2])
global xp_end
xp_end = math.floor(5 * (lvl_start ** 2) + 50 * lvl_start + 100)
else:
cursor.execute(f"SELECT user_id, exp, lvl, texp FROM levels WHERE guild_id = '{message.author.guild.id}' and user_id = '{message.author.id}'")
result1 = cursor.fetchone()
exp = int(result1[1])
texp = int(result1[3])
xp = random.randrange(15, 25)
sql = "UPDATE levels SET exp = ?, texp = ? WHERE guild_id = ? and user_id = ?"
val = (exp + xp, texp + xp, str(message.guild.id), str(message.author.id))
cursor.execute(sql, val)
db.commit()
cursor.execute(f"SELECT user_id, exp, lvl, texp FROM levels WHERE guild_id = '{message.author.guild.id}' and user_id = '{message.author.id}'")
result2 = cursor.fetchone()
texptotal = result2[3]
xp_start = int(result2[1])
lvl_start = int(result2[2])
xp_end = math.floor(5 * (lvl_start ** 2) + 50 * lvl_start + 100)
if xp_end <= xp_start:
await message.channel.send(f'{message.author.mention} has leveled up to level {lvl_start + 1}!')
sql = "UPDATE levels SET lvl = ? WHERE guild_id = ? and user_id = ? "
val = (int(lvl_start + 1), str(message.guild.id), str(message.author.id))
cursor.execute(sql, val)
db.commit()
sql = "UPDATE levels SET exp = ? WHERE guild_id = ? and user_id = ?"
val = (0, str(message.guild.id), str(message.author.id))
cursor.execute(sql, val)
db.commit()
cursor.execute(f"SELECT user_id, exp, lvl, texp FROM levels WHERE guild_id = '{message.author.guild.id}' and user_id = '{message.author.id}'")
result2 = cursor.fetchone()
xp_start = int(result2[1])
lvl_start = int(result2[2])
xp_end = 5 * (lvl_start ** 2) + 50 * lvl_start + 100
cursor.close()
db.close()