def kick(update: Update, context: CallbackContext) -> str: chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] message = update.effective_message # type: Optional[Message] args = message.text.split(" ") user_id, reason = extract_user_and_text(message, args) if not user_id: return "" try: member = chat.get_member(user_id) except BadRequest as excp: if excp.message == "User not found": message.reply_text("I can't seem to find this user") return "" else: raise if is_user_ban_protected(chat, user_id): message.reply_text( "I'm not gonna kick an admin... Though I reckon it'd be pretty funny." ) return "" if user_id == context.bot.id: message.reply_text("Yeahhh I'm not gonna do that") return "" res = chat.unban_member(user_id) # unban on current user = kick if res: log = "<b>{}:</b>" \ "\n#KICKED" \ "\n<b>• Admin:</b> {}" \ "\n<b>• User:</b> {}" \ "\n<b>• ID:</b> <code>{}</code>".format(html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), user_id) reply = "{} has been kicked!".format( mention_html(member.user.id, member.user.first_name)) if reason: log += "\n<b>• Reason:</b> {}".format(reason) reply += "\n<b>Reason:</b> <i>{}</i>".format(reason) context.bot.send_sticker(chat.id, BAN_STICKER) # banhammer hercules sticker message.reply_text(reply, parse_mode=ParseMode.HTML) return log else: message.reply_text("Well damn, I can't kick that user.") return ""
def sban(update: Update, context: CallbackContext) -> str: chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] message = update.effective_message # type: Optional[Message] args = message.text.split(" ") update.effective_message.delete() user_id, reason = extract_user_and_text(message, args) if not user_id: return "" try: member = chat.get_member(user_id) except BadRequest as excp: if excp.message == "User not found": return "" else: raise if is_user_ban_protected(chat, user_id, member): return "" if user_id == context.bot.id: return "" log = "<b>{}:</b>" \ "\n#SILENT_BAN" \ "\n<b>• Admin:</b> {}" \ "\n<b>• User:</b> {}" \ "\n<b>• ID:</b> <code>{}</code>".format(html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), user_id) if reason: log += "\n<b>• Reason:</b> {}".format(reason) try: chat.kick_member(user_id) return log except BadRequest as excp: if excp.message == "Reply message not found": return log else: LOGGER.warning(update) LOGGER.exception("ERROR banning user %s in chat %s (%s) due to %s", user_id, chat.title, chat.id, excp.message) return ""
def temp_ban(update: Update, context: CallbackContext) -> str: chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] message = update.effective_message # type: Optional[Message] args = message.text.split(" ") user_id, reason = extract_user_and_text(message, args) if not user_id: message.reply_text("You don't seem to be referring to a user.") return "" try: member = chat.get_member(user_id) except BadRequest as excp: if excp.message == "User not found": message.reply_text("I can't seem to find this user") return "" else: raise if is_user_ban_protected(chat, user_id, member): message.reply_text("I really wish I could ban admins...") return "" if user_id == context.bot.id: message.reply_text("I'm not gonna BAN myself, are you crazy?") return "" if not reason: message.reply_text( "You haven't specified a time to ban this user for!") return "" split_reason = reason.split(None, 1) time_val = split_reason[0].lower() if len(split_reason) > 1: reason = split_reason[1] else: reason = "" bantime = extract_time(message, time_val) if not bantime: return "" log = "<b>{}:</b>" \ "\n#TEMPBAN" \ "\n<b>• Admin:</b> {}" \ "\n<b>• User:</b> {}" \ "\n<b>• ID:</b> <code>{}</code>" \ "\n<b>• Time:</b> {}".format(html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), user_id, time_val) if reason: log += "\n<b>• Reason:</b> {}".format(reason) try: chat.kick_member(user_id, until_date=bantime) context.bot.send_sticker(update.effective_chat.id, BAN_STICKER) # banhammer marie sticker reply = "{} has been temporarily banned for {}!".format( mention_html(member.user.id, member.user.first_name), time_val) message.reply_text(reply, parse_mode=ParseMode.HTML) return log except BadRequest as excp: if excp.message == "Reply message not found": # Do not reply message.reply_text( "Banned! User will be banned for {}.".format(time_val), quote=False) return log else: LOGGER.warning(update) LOGGER.exception("ERROR banning user %s in chat %s (%s) due to %s", user_id, chat.title, chat.id, excp.message) message.reply_text("Well damn, I can't ban that user.") return ""
def ban(update: Update, context: CallbackContext) -> str: chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] message = update.effective_message # type: Optional[Message] args = message.text.split(" ") user_id, reason = extract_user_and_text(message, args) if not user_id: message.reply_text("You don't seem to be referring to a user.") return "" try: member = chat.get_member(user_id) except BadRequest as excp: if excp.message == "User not found": message.reply_text("I can't seem to find this user") return "" else: raise if is_user_ban_protected(chat, user_id, member): message.reply_text( "Why would I ban an admin? That sounds like a pretty dumb idea.") return "" if user_id == context.bot.id: message.reply_text("I'm not gonna BAN myself, are you crazy?") return "" log = "<b>{}:</b>" \ "\n#BANNED" \ "\n<b>• Admin:</b> {}" \ "\n<b>• User:</b> {}" \ "\n<b>• ID:</b> <code>{}</code>".format(html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), user_id) reply = "{} has been banned!".format( mention_html(member.user.id, member.user.first_name)) if reason: log += "\n<b>• Reason:</b> {}".format(reason) reply += "\n<b>Reason:</b> <i>{}</i>".format(reason) try: chat.kick_member(user_id) context.bot.send_sticker(update.effective_chat.id, BAN_STICKER) # banhammer hercules sticker message.reply_text(reply, parse_mode=ParseMode.HTML) return log except BadRequest as excp: if excp.message == "Reply message not found": # Do not reply message.reply_text('Banned!', quote=False) return log else: LOGGER.warning(update) LOGGER.exception("ERROR banning user %s in chat %s (%s) due to %s", user_id, chat.title, chat.id, excp.message) message.reply_text("Well damn, I can't ban that user.") return ""
def new_member(update: Update, context: CallbackContext): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] msg = update.effective_message # type: Optional[Message] chat_name = chat.title or chat.first or chat.username # type: Optional:[chat name] should_welc, cust_welcome, welc_type, welc_caption = sql.get_welc_pref(chat.id) welc_mutes = sql.welcome_mutes(chat.id) human_checks = sql.get_human_checks(user.id, chat.id) prev_welc = sql.get_clean_pref(chat.id) media = False no_mute = False try: for mem in msg.new_chat_members: user_id = mem.id except: LOGGER.log("User being added or no ID found for user.") gban_checks = get_gbanned_user(user_id) join_log = sql.get_join_event_pref(chat.id) join_members = msg.new_chat_members if should_welc: sent = None new_members = msg.new_chat_members for new_mem in new_members: spamwatch_banned = client.get_ban(new_mem.id) user_add = context.bot.get_chat_member(chat.id, user.id) # edge case of empty name - occurs for some bugs. first_name = new_mem.first_name or "PersonWithNoName" # Give the owner a special welcome log = "" if join_log == True: log += "<b>{}:</b>" \ "\n#WELCOME" \ "\n<b>A new user has joined:</b>" \ "\n<b>• User:</b> {}" \ "\n<b>• ID:</b> <code>{}</code>".format(escape(chat.title), mention_html(mem.id, mem.first_name), mem.id) if user.id != mem.id: log += "\n<b>• Added by:</b> {}".format(mention_html(user.id, user.first_name)) if new_mem.id == OWNER_ID: update.effective_message.reply_text("Master is in the houseeee, let's get this party started!") continue #### BAN CHECKERS #### # Ignore welc messages for gbanned users if gban_checks: continue # Ignore welc messages for SpamWatch banned users if spamwatch_banned: continue # Make bot greet admins elif new_mem.id == context.bot.id: update.effective_message.reply_text("Hey {}, I'm {}! Thank you for adding me to {}" " and be sure to check /help in PM for more commands and tricks!".format(user.first_name, context.bot.first_name, chat_name)) else: # If welcome message is media, send with appropriate function if welc_type != sql.Types.TEXT and welc_type != sql.Types.BUTTON_TEXT: if welc_type == sql.Types.PHOTO: media = True type = 'photo' if welc_caption: res = format_welcome_message(welc_caption, first_name, chat, new_mem) buttons = sql.get_welc_buttons(chat.id) keyb = build_keyboard(buttons) keyboard = InlineKeyboardMarkup(keyb) sent = send(update, context, cust_welcome, keyboard, sql.DEFAULT_WELCOME.format(first=first_name, chatname=chat_name), res, type) if sent: sql.set_clean_welcome(chat.id, sent.message_id) elif welc_type == sql.Types.VIDEO: media = True type = 'video' if welc_caption: res = format_welcome_message(welc_caption, first_name, chat, new_mem) buttons = sql.get_welc_buttons(chat.id) keyb = build_keyboard(buttons) keyboard = InlineKeyboardMarkup(keyb) sent = send(update, context, cust_welcome, keyboard, sql.DEFAULT_WELCOME.format(first=first_name, chatname=chat_name), res, type) if sent: sql.set_clean_welcome(chat.id, sent.message_id) elif welc_type == sql.Types.DOCUMENT: media = True type = 'document' if welc_caption: res = format_welcome_message( welc_caption, first_name, chat, new_mem) buttons = sql.get_welc_buttons(chat.id) keyb = build_keyboard(buttons) keyboard = InlineKeyboardMarkup(keyb) sent = send(update, context, cust_welcome, keyboard, sql.DEFAULT_WELCOME.format(first=first_name, chatname=chat_name), res, type) if sent: sql.set_clean_welcome(chat.id, sent.message_id) else: media = True ENUM_FUNC_MAP[welc_type](chat.id, cust_welcome) # else, move on if media == False: if welc_caption: res = format_welcome_message(welc_caption, first_name, chat, new_mem) buttons = sql.get_welc_buttons(chat.id) keyb = build_keyboard(buttons) else: res = sql.DEFAULT_WELCOME.format(first=first_name, chatname=chat_name) keyb = [] keyboard = InlineKeyboardMarkup(keyb) sent = send(update, context, res, keyboard, sql.DEFAULT_WELCOME.format(first=first_name, chatname=chat_name)) # type: Optional[Message] #User exception from mutes: if is_user_ban_protected(chat, new_mem.id, chat.get_member(new_mem.id)) or human_checks or gban_checks: no_mute = True if not no_mute: #Join welcome: soft mute if welc_mutes == "soft": context.bot.restrict_chat_member(chat.id, new_mem.id, WELCOME_PERMISSIONS_SOFT, until_date=(int(time.time() + 24 * 60 * 60))) #Join welcome: strong mute if welc_mutes == "strong": mute_message = msg.reply_text("Hey {} (`{}`),\nClick the button below to prove you're human:".format(new_mem.first_name, new_mem.id), reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton(text="Tap here to speak", callback_data="user_join_({})".format(new_mem.id))]]), parse_mode=ParseMode.MARKDOWN) context.bot.restrict_chat_member(chat.id, new_mem.id, WELCOME_PERMISSIONS_STRONG) #Join welcome: aggressive mute elif welc_mutes == "aggressive": mute_message = msg.reply_text("Hey {} (`{}`),\nClick the button below to prove you're human:".format(new_mem.first_name, new_mem.id), reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton(text="Tap here to speak", callback_data="user_join_({})".format(new_mem.id))]]), parse_mode=ParseMode.MARKDOWN) context.bot.restrict_chat_member(chat.id, new_mem.id, WELCOME_PERMISSIONS_AGGRESSIVE) delete_join(update, context) if prev_welc: try: context.bot.delete_message(chat.id, prev_welc) except BadRequest as excp: pass if sent: sql.set_clean_welcome(chat.id, sent.message_id) if not human_checks and welc_mutes == "aggressive": t = threading.Thread(target=aggr_mute_check, args=( context.bot, chat, mute_message.message_id, new_mem.id,)) t.start() return log return ""
def rban(update: Update, context: CallbackContext): message = update.effective_message #chat_name = chat.title or chat.first or chat.username chat = update.effective_chat # type: Optional[Chat] args = message.text.split(" ") if not args: message.reply_text("You don't seem to be referring to a chat/user.") return user_id, chat_id = extract_user_and_text(message, args) if not user_id: message.reply_text("You don't seem to be referring to a user.") return elif not chat_id: message.reply_text("You don't seem to be referring to a chat.") return try: chat = context.bot.get_chat(chat_id.split()[0]) except BadRequest as excp: if excp.message == "Chat not found": message.reply_text( "Chat not found! Make sure you entered a valid chat ID and I'm part of that chat." ) return else: raise if chat.type == 'private': message.reply_text("I'm sorry, but that's a private chat!") return if not is_bot_admin(chat, context.bot.id) or not chat.get_member( context.bot.id).can_restrict_members: message.reply_text( "I can't restrict people there! Make sure I'm admin and can ban users." ) return try: member = chat.get_member(user_id) except BadRequest as excp: if excp.message == "User not found": message.reply_text("I can't seem to find this user") return else: raise if is_user_ban_protected(chat, user_id, member): message.reply_text("I really wish I could ban admins...") return if user_id == context.bot.id: message.reply_text("I'm not gonna BAN myself, are you crazy?") return try: chat.kick_member(user_id) rbanning = "Hunting again in the wild!\n{} has been remotely banned from {}! \n".format( mention_html(member.user.id, member.user.first_name), (chat.title or chat.first or chat.username)) context.bot.send_sticker(update.effective_chat.id, BAN_STICKER) message.reply_text(rbanning, parse_mode=ParseMode.HTML) except BadRequest as excp: if excp.message == "Reply message not found": # Do not reply message.reply_text('Banned!', quote=False) elif excp.message in RBAN_ERRORS: message.reply_text(excp.message) else: LOGGER.warning(update) LOGGER.exception("ERROR banning user %s in chat %s (%s) due to %s", user_id, chat.title, chat.id, excp.message) message.reply_text("Well damn, I can't ban that user.")