[Python] How to update a database using a loop?

Discussão em 'Python' iniciado por Stack, Setembro 25, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to get my bot to check the database every second, and if someone has 800 XP, to change their rank to '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)


    But when I run the code, I get this error.

    TypeError: loop() missing 1 required positional argument: 'ctx'

    Continue reading...

Compartilhe esta Página