From ef485086cef2433cd122464f1bd94f341416daef Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 5 Apr 2019 11:54:19 +0200 Subject: [PATCH] Add some checks and fix some bugs --- requirements.txt | 1 + royalnet/bots/telegram.py | 4 ++-- royalnet/commands/debug_author.py | 2 +- royalnet/commands/debug_create.py | 4 ++-- royalnet/commands/diario.py | 13 ++++++++++++- royalnet/commands/sync.py | 4 ++-- royalnet/database/tables/diario.py | 6 +++--- royalnet/database/tables/royals.py | 2 +- royalnet/database/tables/telegram.py | 2 +- 9 files changed, 25 insertions(+), 13 deletions(-) diff --git a/requirements.txt b/requirements.txt index 483e63f5..75e6bd32 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ python-telegram-bot>=11.1.0 websockets>=7.0 pytest>=4.3.1 +psycopg2-binary>=2.8 diff --git a/royalnet/bots/telegram.py b/royalnet/bots/telegram.py index 3af8100d..9595ab0a 100644 --- a/royalnet/bots/telegram.py +++ b/royalnet/bots/telegram.py @@ -63,8 +63,8 @@ class TelegramBot: .replace("[/i]", "") \ .replace("[u]", "") \ .replace("[/u]", "") \ - .replace("[c]", "
") \
-                                   .replace("[/c]", "
") + .replace("[c]", "") \ + .replace("[/c]", "") await asyncify(call.channel.send_message, escaped_text, parse_mode="HTML") async def net_request(call, message: Message, destination: str): diff --git a/royalnet/commands/debug_author.py b/royalnet/commands/debug_author.py index e3b3285c..0c7bfdcd 100644 --- a/royalnet/commands/debug_author.py +++ b/royalnet/commands/debug_author.py @@ -14,4 +14,4 @@ class DebugAuthorCommand(Command): author = await call.get_author() if author is None: await call.reply(f"☁️ L'autore di questa chiamata Γ¨ sconosciuto.") - await call.reply(f"🌞 [c]{str(author)}[/c] Γ¨ l'autore di questa chiamata.") + await call.reply(f"🌞 {str(author)} Γ¨ l'autore di questa chiamata.") diff --git a/royalnet/commands/debug_create.py b/royalnet/commands/debug_create.py index 4804a2ab..46ddb39e 100644 --- a/royalnet/commands/debug_create.py +++ b/royalnet/commands/debug_create.py @@ -13,7 +13,7 @@ class DebugCreateCommand(Command): async def common(self, call: Call): royal = call.alchemy.Royal(username=call.args[0], role="Member") call.session.add(royal) - alias = call.alchemy.Alias(royal=royal, alias=royal.username) + alias = call.alchemy.Alias(royal=royal, alias=royal.username.lower()) call.session.add(alias) await asyncify(call.session.commit) - await call.reply(f"βœ… Utente [c]{royal}[/c] creato!") + await call.reply(f"βœ… Utente {royal} creato!") diff --git a/royalnet/commands/diario.py b/royalnet/commands/diario.py index 55f35574..9f589595 100644 --- a/royalnet/commands/diario.py +++ b/royalnet/commands/diario.py @@ -9,7 +9,7 @@ class DiarioCommand(Command): command_name = "diario" command_title = "Aggiungi una citazione al Diario." - command_syntax = "\"(testo)\" --[autore], [contesto]" + command_syntax = "[!] \"(testo)\" --[autore], [contesto]" require_alchemy_tables = {Royal, Diario, Alias} @@ -36,12 +36,23 @@ class DiarioCommand(Command): quoted = None context = None timestamp = datetime.datetime.now() + # Ensure there is some text + if not text: + raise InvalidInputError("Missing text.") + # Or a quoted + if not quoted: + quoted = None + if not context: + context = None # Find if there's a Royalnet account associated with the quoted name if quoted is not None: quoted_alias = await asyncify(call.session.query(call.alchemy.Alias).filter_by(alias=quoted.lower()).one_or_none) else: quoted_alias = None quoted_account = quoted_alias.royal if quoted_alias is not None else None + if quoted_alias is not None and quoted_account is None: + await call.reply("⚠️ Il nome dell'autore Γ¨ ambiguo, quindi la riga non Γ¨ stata aggiunta.\nPer piacere, ripeti il comando con un nome piΓΉ specifico!") + return # Create the diario quote diario = call.alchemy.Diario(creator=creator, quoted_account=quoted_account, diff --git a/royalnet/commands/sync.py b/royalnet/commands/sync.py index f4863834..9f2e287b 100644 --- a/royalnet/commands/sync.py +++ b/royalnet/commands/sync.py @@ -37,13 +37,13 @@ class SyncCommand(Command): tg_last_name=user.last_name, tg_username=user.username) call.session.add(telegram) - await call.reply(f"βœ… Connessione completata: [c]{str(royal)}[/c] ⬌ [c]{str(telegram)}[/c]") + 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 [c]{str(telegram)}[/c] aggiornati.") + await call.reply(f"βœ… Dati di {str(telegram)} aggiornati.") # Commit the session await asyncify(call.session.commit) diff --git a/royalnet/database/tables/diario.py b/royalnet/database/tables/diario.py index 720789e4..7758e88b 100644 --- a/royalnet/database/tables/diario.py +++ b/royalnet/database/tables/diario.py @@ -32,15 +32,15 @@ class Diario: def __str__(self): # TODO: support media_url text = f"Riga #{self.diario_id}" - text += f" (salvata da {self.creator.username}" - text += f" alle {self.timestamp.strftime('%Y-%m-%d %H:%M')}):\n" + text += f" (salvata da {str(self.creator)}" + text += f" il {self.timestamp.strftime('%Y-%m-%d %H:%M')}):\n" if self.spoiler: hidden = re.sub("\w", "β–ˆ", self.text) text += f"\"{hidden}\"\n" else: text += f"[b]\"{self.text}\"[/b]\n" if self.quoted_account is not None: - text += f" β€”{self.quoted_account.username}" + text += f" β€”{str(self.quoted_account)}" elif self.quoted is not None: text += f" β€”{self.quoted}" else: diff --git a/royalnet/database/tables/royals.py b/royalnet/database/tables/royals.py index 180720bc..63a85f1b 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 f"royalnet:{self.username}" + return f"[c]royalnet:{self.username}[/c]" diff --git a/royalnet/database/tables/telegram.py b/royalnet/database/tables/telegram.py index e11c9f5f..20f3d7e1 100644 --- a/royalnet/database/tables/telegram.py +++ b/royalnet/database/tables/telegram.py @@ -24,7 +24,7 @@ class Telegram: return f"" def __str__(self): - return f"telegram:{self.mention()}" + return f"[c]telegram:{self.mention()}[/c]" def mention(self) -> str: if self.tg_username is not None: