From 8fec58827f9c3b6ee7b3818e12d2c44600f74026 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 26 Apr 2017 11:59:44 +0200 Subject: [PATCH] unfinished --- database.py | 5 ++++- lol.py | 2 +- royalbot.py | 20 +++++++++++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/database.py b/database.py index 6f220f80..c2bb0f8c 100644 --- a/database.py +++ b/database.py @@ -89,6 +89,7 @@ def new_diario_entry(dt, text): session.commit() +# TODO: improve this async def update_lol(lid): # Create a new database session session = Session() @@ -121,4 +122,6 @@ async def update_lol(lid): user.lol.ttq_tier = None user.lol.ttq_division = None # Mark the user as updated - user.lol.last_updated = datetime.datetime.now() \ No newline at end of file + user.lol.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 c125db2b..046910ae 100644 --- a/lol.py +++ b/lol.py @@ -31,7 +31,7 @@ divisions = { async def get_json(url, **kwargs): async with aiohttp.ClientSession() as session: async with session.get(url, **kwargs) as response: - json = await session.json() + json = await response.json() if response.status != 200: raise LoLAPIError(f"Riot API returned {response.status}") return json diff --git a/royalbot.py b/royalbot.py index 696e1bc6..b9c48c17 100644 --- a/royalbot.py +++ b/royalbot.py @@ -2,6 +2,7 @@ import asyncio import random import extradiscord import database +import lol import royalbotconfig import telegram @@ -252,6 +253,7 @@ Sintassi: `{symbol}syncdiscord`""" await answer(bot, thing, "✅ Account registrato con successo!") +# DISCORD ONLY! async def synclol(bot, thing, arguments): """Connetti il tuo account di LoL all'account Royal Games! @@ -259,10 +261,25 @@ Sintassi: `{symbol}synclol `""" # Set status to typing await status_typing(bot, thing) # Check the command syntax - if len(arguments) != 0: + if len(arguments) != 1: await display_help(bot, thing, synclol) return + # Open a new database session + session = database.Session() # Create a new lol account and connect it to the user + user = session.query(database.Account).filter_by(id=thing.author.id).first() + if user is None: + await answer(bot, thing, "⚠ Fai il login prima di sincronizzare l'account di LoL.") + return + # Create a new LoL account and link it to the user + # TODO: IMPROVE THIS + summoner_name = " ".join(arguments) + data = await lol.get_summoner_data("euw", summoner_name=summoner_name) + lolaccount = database.LoL(data["id"]) + lolaccount.parentid = user.id + session.add(lolaccount) + session.commit() + database.update_lol(data["id"]) if __name__ == "__main__": # Init universal bot commands @@ -275,6 +292,7 @@ if __name__ == "__main__": d.commands["helpme"] = helpme b.commands["cv"] = cv d.commands["syncdiscord"] = syncdiscord + d.commands["synclol"] = synclol # Init Telegram bot loop.create_task(b.run()) print("Telegram bot start scheduled!")