def check_and_ban(update, user_id, should_message=True): chat = update.effective_chat message = update.effective_message try: if spam_watch is not None: spam_watch_ban = spam_watch.get_ban(user_id) if spam_watch_ban: chat.kick_member(user_id) if should_message: message.reply_markdown( "**This user is detected as spam bot by SpamWatch and have been removed!**\n\nPlease visit @SpamWatchSupport to appeal!") return else: return except Exception as e: print(e) pass if sql.is_user_gbanned(user_id): update.effective_chat.kick_member(user_id) if should_message: update.effective_message.reply_text( "Alert: This user is globally banned.\n" "*bans them from here*.\n" "PM my master for unban")
def left_member(update: Update, context: CallbackContext): bot = context.bot chat = update.effective_chat user = update.effective_user should_goodbye, cust_goodbye, goodbye_type = sql.get_gdbye_pref(chat.id) if user.id == bot.id: return if should_goodbye: left_mem = update.effective_message.left_chat_member if left_mem: if spam_watch is None: sw_ban = spam_watch.get_ban(left_mem.id) if sw_ban: return # Ignore bot being kicked if left_mem.id == bot.id: return # Give the owner a special goodbye if left_mem.id == OWNER_ID: update.effective_message.reply_text("Oi! Genos! He left..") return # Give the devs a special goodbye elif left_mem.id in DEV_USERS: update.effective_message.reply_text( "See you later at the Hero's Union!") return # if media goodbye, use appropriate function for it if goodbye_type not in (sql.Types.TEXT, sql.Types.BUTTON_TEXT): ENUM_FUNC_MAP[goodbye_type](chat.id, cust_goodbye) return # edge case of empty name - occurs for some bugs. first_name = left_mem.first_name or "PersonWithNoName" if cust_goodbye: if cust_goodbye == sql.DEFAULT_GOODBYE: cust_goodbye = random.choice( sql.DEFAULT_GOODBYE_MESSAGES).format( first=escape_markdown(first_name)) if left_mem.last_name: fullname = escape_markdown( f"{first_name} {left_mem.last_name}") else: fullname = escape_markdown(first_name) count = chat.get_members_count() mention = mention_markdown(left_mem.id, first_name) if left_mem.username: username = "******" + escape_markdown(left_mem.username) else: username = mention valid_format = escape_invalid_curly_brackets( cust_goodbye, VALID_WELCOME_FORMATTERS) res = valid_format.format( first=escape_markdown(first_name), last=escape_markdown(left_mem.last_name or first_name), fullname=escape_markdown(fullname), username=username, mention=mention, count=count, chatname=escape_markdown(chat.title), id=left_mem.id) buttons = sql.get_gdbye_buttons(chat.id) keyb = build_keyboard(buttons) else: res = random.choice( sql.DEFAULT_GOODBYE_MESSAGES).format(first=first_name) keyb = [] keyboard = InlineKeyboardMarkup(keyb) send( update, res, keyboard, random.choice( sql.DEFAULT_GOODBYE_MESSAGES).format(first=first_name))