1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Guess the author if specified in the diario text

This commit is contained in:
Steffo 2019-02-22 00:58:55 +01:00
parent bf6e46cc4f
commit ba5b9f68b7
3 changed files with 18 additions and 3 deletions

4
db.py
View file

@ -808,9 +808,9 @@ class Diario(Base):
return f"{self.id} - {self.timestamp} - {self.author}: {self.text}"
def to_telegram(self):
return '<a href="https://ryg.steffo.eu/diario#entry-{id}">#{id}</a> di <b>{author}</b>\n{text}'.format(
return '<a href="https://ryg.steffo.eu/diario#entry-{id}">#{id}</a> di {author}\n{text}'.format(
id=self.id,
author=self.author,
author=f"<b>{self.author}</b>" if self.author is not None else strings.DIARIO.ANONYMOUS,
text=escape(self.text))
def to_html(self):

View file

@ -70,6 +70,7 @@ COLOR = "<i>I am sorry, unknown error occured during working with your request,
# Diario
class DIARIO:
SUCCESS = "✅ Riga aggiunta al diario:\n{diario}"
ANONYMOUS = "Anonimo"
class ERRORS:
INVALID_SYNTAX = "⚠ Sintassi del comando errata.\nSintassi: <code>/diario (frase)</code>, oppure rispondi a un messaggio con <code>/diario</code>."

View file

@ -223,6 +223,20 @@ def cmd_balurage(bot: telegram.Bot, update: telegram.Update, session: db.Session
bot.send_message(update.message.chat.id, f"😡 Stai sfogando la tua ira sul bot!")
def find_author(session: db.Session, text: str):
author_match = re.match(r".*(?:—|-{1,2}) ?@?([A-Za-z0-9_]+)$", text)
if author_match is None:
return None
author_string = author_match.group(1).lower()
author = session.query(db.Royal).filter(db.func.lower(db.Royal.username) == author_string).first().telegram[0]
if author is not None:
return author
author = session.query(db.Telegram).filter(db.func.lower(db.Telegram.username) == author_string).first()
if author is not None:
return author
return None
@command
@database_access
def cmd_diario(bot: telegram.Bot, update: telegram.Update, session: db.Session):
@ -233,7 +247,7 @@ def cmd_diario(bot: telegram.Bot, update: telegram.Update, session: db.Session):
try:
text = update.message.text.split(" ", 1)[1]
saver = session.query(db.Telegram).filter_by(telegram_id=update.message.from_user.id).one_or_none()
author = None
author = find_author(session, text)
except IndexError:
if update.message.reply_to_message is None:
reply(bot, update, strings.DIARIO.ERRORS.INVALID_SYNTAX)