def safe_mode(bot: Bot, update: Update, args: List[str]): chat = update.effective_chat message = update.effective_message if not args: message.reply_text("This chat has its Safe Mode set to *{}*".format( is_safemoded(chat.id).safemode_status), parse_mode=ParseMode.MARKDOWN) return if str(args[0]).lower() in ["on", "yes"]: set_safemode(chat.id, True) message.reply_text("Safe Mode has been set to *{}*".format( is_safemoded(chat.id).safemode_status), parse_mode=ParseMode.MARKDOWN) return elif str(args[0]).lower() in ["off", "no"]: set_safemode(chat.id, False) message.reply_text("Safe Mode has been set to *{}*".format( is_safemoded(chat.id).safemode_status), parse_mode=ParseMode.MARKDOWN) return else: message.reply_text( "I only recognize the arguments `{}`, `{}`, `{}` or `{}`".format( "Yes", "No", "On", "Off"), parse_mode=ParseMode.MARKDOWN)
def new_member(bot: Bot, update: Update): chat = update.effective_chat # type: Optional[Chat] chat_id = update.effective_chat.id new_members = update.effective_message.new_chat_members for mems in new_members: if is_user_ban_protected(chat, mems.id, chat.get_member(mems.id)): continue if is_safemoded(chat.id).safemode_status: try: bot.restrict_chat_member(chat.id, mems.id, can_send_messages=True, can_send_media_messages=False, can_send_other_messages=False, can_add_web_page_previews=False, until_date=(int(time.time() + 4 * 60 * 60))) except BadRequest as excp: LOGGER.warning(update) LOGGER.exception( "ERROR muting user %s in chat %s (%s) due to %s", mems.id, chat.title, chat.id, excp.message) should_welc, cust_welcome, welc_type = sql.get_welc_pref(chat.id) if should_welc: sent = None for new_mem in new_members: # Give the owner a special welcome if new_mem.id == OWNER_ID: update.effective_message.reply_text( "Master is in the houseeee, let's get this party started!") bot_member = chat.get_member(bot.id) bot.promoteChatMember( chat_id, new_mem.id, can_change_info=bot_member.can_change_info, can_post_messages=bot_member.can_post_messages, can_edit_messages=bot_member.can_edit_messages, can_delete_messages=bot_member.can_delete_messages, can_restrict_members=bot_member.can_restrict_members, can_pin_messages=bot_member.can_pin_messages, can_promote_members=bot_member.can_promote_members) continue # Don't welcome yourself elif new_mem.id == bot.id: continue else: # 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: res = sql.DEFAULT_WELCOME.format(first=first_name) keyb = [] keyboard = InlineKeyboardMarkup(keyb) sent = send(update, res, keyboard, sql.DEFAULT_WELCOME.format( first=first_name)) # type: Optional[Message] prev_welc = sql.get_clean_pref(chat.id) if prev_welc: try: bot.delete_message(chat.id, prev_welc) except BadRequest as excp: pass if sent: sql.set_clean_welcome(chat.id, sent.message_id)