def ban(update: Update, context: CallbackContext) -> str: chat = update.effective_chat user = update.effective_user message = update.effective_message log_message = "" bot = context.bot args = context.args user_id, reason = extract_user_and_text(message, args) if not user_id: message.reply_text("I doubt that's a user.") return log_message try: member = chat.get_member(user_id) except BadRequest as excp: if excp.message != "User not found": raise message.reply_text("Can't seem to find this person.") return log_message if user_id == bot.id: message.reply_text("Oh yeah, ban myself, noob!") return log_message if is_user_ban_protected(chat, user_id, member) and user not in DEV_USERS: if user_id == OWNER_ID: message.reply_text("Trying to put me against a God level disaster huh?") elif user_id in DEV_USERS: message.reply_text("I can't act against our own.") elif user_id in DRAGONS: message.reply_text( "Fighting this Dragon here will put civilian lives at risk." ) elif user_id in DEMONS: message.reply_text( "Bring an order from Heroes association to fight a Demon disaster." ) elif user_id in TIGERS: message.reply_text( "Bring an order from Heroes association to fight a Tiger disaster." ) elif user_id in WOLVES: message.reply_text("Wolf abilities make them ban immune!") else: message.reply_text("This user has immunity and cannot be banned.") return log_message if message.text.startswith("/s"): silent = True if not can_delete(chat, context.bot.id): return "" else: silent = False log = ( f"<b>{html.escape(chat.title)}:</b>\n" f"#{'S' if silent else ''}BANNED\n" f"<b>Admin:</b> {mention_html(user.id, html.escape(user.first_name))}\n" f"<b>User:</b> {mention_html(member.user.id, html.escape(member.user.first_name))}" ) if reason: log += "\n<b>Reason:</b> {}".format(reason) try: chat.kick_member(user_id) if silent: if message.reply_to_message: message.reply_to_message.delete() message.delete() return log # bot.send_sticker(chat.id, BAN_STICKER) # banhammer marie sticker reply = ( f"<code>❕</code><b>Ban Event</b>\n" f"<code> </code><b>• User:</b> {mention_html(member.user.id, html.escape(member.user.first_name))}" ) if reason: reply += f"\n<code> </code><b>• Reason:</b> \n{html.escape(reason)}" bot.sendMessage(chat.id, reply, parse_mode=ParseMode.HTML, quote=False) return log except BadRequest as excp: if excp.message == "Reply message not found": # Do not reply if silent: return log 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("Uhm...that didn't work...") return log_message
def dkick(update, context): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] message = update.effective_message # type: Optional[Message] args = context.args bot = context.bot #if user_can_ban(chat, user, context.bot.id) is False: # message.reply_text("You don't have enough rights to kick users!") # return "" if message.reply_to_message: user = update.effective_user # type: Optional[User] chat = update.effective_chat # type: Optional[Chat] if can_delete(chat, bot.id): update.effective_message.reply_to_message.delete() else: message.reply_text("You have to reply to a message to delete it and kick the user.") return "" user_id, reason = extract_user_and_text(message, args) 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("Yeahh... let's start kicking admins?") return "" if user_id == context.bot.id: message.reply_text("Yeahhh I'm not gonna do that") return "" if user_id == 777000 or user_id == 1087968824: message.reply_text(str(user_id) + " is an account reserved for telegram, I cannot kick it!") return "" res = chat.unban_member(user_id) # unban on current user = kick if res: context.bot.sendMessage( chat.id, "Admin {} has successfully kicked {} in <b>{}</b>!".format( mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), html.escape(chat.title)), parse_mode=ParseMode.HTML, ) log = ( "<b>{}:</b>" "\n#KICKED" "\n<b>Admin:</b> {}" "\n<b>User:</b> {} (<code>{}</code>)".format( html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), member.user.id, ) ) if reason: log += "\n<b>Reason:</b> {}".format(reason) return log else: message.reply_text("Get Out!.") return ""
def del_lockables(update, context): chat = update.effective_chat # type: Optional[Chat] message = update.effective_message # type: Optional[Message] for lockable, filter in LOCK_TYPES.items(): if lockable == "rtl": if sql.is_locked(chat.id, lockable) and can_delete( chat, context.bot.id): if message.caption: check = ad.detect_alphabet(u"{}".format(message.caption)) if "ARABIC" in check: try: message.delete() except BadRequest as excp: if excp.message == "Message to delete not found": pass else: LOGGER.exception("ERROR in lockables") break if message.text: check = ad.detect_alphabet(u"{}".format(message.text)) if "ARABIC" in check: try: message.delete() except BadRequest as excp: if excp.message == "Message to delete not found": pass else: LOGGER.exception("ERROR in lockables") break continue if lockable == "button": if sql.is_locked(chat.id, lockable) and can_delete( chat, context.bot.id): if message.reply_markup and message.reply_markup.inline_keyboard: try: message.delete() except BadRequest as excp: if excp.message == "Message to delete not found": pass else: LOGGER.exception("ERROR in lockables") break continue if lockable == "inline": if sql.is_locked(chat.id, lockable) and can_delete( chat, context.bot.id): if message and message.via_bot: try: message.delete() except BadRequest as excp: if excp.message == "Message to delete not found": pass else: LOGGER.exception("ERROR in lockables") break continue if (filter(update) and sql.is_locked(chat.id, lockable) and can_delete(chat, context.bot.id)): if lockable == "bots": new_members = update.effective_message.new_chat_members for new_mem in new_members: if new_mem.is_bot: if not is_bot_admin(chat, context.bot.id): send_message( update.effective_message, "I see a bot and I've been told to stop them from joining..." "but I'm not admin!", ) return chat.kick_member(new_mem.id) send_message( update.effective_message, "Only admins are allowed to add bots in this chat! Get outta here.", ) break else: try: message.delete() except BadRequest as excp: if excp.message == "Message to delete not found": pass else: LOGGER.exception("ERROR in lockables") break
def dban(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 = context.args bot = context.bot #if user_can_ban(chat, user, context.bot.id) is False: # message.reply_text("You don't have enough rights to ban users!") # return "" if message.reply_to_message: user = update.effective_user # type: Optional[User] chat = update.effective_chat # type: Optional[Chat] if can_delete(chat, bot.id): update.effective_message.reply_to_message.delete() else: message.reply_text("You have to reply to a message to delete it and ban the user.") return "" user_id, reason = extract_user_and_text(message, args) 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'm not gonna ban an admin, don't make fun of yourself!") return "" if user_id == context.bot.id: message.reply_text("I'm not gonna ban myself, that's pretty dumb idea!") return "" if user_id == 777000 or user_id == 1087968824: message.reply_text(str(user_id) + " is an account reserved for telegram, I cannot ban it!") return "" log = ( "<b>{}:</b>" "\n#BANNED" "\n<b>Admin:</b> {}" "\n<b>User:</b> {} (<code>{}</code>)".format( html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), member.user.id, ) ) if reason: log += "\n<b>Reason:</b> {}".format(reason) try: chat.kick_member(user_id) context.bot.sendMessage( chat.id, "Admin {} has successfully banned {} in <b>{}</b>!.".format( mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), html.escape(chat.title) ), 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 lock(update, context) -> str: args = context.args chat = update.effective_chat user = update.effective_user if (can_delete(chat, context.bot.id) or update.effective_message.chat.type == "private"): if len(args) >= 1: ltype = args[0].lower() if ltype in LOCK_TYPES: # Connection check conn = connected(context.bot, update, chat, user.id, need_admin=True) if conn: chat = dispatcher.bot.getChat(conn) chat_id = conn chat_name = chat.title text = "Locked {} for non-admins in {}!".format( ltype, chat_name) else: if update.effective_message.chat.type == "private": send_message( update.effective_message, "This command is meant to use in group not in PM", ) return "" chat = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title text = "Locked {} for non-admins!".format(ltype) sql.update_lock(chat.id, ltype, locked=True) send_message(update.effective_message, text, parse_mode="markdown") return ("<b>{}:</b>" "\n#LOCK" "\n<b>Admin:</b> {}" "\nLocked <code>{}</code>.".format( html.escape(chat.title), mention_html(user.id, user.first_name), ltype, )) elif ltype in LOCK_CHAT_RESTRICTION: # Connection check conn = connected(context.bot, update, chat, user.id, need_admin=True) if conn: chat = dispatcher.bot.getChat(conn) chat_id = conn chat_name = chat.title text = "Locked {} for all non-admins in {}!".format( ltype, chat_name) else: if update.effective_message.chat.type == "private": send_message( update.effective_message, "This command is meant to use in group not in PM", ) return "" chat = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title text = "Locked {} for all non-admins!".format(ltype) current_permission = context.bot.getChat(chat_id).permissions context.bot.set_chat_permissions( chat_id=chat_id, permissions=get_permission_list( eval(str(current_permission)), LOCK_CHAT_RESTRICTION[ltype.lower()], ), ) send_message(update.effective_message, text, parse_mode="markdown") return ("<b>{}:</b>" "\n#Permission_LOCK" "\n<b>Admin:</b> {}" "\nLocked <code>{}</code>.".format( html.escape(chat.title), mention_html(user.id, user.first_name), ltype, )) else: send_message( update.effective_message, "What are you trying to lock...? Try /locktypes for the list of lockables", ) else: send_message(update.effective_message, "What are you trying to lock...?") else: send_message( update.effective_message, "I am not administrator or haven't got enough rights.", ) return ""