如何使用循环更新数据库?
尝试让机器人每秒检查一次数据库,如果有人有 800 XP,就把他们的等级改成'Elite Genin'。
@bot.event
async def on_ready():
print(f"Logged in as: {bot.user.name}")
if not loop.is_running():
loop.start()
@tasks.loop(seconds = 1)
async def loop(ctx):
guild = ctx.guild.id
table = "PEOPLE_" + str(guild)
try:
connection = mysql.connector.connect(
host="localhost",
port="3306",
user="root",
password="root",
database="naruto_game"
)
cursor = connection.cursor()
except mysql.connector.Error as error:
print("Failed to find name: {}".format(error))
finally:
print(f"Logged in as: {bot.user.name}")
sql_update_query = "UPDATE " + table + "SET rank = 'Elite Genin' where xp = 800"
cursor.execute(sql_update_query)
错误提示:
TypeError: loop() missing 1 required positional argument: 'ctx'