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

Move more strings to strings.py

This commit is contained in:
Steffo 2019-01-28 22:55:55 +01:00
parent 616b3cf3ab
commit baafe5278a
2 changed files with 47 additions and 34 deletions

View file

@ -10,12 +10,26 @@ def safely_format_string(string, **kwargs):
return string.format_map(SafeDict(**kwargs)) return string.format_map(SafeDict(**kwargs))
# Generic errors # Generic telegram errors
class ROYALNET: class TELEGRAM:
class ERRORS: class ERRORS:
CRITICAL_ERROR = "☢ <b>ERRORE CRITICO!</b>\nIl bot ha ignorato il comando.\nUna segnalazione di errore è stata automaticamente mandata a @Steffo.\n\nDettagli dell'errore:\n<pre>{exc_info}</pre>"
TELEGRAM_NOT_LINKED = "⚠ Il tuo account Telegram non è registrato a Royalnet! Registrati con `/register@royalgamesbot <nomeutenteryg>`." TELEGRAM_NOT_LINKED = "⚠ Il tuo account Telegram non è registrato a Royalnet! Registrati con `/register@royalgamesbot <nomeutenteryg>`."
PONG = "🏓 Pong!"
# Royalnet linking
class LINKING:
SUCCESSFUL = "✅ Collegamento riuscito!"
class ERRORS:
INVALID_SYNTAX = "⚠ Non hai specificato un username!\nSintassi del comando: <code>/register {username}</code>"
NOT_FOUND = "⚠ Non esiste nessun account Royalnet con quel nome."
ALREADY_EXISTING = "⚠ Questo account è già collegato a un account Royalnet."
# Markov strings # Markov strings
class MARKOV: class MARKOV:
class ERRORS: class ERRORS:
@ -55,7 +69,7 @@ class MATCHMAKING:
} }
class ERRORS: class ERRORS:
INVALID_SYNTAX = "⚠ Sintassi del comando errata.\n Sintassi: `/mm [minplayers-][maxplayers] per <gamename> \\n[descrizione]`" INVALID_SYNTAX = "⚠ Sintassi del comando errata.\n Sintassi: <pre>/mm [minplayers-][maxplayers] per <gamename> \\n[descrizione]</pre>"
NOT_ADMIN = "⚠ Non sei il creatore di questo match!" NOT_ADMIN = "⚠ Non sei il creatore di questo match!"
MATCH_CLOSED = "⚠ Il matchmaking per questa partita è terminato!" MATCH_CLOSED = "⚠ Il matchmaking per questa partita è terminato!"
UNAUTHORIZED = "⚠ Non sono autorizzato a inviare messaggi a {mention}. \nPer piacere, {mention}, inviami un messaggio in privata!" UNAUTHORIZED = "⚠ Non sono autorizzato a inviare messaggi a {mention}. \nPer piacere, {mention}, inviami un messaggio in privata!"

View file

@ -56,21 +56,16 @@ def catch_and_report(func: "function"):
except TimedOut: except TimedOut:
logger.warning(f"Telegram timed out in {update}") logger.warning(f"Telegram timed out in {update}")
except Exception: except Exception:
if __debug__:
raise
logger.error(f"Critical error: {sys.exc_info()}") logger.error(f"Critical error: {sys.exc_info()}")
# noinspection PyBroadException # noinspection PyBroadException
try: try:
bot.send_message(int(config["Telegram"]["main_group"]), bot.send_message(int(config["Telegram"]["main_group"]),
"☢ **ERRORE CRITICO!** \n" s(strings.TELEGRAM.ERRORS.CRITICAL_ERROR, exc_info=sys.exc_info()),
f"Il bot ha ignorato il comando.\n" parse_mode="Markdown")
f"Una segnalazione di errore è stata automaticamente mandata a @Steffo.\n\n"
f"Dettagli dell'errore:\n"
f"```\n"
f"{sys.exc_info()}\n"
f"```", parse_mode="Markdown")
except Exception: except Exception:
logger.error(f"Double critical error: {sys.exc_info()}") logger.error(f"Double critical error: {sys.exc_info()}")
if __debug__:
raise
sentry.user_context({ sentry.user_context({
"id": update.effective_user.id, "id": update.effective_user.id,
"telegram": { "telegram": {
@ -88,31 +83,35 @@ def catch_and_report(func: "function"):
@catch_and_report @catch_and_report
def cmd_ping(bot: Bot, update: Update): def cmd_ping(bot: Bot, update: Update):
bot.send_message(update.message.chat.id, "🏓 Pong!") bot.send_message(update.message.chat.id, s(strings.PONG))
@catch_and_report @catch_and_report
def cmd_register(bot: Bot, update: Update): def cmd_register(bot: Bot, update: Update):
session = db.Session() session = db.Session()
try:
try: try:
username = update.message.text.split(" ", 1)[1] username = update.message.text.split(" ", 1)[1]
except IndexError: except IndexError:
bot.send_message(update.message.chat.id, "⚠️ Non hai specificato un username!") bot.send_message(update.message.chat.id, s(strings.LINKING.ERRORS.INVALID_SYNTAX))
session.close() session.close()
return return
try: try:
t = db.Telegram.create(session, t = db.Telegram.create(session,
royal_username=username, royal_username=username,
telegram_user=update.message.from_user) telegram_user=update.message.from_user)
except errors.NotFoundError:
bot.send_message(update.message.chat.id, s(strings.LINKING.ERRORS.NOT_FOUND))
session.close()
return
except errors.AlreadyExistingError: except errors.AlreadyExistingError:
bot.send_message(update.message.chat.id, "⚠ Il tuo account Telegram è già collegato a un account RYG o" bot.send_message(update.message.chat.id, s(strings.LINKING.ERRORS.ALREADY_EXISTING))
" l'account RYG che hai specificato è già collegato a un account"
" Telegram.")
session.close() session.close()
return return
session.add(t) session.add(t)
session.commit() session.commit()
bot.send_message(update.message.chat.id, "✅ Sincronizzazione completata!") bot.send_message(update.message.chat.id, s(strings.LINKING.SUCCESSFUL))
finally:
session.close() session.close()
@ -317,7 +316,7 @@ def cmd_mm(bot: Bot, update: Update):
try: try:
user = session.query(db.Telegram).filter_by(telegram_id=update.message.from_user.id).one_or_none() user = session.query(db.Telegram).filter_by(telegram_id=update.message.from_user.id).one_or_none()
if user is None: if user is None:
bot.send_message(update.message.chat.id, strings.ROYALNET.ERRORS.TELEGRAM_NOT_LINKED, parse_mode="Markdown") bot.send_message(update.message.chat.id, strings.TELEGRAM.ERRORS.TELEGRAM_NOT_LINKED, parse_mode="Markdown")
return return
match = re.match(r"/(?:mm|matchmaking)(?:@royalgamesbot)?(?: (?:([0-9]+)-)?([0-9]+))? (?:per )?([A-Za-z0-9!\-_\. ]+)(?:.*\n(.+))?", match = re.match(r"/(?:mm|matchmaking)(?:@royalgamesbot)?(?: (?:([0-9]+)-)?([0-9]+))? (?:per )?([A-Za-z0-9!\-_\. ]+)(?:.*\n(.+))?",
update.message.text) update.message.text)
@ -365,7 +364,7 @@ def on_callback_query(bot: Bot, update: Update):
user = session.query(db.Telegram).filter_by(telegram_id=update.callback_query.from_user.id).one_or_none() user = session.query(db.Telegram).filter_by(telegram_id=update.callback_query.from_user.id).one_or_none()
if user is None: if user is None:
bot.answer_callback_query(update.callback_query.id, show_alert=True, bot.answer_callback_query(update.callback_query.id, show_alert=True,
text=strings.ROYALNET.ERRORS.TELEGRAM_NOT_LINKED, text=strings.TELEGRAM.ERRORS.TELEGRAM_NOT_LINKED,
parse_mode="Markdown") parse_mode="Markdown")
return return
question = session.query(db.VoteQuestion)\ question = session.query(db.VoteQuestion)\
@ -404,7 +403,7 @@ def on_callback_query(bot: Bot, update: Update):
if user is None: if user is None:
bot.answer_callback_query(update.callback_query.id, bot.answer_callback_query(update.callback_query.id,
show_alert=True, show_alert=True,
text=strings.ROYALNET.ERRORS.TELEGRAM_NOT_LINKED, text=strings.TELEGRAM.ERRORS.TELEGRAM_NOT_LINKED,
parse_mode="Markdown") parse_mode="Markdown")
return return
match = session.query(db.Match).filter_by(message_id=update.callback_query.message.message_id).one() match = session.query(db.Match).filter_by(message_id=update.callback_query.message.message_id).one()