From 65b965ed4c8d9e8cc00a378fa465d0914f558bb9 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 2 Apr 2019 00:25:17 +0200 Subject: [PATCH] Do... stuff? --- .gitignore | 1 + royalgames.py | 2 +- royalnet/commands/debug_create.py | 2 +- royalnet/commands/sync.py | 28 ++++++++++++++++++++-------- royalnet/database/alchemy.py | 4 +++- royalnet/database/tables/royals.py | 2 +- royalnet/database/tables/telegram.py | 4 ++++ 7 files changed, 31 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 015ad6c0..e9a9e414 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ opusfiles/ ignored/ markovmodels/ logs/ +royalnet.egg-info/ diff --git a/royalgames.py b/royalgames.py index da03bb3d..3aa357ec 100644 --- a/royalgames.py +++ b/royalgames.py @@ -10,7 +10,7 @@ loop = asyncio.get_event_loop() commands = [PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, DebugCreateCommand, SyncCommand] master = RoyalnetServer("localhost", 1234, "sas") -tg_bot = TelegramBot(os.environ["TG_AK"], "localhost:1234", "sas", commands, "sqlite://") +tg_bot = TelegramBot(os.environ["TG_AK"], "localhost:1234", "sas", commands, os.environ["DB_PATH"]) loop.create_task(master.run()) loop.create_task(tg_bot.run()) print("Starting loop...") diff --git a/royalnet/commands/debug_create.py b/royalnet/commands/debug_create.py index 2bb7e7dd..7e1021b0 100644 --- a/royalnet/commands/debug_create.py +++ b/royalnet/commands/debug_create.py @@ -13,4 +13,4 @@ class DebugCreateCommand(Command): royal = call.interface_alchemy.Royal(username=args[0], role="Member") call.session.add(royal) call.session.commit() - await call.reply(f"✅ Utente {royal} creato!") + await call.reply(f"✅ Utente {royal} creato!") diff --git a/royalnet/commands/sync.py b/royalnet/commands/sync.py index 2aabff77..6b0d9793 100644 --- a/royalnet/commands/sync.py +++ b/royalnet/commands/sync.py @@ -24,13 +24,25 @@ class SyncCommand(Command): royal = await asyncify(call.session.query(call.interface_alchemy.Royal).filter_by(username=args[0]).one_or_none) if royal is None: await call.reply("⚠️ Non esiste alcun account Royalnet con quel nome.") - # Create a Telegram to connect to the Royal - # Avatar is WIP - telegram = call.interface_alchemy.Telegram(royal=royal, - tg_id=user.id, - tg_first_name=user.first_name, - tg_last_name=user.last_name, - tg_username=user.username) - call.session.add(telegram) + # Find if the user is already synced + telegram = await asyncify(call.session.query(call.interface_alchemy.Telegram).filter_by(tg_id=user.id).one_or_none) + if telegram is None: + # Create a Telegram to connect to the Royal + # Avatar is WIP + telegram = call.interface_alchemy.Telegram(royal=royal, + tg_id=user.id, + tg_first_name=user.first_name, + tg_last_name=user.last_name, + tg_username=user.username) + call.session.add(telegram) + await call.reply(f"✅ Connessione completata: {str(royal)}{str(telegram)}") + else: + # Update the Telegram data + # Avatar is WIP + telegram.tg_first_name = user.first_name + telegram.tg_last_name = user.last_name + telegram.tg_username = user.username + await call.reply(f"✅ Dati di {str(telegram)} aggiornati.") # Commit the session await asyncify(call.session.commit()) + # Notify the user diff --git a/royalnet/database/alchemy.py b/royalnet/database/alchemy.py index 8dd9ba4c..43417dfd 100644 --- a/royalnet/database/alchemy.py +++ b/royalnet/database/alchemy.py @@ -10,7 +10,9 @@ loop = asyncio.get_event_loop() class Alchemy: - def __init__(self, database_uri: str = "sqlite://", tables: typing.Optional[typing.Set] = None): + def __init__(self, database_uri: str, tables: typing.Optional[typing.Set] = None): + if database_uri.startswith("sqlite"): + raise NotImplementedError("Support for sqlite databases is currently missing") self.engine = create_engine(database_uri) self.Base = declarative_base(bind=self.engine) self.Session = sessionmaker(bind=self.engine) diff --git a/royalnet/database/tables/royals.py b/royalnet/database/tables/royals.py index 7d71a1ca..180720bc 100644 --- a/royalnet/database/tables/royals.py +++ b/royalnet/database/tables/royals.py @@ -17,4 +17,4 @@ class Royal: return f"" def __str__(self): - return self.username + return f"royalnet:{self.username}" diff --git a/royalnet/database/tables/telegram.py b/royalnet/database/tables/telegram.py index dec361f0..624584fa 100644 --- a/royalnet/database/tables/telegram.py +++ b/royalnet/database/tables/telegram.py @@ -5,6 +5,7 @@ from sqlalchemy import Column, \ LargeBinary, \ ForeignKey from sqlalchemy.orm import relationship +from .royals import Royal class Telegram: @@ -23,6 +24,9 @@ class Telegram: return f"" def __str__(self): + return f"telegram:{self.mention()}" + + def mention(self) -> str: if self.tg_username is not None: return f"@{self.tg_username}" elif self.tg_last_name is not None: