From 6758555ade91623852647e7efcb162025a3ae518 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 28 Apr 2017 19:19:16 +0200 Subject: [PATCH] moar db stuff --- database.py | 4 ++++ royalbot.py | 21 ++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/database.py b/database.py index a9864f15..62cbb7b9 100644 --- a/database.py +++ b/database.py @@ -106,16 +106,20 @@ def new_diario_entry(dt, text): session.commit() +# TODO: this can be moved to a method of the LoL class async def update_lol(discord_id): # Create a new database session session = Session() # Find the user user = session.query(Account).filter_by(id=discord_id).join(LoL).first() + # TODO: ewww for account in user.lol: # Find the League of Legends ID lid = account.id # Poll the League API for more information data = await lol.get_summoner_data("euw", summoner_id=lid) + # Change tracker: if anything meaningful changes, set this to True + changes = False # Update the user data account.summoner_name = data["name"] account.level = data["summonerLevel"] diff --git a/royalbot.py b/royalbot.py index 07dc034f..635538db 100644 --- a/royalbot.py +++ b/royalbot.py @@ -287,7 +287,23 @@ Sintassi: `{symbol}synclol `""" # Update the newly added user await database.update_lol(thing.author.id) # Send some info to Discord - await d.client.send_message(thing.channel, embed=lolaccount.generate_discord_embed()) + await d.client.send_message(thing.channel, "Successfully connected: ", embed=lolaccount.generate_discord_embed()) + + +async def job_updatelol(singletimeout=1, alltimeout=300): + await d.client.wait_until_ready() + while True: + # Open a new database session + session = database.Session() + # Query all the LoL accounts + users = session.query(database.LoL).all() + # Update all the users' stats + for user in users: + await database.update_lol(user.parent_id) + await asyncio.sleep(singletimeout) + await asyncio.sleep(alltimeout) + + if __name__ == "__main__": # Init universal bot commands @@ -307,5 +323,8 @@ if __name__ == "__main__": # Init Discord bot loop.create_task(d.run()) print("Discord bot start scheduled!") + # Init LoL stats updater + loop.create_task(job_updatelol()) + print("LoL database updater scheduled!") # Run everything! loop.run_forever()