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

Fix /search and /regex

This commit is contained in:
Steffo 2019-02-01 20:05:21 +01:00
parent ba507ce945
commit d118fddf13
2 changed files with 23 additions and 12 deletions

View file

@ -3,7 +3,7 @@ from db import MatchmakingStatus
class SafeDict(dict):
def __missing__(self, key):
return '<code>' + key + '</code>'
return key
def safely_format_string(string, **kwargs):
@ -16,7 +16,7 @@ class TELEGRAM:
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 <code>/register (NomeUtenteRoyalnet)</code>."
PONG = "🏓 Pong!"
@ -27,7 +27,7 @@ class LINKING:
SUCCESSFUL = "✅ Collegamento riuscito!"
class ERRORS:
INVALID_SYNTAX = "⚠ Non hai specificato un username!\nSintassi del comando: <code>/register {username}</code>"
INVALID_SYNTAX = "⚠ Non hai specificato un username!\nSintassi del comando: <code>/register (NomeUtenteRoyalnet)</code>"
NOT_FOUND = "⚠ Non esiste nessun account Royalnet con quel nome."
ALREADY_EXISTING = "⚠ Questo account è già collegato a un account Royalnet."
@ -71,7 +71,7 @@ class MATCHMAKING:
}
class ERRORS:
INVALID_SYNTAX = "⚠ Sintassi del comando errata.\nSintassi: <pre>/mm [minplayers-][maxplayers] per {gamename} \\n[descrizione]</pre>"
INVALID_SYNTAX = "⚠ Sintassi del comando errata.\nSintassi: <pre>/mm [minplayers-][maxplayers] per (gamename) \\n[descrizione]</pre>"
NOT_ADMIN = "⚠ Non sei il creatore di questo match!"
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!"
@ -82,4 +82,5 @@ class DIARIOSEARCH:
HEADER = " Risultati della ricerca di {term}:\n"
class ERRORS:
INVALID_SYNTAX = "⚠ Non hai specificato un termine da cercare!\nSintassi: <pre>/search {termine}</pre>"
INVALID_SYNTAX = "⚠ Non hai specificato un termine da cercare!\nSintassi: <pre>/{command} (termine)</pre>"
RESULTS_TOO_LONG = "⚠ Sono presenti troppi risultati da visualizzare! Prova a restringere la ricerca."

View file

@ -10,7 +10,7 @@ from sqlalchemy.sql import text
from telegram import Bot, Update, InlineKeyboardMarkup, InlineKeyboardButton
# noinspection PyPackageRequirements
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler
from telegram.error import TimedOut, Unauthorized, BadRequest
from telegram.error import TimedOut, Unauthorized, BadRequest, TelegramError
import dice
import sys
import os
@ -298,12 +298,18 @@ def cmd_vote(bot: Bot, update: Update):
def generate_search_message(term, entries):
msg = s(strings.DIARIOSEARCH.HEADER, term=term)
if len(entries) < 100:
for entry in entries[:5]:
msg += f'<a href="https://ryg.steffo.eu/diario#entry-{entry.id}">#{entry.id}</a> di {entry.author or "Anonimo"}\n{entry.text}\n\n'
msg += f'<a href="https://ryg.steffo.eu/diario#entry-{entry.id}">#{entry.id}</a> di <i>{entry.author or "Anonimo"}</i>\n{entry.text}\n\n'
if len(entries) > 5:
msg += "I termini comapiono anche nelle righe:\n"
for entry in entries[5:]:
msg += f'<a href="https://ryg.steffo.eu/diario#entry-{entry.id}">#{entry.id}</a> '
else:
for entry in entries[:100]:
msg += f'<a href="https://ryg.steffo.eu/diario#entry-{entry.id}">#{entry.id}</a> '
for entry in entries[100:]:
msg += f"#{entry.id} "
return msg
@ -314,7 +320,7 @@ def cmd_search(bot: Bot, update: Update):
try:
query = update.message.text.split(" ", 1)[1]
except IndexError:
bot.send_message(update.message.chat.id, s(strings.DIARIOSEARCH.ERRORS.INVALID_SYNTAX))
bot.send_message(update.message.chat.id, s(strings.DIARIOSEARCH.ERRORS.INVALID_SYNTAX, command="search"), parse_mode="HTML")
return
query = query.replace('%', '\\%').replace('_', '\\_')
entries = session.query(db.Diario)\
@ -323,7 +329,7 @@ def cmd_search(bot: Bot, update: Update):
r"(?:[\s\.,:;!?\"'>\})\]]+|$)"))\
.order_by(db.Diario.id.desc())\
.all()
bot.send_message(update.message.chat.id, generate_search_message(f"<b>query</b>", entries))
bot.send_message(update.message.chat.id, generate_search_message(f"<b>{query}</b>", entries), parse_mode="HTML")
finally:
session.close()
@ -335,11 +341,14 @@ def cmd_regex(bot: Bot, update: Update):
try:
query = update.message.text.split(" ", 1)[1]
except IndexError:
bot.send_message(update.message.chat.id, s(strings.DIARIOSEARCH.ERRORS.INVALID_SYNTAX))
bot.send_message(update.message.chat.id, s(strings.DIARIOSEARCH.ERRORS.INVALID_SYNTAX, command="regex"), parse_mode="HTML")
return
query = query.replace('%', '\\%').replace('_', '\\_')
entries = session.query(db.Diario).filter(db.Diario.text.op("~*")(query)).order_by(db.Diario.id.desc()).all()
bot.send_message(update.message.chat.id, generate_search_message(f"<code>query</code>", entries))
try:
bot.send_message(update.message.chat.id, generate_search_message(f"<code>{query}</code>", entries), parse_mode="HTML")
except (BadRequest, TelegramError):
bot.send_message(update.message.chat.id, s(strings.DIARIOSEARCH.ERRORS.RESULTS_TOO_LONG))
finally:
session.close()
@ -781,6 +790,7 @@ def process(arg_discord_connection):
u.dispatcher.add_handler(CommandHandler("matchmaking", cmd_mm))
u.dispatcher.add_handler(CommandHandler("search", cmd_search))
u.dispatcher.add_handler(CommandHandler("regex", cmd_regex))
u.dispatcher.add_handler(CommandHandler("start", cmd_start))
u.dispatcher.add_handler(CallbackQueryHandler(on_callback_query))
logger.info("Handlers registered.")
u.start_polling()