1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00

Add some checks and fix some bugs

This commit is contained in:
Steffo 2019-04-05 11:54:19 +02:00
parent 6666331a15
commit ef485086ce
9 changed files with 25 additions and 13 deletions

View file

@ -1,3 +1,4 @@
python-telegram-bot>=11.1.0 python-telegram-bot>=11.1.0
websockets>=7.0 websockets>=7.0
pytest>=4.3.1 pytest>=4.3.1
psycopg2-binary>=2.8

View file

@ -63,8 +63,8 @@ class TelegramBot:
.replace("[/i]", "</i>") \ .replace("[/i]", "</i>") \
.replace("[u]", "<b>") \ .replace("[u]", "<b>") \
.replace("[/u]", "</b>") \ .replace("[/u]", "</b>") \
.replace("[c]", "<pre>") \ .replace("[c]", "<code>") \
.replace("[/c]", "</pre>") .replace("[/c]", "</code>")
await asyncify(call.channel.send_message, escaped_text, parse_mode="HTML") await asyncify(call.channel.send_message, escaped_text, parse_mode="HTML")
async def net_request(call, message: Message, destination: str): async def net_request(call, message: Message, destination: str):

View file

@ -14,4 +14,4 @@ class DebugAuthorCommand(Command):
author = await call.get_author() author = await call.get_author()
if author is None: if author is None:
await call.reply(f"☁️ L'autore di questa chiamata è sconosciuto.") 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.")

View file

@ -13,7 +13,7 @@ class DebugCreateCommand(Command):
async def common(self, call: Call): async def common(self, call: Call):
royal = call.alchemy.Royal(username=call.args[0], role="Member") royal = call.alchemy.Royal(username=call.args[0], role="Member")
call.session.add(royal) 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) call.session.add(alias)
await asyncify(call.session.commit) await asyncify(call.session.commit)
await call.reply(f"✅ Utente [c]{royal}[/c] creato!") await call.reply(f"✅ Utente {royal} creato!")

View file

@ -9,7 +9,7 @@ class DiarioCommand(Command):
command_name = "diario" command_name = "diario"
command_title = "Aggiungi una citazione al Diario." command_title = "Aggiungi una citazione al Diario."
command_syntax = "\"(testo)\" --[autore], [contesto]" command_syntax = "[!] \"(testo)\" --[autore], [contesto]"
require_alchemy_tables = {Royal, Diario, Alias} require_alchemy_tables = {Royal, Diario, Alias}
@ -36,12 +36,23 @@ class DiarioCommand(Command):
quoted = None quoted = None
context = None context = None
timestamp = datetime.datetime.now() 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 # Find if there's a Royalnet account associated with the quoted name
if quoted is not None: if quoted is not None:
quoted_alias = await asyncify(call.session.query(call.alchemy.Alias).filter_by(alias=quoted.lower()).one_or_none) quoted_alias = await asyncify(call.session.query(call.alchemy.Alias).filter_by(alias=quoted.lower()).one_or_none)
else: else:
quoted_alias = None quoted_alias = None
quoted_account = quoted_alias.royal if quoted_alias is not None else 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 # Create the diario quote
diario = call.alchemy.Diario(creator=creator, diario = call.alchemy.Diario(creator=creator,
quoted_account=quoted_account, quoted_account=quoted_account,

View file

@ -37,13 +37,13 @@ class SyncCommand(Command):
tg_last_name=user.last_name, tg_last_name=user.last_name,
tg_username=user.username) tg_username=user.username)
call.session.add(telegram) 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: else:
# Update the Telegram data # Update the Telegram data
# Avatar is WIP # Avatar is WIP
telegram.tg_first_name = user.first_name telegram.tg_first_name = user.first_name
telegram.tg_last_name = user.last_name telegram.tg_last_name = user.last_name
telegram.tg_username = user.username 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 # Commit the session
await asyncify(call.session.commit) await asyncify(call.session.commit)

View file

@ -32,15 +32,15 @@ class Diario:
def __str__(self): def __str__(self):
# TODO: support media_url # TODO: support media_url
text = f"Riga #{self.diario_id}" text = f"Riga #{self.diario_id}"
text += f" (salvata da {self.creator.username}" text += f" (salvata da {str(self.creator)}"
text += f" alle {self.timestamp.strftime('%Y-%m-%d %H:%M')}):\n" text += f" il {self.timestamp.strftime('%Y-%m-%d %H:%M')}):\n"
if self.spoiler: if self.spoiler:
hidden = re.sub("\w", "", self.text) hidden = re.sub("\w", "", self.text)
text += f"\"{hidden}\"\n" text += f"\"{hidden}\"\n"
else: else:
text += f"[b]\"{self.text}\"[/b]\n" text += f"[b]\"{self.text}\"[/b]\n"
if self.quoted_account is not None: 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: elif self.quoted is not None:
text += f"{self.quoted}" text += f"{self.quoted}"
else: else:

View file

@ -17,4 +17,4 @@ class Royal:
return f"<Royal {self.username}>" return f"<Royal {self.username}>"
def __str__(self): def __str__(self):
return f"royalnet:{self.username}" return f"[c]royalnet:{self.username}[/c]"

View file

@ -24,7 +24,7 @@ class Telegram:
return f"<Telegram {str(self)}>" return f"<Telegram {str(self)}>"
def __str__(self): def __str__(self):
return f"telegram:{self.mention()}" return f"[c]telegram:{self.mention()}[/c]"
def mention(self) -> str: def mention(self) -> str:
if self.tg_username is not None: if self.tg_username is not None: