diff --git a/database.py b/database.py index c2bb0f8c..b539e667 100644 --- a/database.py +++ b/database.py @@ -89,39 +89,41 @@ def new_diario_entry(dt, text): session.commit() -# TODO: improve this -async def update_lol(lid): +async def update_lol(discord_id): # Create a new database session session = Session() # Find the user - user = session.query(Account).join(LoL).filter_by(id=lid).first() - # Poll the League API for more information - data = await lol.get_summoner_data("euw", summoner_id=user.lol.id) - # Update the user data - user.lol.summoner_name = data["name"] - user.lol.level = data["level"] - # Poll the League API for ranked data - soloq, flexq, ttq = await lol.get_rank_data("euw", lid) - # Update the user data - if soloq is not None: - user.lol.soloq_tier = lol.tiers[soloq["tier"]] - user.lol.soloq_division = lol.divisions[soloq["entries"][0]["division"]] - else: - user.lol.soloq_tier = None - user.lol.soloq_division = None - if flexq is not None: - user.lol.flexq_tier = lol.tiers[flexq["tier"]] - user.lol.flexq_division = lol.divisions[flexq["entries"][0]["division"]] - else: - user.lol.flexq_tier = None - user.lol.flexq_division = None - if ttq is not None: - user.lol.ttq_tier = lol.tiers[ttq["tier"]] - user.lol.ttq_division = lol.divisions[ttq["entries"][0]["division"]] - else: - user.lol.ttq_tier = None - user.lol.ttq_division = None - # Mark the user as updated - user.lol.last_updated = datetime.datetime.now() + user = session.query(Account).filter_by(id=discord_id).join(LoL).first() + 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) + # Update the user data + account.summoner_name = data["name"] + account.level = data["summonerLevel"] + # Poll the League API for ranked data + soloq, flexq, ttq = await lol.get_rank_data("euw", lid) + # Update the user data + if soloq is not None: + account.soloq_tier = lol.tiers[soloq["tier"]] + account.soloq_division = lol.divisions[soloq["entries"][0]["division"]] + else: + account.soloq_tier = None + account.soloq_division = None + if flexq is not None: + account.flexq_tier = lol.tiers[flexq["tier"]] + account.flexq_division = lol.divisions[flexq["entries"][0]["division"]] + else: + account.flexq_tier = None + account.flexq_division = None + if ttq is not None: + account.ttq_tier = lol.tiers[ttq["tier"]] + account.ttq_division = lol.divisions[ttq["entries"][0]["division"]] + else: + account.ttq_tier = None + account.ttq_division = None + # Mark the user as updated + account.last_updated = datetime.datetime.now() # Commit the changes session.commit() \ No newline at end of file diff --git a/lol.py b/lol.py index 046910ae..61a61230 100644 --- a/lol.py +++ b/lol.py @@ -1,7 +1,6 @@ import asyncio import aiohttp import royalbotconfig -import enum # https://euw.api.riotgames.com/api/lol/EUW/v1.4/summoner/52348350?api_key=RGAPI-1008c33d-b0a4-4091-8600-27022d570964 @@ -46,23 +45,26 @@ async def get_summoner_data(region: str, summoner_id=None, summoner_name=None): "api_key": royalbotconfig.lol_token } if summoner_id is not None: - data = await get_json(f"https://{region.lower()}.api.riotgames.com/api/lol/{region.upper()}/v1.4/summoner/{summoner_id}") - return data[summoner_id] + data = await get_json(f"https://{region.lower()}.api.riotgames.com/api/lol/{region.upper()}/v1.4/summoner/{summoner_id}", params=params) + return data[str(summoner_id)] elif summoner_name is not None: - data = await get_json(f"https://{region.lower()}.api.riotgames.com/api/lol/{region.upper()}/v1.4/summoner/by-name/{summoner_name}") - return data[summoner_name] + data = await get_json(f"https://{region.lower()}.api.riotgames.com/api/lol/{region.upper()}/v1.4/summoner/by-name/{summoner_name}", params=params) + return data[summoner_name.lower().replace(" ", "")] async def get_rank_data(region: str, summoner_id: int): - data = await get_json(f"https://{region.lower()}.api.riotgames.com/api/lol/{region.upper()}/v2.5/league/by-summoner/{summoner_id}/entry") + params = { + "api_key": royalbotconfig.lol_token + } + data = await get_json(f"https://{region.lower()}.api.riotgames.com/api/lol/{region.upper()}/v2.5/league/by-summoner/{summoner_id}/entry", params=params) soloq = None flexq = None ttq = None - for entry in data: - if data["queue"] == "RANKED_SOLO_5x5": + for entry in data[str(summoner_id)]: + if entry["queue"] == "RANKED_SOLO_5x5": soloq = entry - elif data["queue"] == "RANKED_FLEX_SR": + elif entry["queue"] == "RANKED_FLEX_SR": flexq = entry - elif data["queue"] == "RANKED_FLEX_TT": + elif entry["queue"] == "RANKED_FLEX_TT": ttq = entry return soloq, flexq, ttq \ No newline at end of file diff --git a/royalbot.py b/royalbot.py index b9c48c17..9e560db7 100644 --- a/royalbot.py +++ b/royalbot.py @@ -261,7 +261,7 @@ Sintassi: `{symbol}synclol `""" # Set status to typing await status_typing(bot, thing) # Check the command syntax - if len(arguments) != 1: + if len(arguments) < 1: await display_help(bot, thing, synclol) return # Open a new database session @@ -275,11 +275,16 @@ Sintassi: `{symbol}synclol `""" # TODO: IMPROVE THIS summoner_name = " ".join(arguments) data = await lol.get_summoner_data("euw", summoner_name=summoner_name) - lolaccount = database.LoL(data["id"]) + lolaccount = database.LoL(id=data["id"], summoner_name=summoner_name) lolaccount.parentid = user.id session.add(lolaccount) session.commit() - database.update_lol(data["id"]) + await database.update_lol(user.id) + + +async def update_stats(timeout): + await asyncio.sleep(timeout) + loop.create_task() if __name__ == "__main__": # Init universal bot commands