From 80dc0b9c4fc15951b1f8c2193d84fccd05c53430 Mon Sep 17 00:00:00 2001 From: Mirco Grillo Date: Fri, 2 Oct 2020 15:26:58 +0200 Subject: [PATCH] Kick user if it has no username - Debugging welcome.py - Improving username check - Improving username check by checking the existence of the `username` attribute. Refactorizing username check by moving the functionality to the main new_user loop - Fix kick member if username is not set --- tg_bot/modules/welcome.py | 201 ++++++++++++++++++++------------------ 1 file changed, 106 insertions(+), 95 deletions(-) diff --git a/tg_bot/modules/welcome.py b/tg_bot/modules/welcome.py index 6d6d6472a..2fa4f963f 100644 --- a/tg_bot/modules/welcome.py +++ b/tg_bot/modules/welcome.py @@ -1,4 +1,5 @@ import html +import time from typing import Optional, List import requests as req @@ -18,6 +19,7 @@ escape_invalid_curly_brackets, ) from tg_bot.modules.log_channel import loggable +from tg_bot import BAN_STICKER VALID_WELCOME_FORMATTERS = [ "first", @@ -129,118 +131,127 @@ def cas_banned(userid): @run_async def new_member(bot: Bot, update: Update): chat = update.effective_chat # type: Optional[Chat] + new_members = update.effective_message.new_chat_members + message = update.effective_message should_welc, cust_welcome, welc_type = sql.get_welc_pref(chat.id) if should_welc: sent = None - new_members = update.effective_message.new_chat_members for new_mem in new_members: - # Check if the user is cas-banned - if not cas_banned(new_mem.id): - # Give the owner a special welcome - if new_mem.id == OWNER_ID: - update.effective_message.reply_text( - "Salve capo!" - ) - continue + # Check if the username is defined + if new_mem.username: + # Check if the user is cas-banned + if not cas_banned(new_mem.id): + # Give the owner a special welcome + if new_mem.id == OWNER_ID: + update.effective_message.reply_text( + "Salve capo!" + ) + continue - # Don't welcome yourself - elif new_mem.id == bot.id: - continue + # Don't welcome yourself + elif new_mem.id == bot.id: + continue - else: - # Muting new users - bot.restrict_chat_member( - chat.id, new_mem.id, can_send_messages=False - ) + else: + # Muting new users + bot.restrict_chat_member( + chat.id, new_mem.id, can_send_messages=False + ) - # If welcome message is media, send with appropriate function - if ( - welc_type != sql.Types.TEXT - and welc_type != sql.Types.BUTTON_TEXT - ): - ENUM_FUNC_MAP[welc_type](chat.id, cust_welcome) - return - # else, move on - first_name = ( - new_mem.first_name or "PersonWithNoName" - ) # edge case of empty name - occurs for some bugs. - - if cust_welcome: - if new_mem.last_name: - fullname = "{} {}".format(first_name, new_mem.last_name) + # If welcome message is media, send with appropriate function + if ( + welc_type != sql.Types.TEXT + and welc_type != sql.Types.BUTTON_TEXT + ): + ENUM_FUNC_MAP[welc_type](chat.id, cust_welcome) + return + # else, move on + first_name = ( + new_mem.first_name or "PersonWithNoName" + ) # edge case of empty name - occurs for some bugs. + + if cust_welcome: + if new_mem.last_name: + fullname = "{} {}".format(first_name, new_mem.last_name) + else: + fullname = first_name + count = chat.get_members_count() + mention = mention_markdown(new_mem.id, first_name) + if new_mem.username: + username = "@" + escape_markdown(new_mem.username) + else: + username = mention + + valid_format = escape_invalid_curly_brackets( + cust_welcome, VALID_WELCOME_FORMATTERS + ) + res = valid_format.format( + first=escape_markdown(first_name), + last=escape_markdown(new_mem.last_name or first_name), + fullname=escape_markdown(fullname), + username=username, + mention=mention, + count=count, + chatname=escape_markdown(chat.title), + id=new_mem.id, + ) + buttons = sql.get_welc_buttons(chat.id) + keyb = build_keyboard(buttons) else: - fullname = first_name - count = chat.get_members_count() - mention = mention_markdown(new_mem.id, first_name) - if new_mem.username: - username = "@" + escape_markdown(new_mem.username) - else: - username = mention + res = sql.DEFAULT_WELCOME.format(first=first_name) + keyb = [] - valid_format = escape_invalid_curly_brackets( - cust_welcome, VALID_WELCOME_FORMATTERS - ) - res = valid_format.format( - first=escape_markdown(first_name), - last=escape_markdown(new_mem.last_name or first_name), - fullname=escape_markdown(fullname), - username=username, - mention=mention, - count=count, - chatname=escape_markdown(chat.title), - id=new_mem.id, - ) - buttons = sql.get_welc_buttons(chat.id) - keyb = build_keyboard(buttons) - else: - res = sql.DEFAULT_WELCOME.format(first=first_name) - keyb = [] + keyboard = InlineKeyboardMarkup(keyb) - keyboard = InlineKeyboardMarkup(keyb) + sent = send( + update, + res, + keyboard, + sql.DEFAULT_WELCOME.format(first=first_name), + ) # type: Optional[Message] - sent = send( - update, - res, - keyboard, - sql.DEFAULT_WELCOME.format(first=first_name), - ) # type: Optional[Message] + else: + # BEGINNING THE BAN + log = ( + "CAS BAN:" "\n#CASBAN" "\nUser: {}".format(new_mem.id) + ) - else: - # BEGINNING THE BAN - log = ( - "CAS BAN:" "\n#CASBAN" "\nUser: {}".format(new_mem.id) - ) + reason = "Ban via CAS api query" - reason = "Ban via CAS api query" + if reason: + log += "\nMotivo: {}".format(reason) - if reason: - log += "\nMotivo: {}".format(reason) + user_id = new_mem.id + try: + chat.kick_member(user_id) + bot.send_sticker(chat.id, BAN_STICKER) # banhammer electus sticker + message.reply_text("BANNATO VIA SISTEMA AUTOMATICO CAS!") + return log + except BadRequest as excp: + if excp.message == "Reply message not found": + # Do not reply + message.reply_text("BANNATO!", quote=False) + return log + else: + LOGGER.warning(update) + LOGGER.exception( + "ERROR in CAS banning user %s in chat %s (%s) due to %s", + user_id, + chat.title, + chat.id, + excp.message, + ) + message.reply_text( + "Diamine, non riesco a bannare questo utente." + ) + else: + # Kicking the user because of the username user_id = new_mem.id - try: - chat.kick_member(user_id) - bot.send_sticker(chat.id, BAN_STICKER) # banhammer electus sticker - message.reply_text("BANNATO VIA SISTEMA AUTOMATICO CAS!") - return log - - except BadRequest as excp: - if excp.message == "Reply message not found": - # Do not reply - message.reply_text("BANNATO!", quote=False) - return log - else: - LOGGER.warning(update) - LOGGER.exception( - "ERROR in CAS banning user %s in chat %s (%s) due to %s", - user_id, - chat.title, - chat.id, - excp.message, - ) - message.reply_text( - "Diamine, non riesco a bannare questo utente." - ) + chat.kick_member(user_id, until_date=time.time() + 300) + bot.send_sticker(chat.id, BAN_STICKER) # banhammer electus sticker + message.reply_text("L'utente non ha uno username, quindi รจ stato rimosso.") prev_welc = sql.get_clean_pref(chat.id) if prev_welc: