diff --git a/Commands.py b/Commands.py index 362bf50..8d4467a 100644 --- a/Commands.py +++ b/Commands.py @@ -12,7 +12,7 @@ import Werewords.Controller as WerewordsController import Unanimo.Controller as UnanimoController -from Utils import restricted, player_call, send_typing_action, get_game, delete_game, save, load_game, save_game +from Utils import restricted, player_call, user_call, send_typing_action, get_game, delete_game, save, load_game, save_game from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update, ForceReply from telegram.constants import ParseMode from telegram.ext import (CallbackContext) @@ -1262,6 +1262,25 @@ async def call_players_group(update: Update, context: CallbackContext): log.info('No se busco bien los jugadores debido al siguiente error: '+str(e)) conn.rollback() +async def command_all(update: Update, context: CallbackContext): + bot = context.bot + cid = update.message.chat_id + # Llama a todos los miembros conocidos del grupo (los que el bot vio entrar, + # o los que se unieron a una partida y quedaron registrados igual). + if update.message.chat.type not in ['group', 'supergroup']: + await bot.send_message(cid, "Este comando solo funciona en grupos.") + return + miembros = MainController.get_group_members(cid) + if not miembros: + await bot.send_message(cid, "Todavia no tengo miembros guardados de este grupo. Se van agregando a medida que entran al grupo o se unen a una partida.") + return + menciones = [user_call(nombre, uid) for uid, nombre in miembros] + # Telegram limita los mensajes a 4096 caracteres, mando en tandas por las dudas. + TANDA = 50 + for i in range(0, len(menciones), TANDA): + texto = "📢 " + " ".join(menciones[i:i + TANDA]) + await bot.send_message(cid, texto, parse_mode=ParseMode.MARKDOWN) + async def callback_timer(update: Update, context: CallbackContext): cid = update.message.chat_id bot = context.bot diff --git a/MainController.py b/MainController.py index 2e467ae..de4bdb4 100644 --- a/MainController.py +++ b/MainController.py @@ -308,9 +308,11 @@ async def unknown(update: Update, context: CallbackContext): bot = context.bot await bot.send_message(chat_id=update.message.chat_id, text="No conozco ese comando") -def add_group(update: Update, context: CallbackContext): +async def add_group(update: Update, context: CallbackContext): cid = update.message.chat.id for member in update.message.new_chat_members: + if member.is_bot: + continue #ot.send_message(ADMIN[0], text="{username} {id} add group {groupname}".format(username=member.first_name, id=member.id, groupname = groupname, cid=cid)) add_member_group(cid, member.id) add_user(member.id, member.first_name) @@ -420,6 +422,28 @@ def remove_member_group(cid, uid): conn.close() +def get_group_members(cid): + # Devuelve [(uid, name), ...] de los miembros conocidos del grupo (los que + # entraron mientras el bot estaba en el grupo, o se unieron a una partida). + try: + conn = psycopg.connect( + dbname=url.path[1:], + user=url.username, + password=url.password, + host=url.hostname, + port=url.port + ) + cur = conn.cursor() + query = ("SELECT u.id, u.name FROM users_group ug " + "JOIN users u ON u.id = ug.user_id WHERE ug.group_id = %s;") + cur.execute(query, [cid]) + miembros = cur.fetchall() + conn.close() + return miembros + except Exception as e: + log.info('No se pudo obtener los miembros del grupo debido a: '+str(e)) + return [] + def put(update: Update, context: CallbackContext): args = context.args @@ -612,6 +636,7 @@ def main(stop_event): app.add_handler(CommandHandler("leave", Commands.command_leave)) app.add_handler(CommandHandler("history", Commands.command_showhistory)) app.add_handler(CommandHandler("ping", Commands.call_players_group)) + app.add_handler(CommandHandler("all", Commands.command_all)) app.add_handler(CommandHandler("call", Commands.command_call)) app.add_handler(CommandHandler("claim", Commands.command_claim)) app.add_handler(CommandHandler("prueba", Commands.command_prueba)) @@ -927,10 +952,10 @@ def main(stop_event): app.add_handler(MessageHandler(filters.TEXT, command_status)) # Handler cuando se una una persona al chat. - #app.add_handler(MessageHandler(filters.StatusUpdate.NEW_CHAT_MEMBERS, add_group)) - + app.add_handler(MessageHandler(filters.StatusUpdate.NEW_CHAT_MEMBERS, add_group)) + # Handler cuando se va una persona del chat. - #app.add_handler(MessageHandler(filters.StatusUpdate.LEFT_CHAT_MEMBER, remove_group)) + app.add_handler(MessageHandler(filters.StatusUpdate.LEFT_CHAT_MEMBER, remove_group)) # Handler cuando se cambia el nombre del chat app.add_handler(MessageHandler(filters.StatusUpdate.NEW_CHAT_TITLE, change_groupname))