def gkickreset(bot: Bot, update: Update, args: List[str]): message = update.effective_message user_id, value = extract_user_and_text(message, args) try: user_chat = bot.get_chat(user_id) except BadRequest as excp: if excp.message in GKICK_ERRORS: pass else: message.reply_text("GENERIC ERROR: {}".format(excp.message)) except TelegramError: pass if not user_id: message.reply_text("You do not seems to be referring to a user") return if int(user_id) in SUDO_USERS or int(user_id) in SUPPORT_USERS: message.reply_text("SUDOER: Irrelevant") return if int(user_id) == OWNER_ID: message.reply_text("OWNER: Irrelevant") return if user_id == bot.id: message.reply_text("It's me, n***a") return sql.gkick_reset(user_id) return
def kick(bot: Bot, update: Update, args: List[str]) -> str: chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] message = update.effective_message # type: Optional[Message] user_id, reason = extract_user_and_text(message, args) if not user_id or int(user_id) == 777000: 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("404 - user not found") 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 == bot.id: message.reply_text("hahahahahahaha nice try.. nope") return "" res = chat.unban_member(user_id) # unban on current user = kick if res: bot.send_sticker(chat.id, BAN_STICKER) # axel ban sticker keyboard = [] reply = "I've kicked {} ".format( mention_html(member.user.id, member.user.first_name)) message.reply_text(reply, reply_markup=keyboard, 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("Well damn, I can't kick that user.") return ""
def warn_user(bot: Bot, update: Update, args: List[str]) -> str: message = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] warner = update.effective_user # type: Optional[User] user_id, reason = extract_user_and_text(message, args) if user_id: if message.reply_to_message and message.reply_to_message.from_user.id == user_id: return warn(message.reply_to_message.from_user, chat, reason, message.reply_to_message, warner) else: return warn(chat.get_member(user_id).user, chat, reason, message, warner) else: message.reply_text("No user was designated!") return ""
def unban(bot: Bot, update: Update, args: List[str]) -> str: message = update.effective_message # type: Optional[Message] user = update.effective_user # type: Optional[User] chat = update.effective_chat # type: Optional[Chat] user_id, reason = extract_user_and_text(message, args) if not user_id or int(user_id) == 777000: 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("404 - user not found") return "" else: raise if user_id == bot.id: message.reply_text("How would I unban myself if I wasn't here...?") return "" if is_user_in_chat(chat, user_id): message.reply_text("User is already in the chat...") return "" chat.unban_member(user_id) message.reply_text("Yep, this user can join!") log = "<b>{}:</b>" \ "\n#UNBANNED" \ "\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
def rban(bot: Bot, update: Update, args: List[str]): message = update.effective_message #chat_name = chat.title or chat.first or chat.username chat = update.effective_chat # type: Optional[Chat] 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 = 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, bot.id) or not chat.get_member( 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 == 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)) bot.send_sticker(update.effective_chat.id, BAN_STICKER) keyboard = [] message.reply_text(rbanning, reply_markup=keyboard, 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.")
def runmute(bot: Bot, update: Update, args: List[str]): message = update.effective_message 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 = 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, bot.id) or not chat.get_member( bot.id).can_restrict_members: message.reply_text( "I can't unrestrict people there! Make sure I'm admin and can unban 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 there") return else: raise if is_user_in_chat(chat, user_id): if member.can_send_messages and member.can_send_media_messages \ and member.can_send_other_messages and member.can_add_web_page_previews: message.reply_text( "This user already has the right to speak in that chat.") return if user_id == bot.id: message.reply_text("I'm not gonna UNMUTE myself, I'm an admin there!") return try: bot.restrict_chat_member(chat.id, int(user_id), can_send_messages=True, can_send_media_messages=True, can_send_other_messages=True, can_add_web_page_previews=True) runmuting = "Well, I will let {} speak on {}!".format( mention_html(member.user.id, member.user.first_name), (chat.title or chat.first or chat.username)) keyboard = [] message.reply_text(runmuting, reply_markup=keyboard, parse_mode=ParseMode.HTML) except BadRequest as excp: if excp.message == "Reply message not found": # Do not reply message.reply_text('Unmuted!', quote=False) elif excp.message in RUNMUTE_ERRORS: message.reply_text(excp.message) else: LOGGER.warning(update) LOGGER.exception( "ERROR unmnuting 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 unmute that user.")
def runban(bot: Bot, update: Update, args: List[str]): message = update.effective_message 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 = 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, bot.id) or not chat.get_member( bot.id).can_restrict_members: message.reply_text( "I can't unrestrict people there! Make sure I'm admin and can unban 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 there") return else: raise if is_user_in_chat(chat, user_id): message.reply_text( "Why are you trying to remotely unban someone that's already in that chat?" ) return if user_id == bot.id: message.reply_text("I'm not gonna UNBAN myself, I'm an admin there!") return try: chat.unban_member(user_id) runbanning = "I am allowing {} to join {}!".format( mention_html(member.user.id, member.user.first_name), (chat.title or chat.first or chat.username)) keyboard = [] message.reply_text(runbanning, reply_markup=keyboard, parse_mode=ParseMode.HTML) except BadRequest as excp: if excp.message == "Reply message not found": # Do not reply message.reply_text('Unbanned!', quote=False) elif excp.message in RUNBAN_ERRORS: message.reply_text(excp.message) else: LOGGER.warning(update) LOGGER.exception( "ERROR unbanning 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 unban that user.")
def gban(bot: Bot, update: Update, args: List[str]): message = update.effective_message # type: Optional[Message] user = update.effective_user # type: Optional[User] user_id, reason = extract_user_and_text(message, args) if not user_id or int(user_id) == 777000: message.reply_text("You don't seem to be referring to a user.") return if int(user_id) in SUDO_USERS: message.reply_text("Now kiss each other!") return if int(user_id) in SUPPORT_USERS: message.reply_text( "OOOH someone's trying to gban a support user! *grabs popcorn*") return if user_id == bot.id: message.reply_text("Lets gban myself why don't I? Good one.") return try: user_chat = bot.get_chat(user_id) except BadRequest as excp: message.reply_text(excp.message) return if user_chat.type != 'private': message.reply_text("That's not a user!") return if sql.is_user_gbanned(user_id): if not reason: message.reply_text( "This user is already gbanned; I'd change the reason, but you haven't given me one..." ) return old_reason = sql.update_gban_reason( user_id, user_chat.username or user_chat.first_name, reason) user_id, new_reason = extract_user_and_text(message, args) if old_reason: banner = update.effective_user # type: Optional[User] send_to_list(bot, SUDO_USERS + SUPPORT_USERS, "<b>Emendation of Global Ban</b>" \ "\n#GBAN" \ "\n<b>Status:</b> <code>Amended</code>" \ "\n<b>Sudo Admin:</b> {}" \ "\n<b>User:</b> {}" \ "\n<b>ID:</b> <code>{}</code>" \ "\n<b>Previous Reason:</b> {}" \ "\n<b>Amended Reason:</b> {}".format(mention_html(banner.id, banner.first_name), mention_html(user_chat.id, user_chat.first_name or "Deleted Account"), user_chat.id, old_reason, new_reason), html=True) message.reply_text( "This user is already gbanned, for the following reason:\n" "<code>{}</code>\n" "I've gone and updated it with your new reason!".format( html.escape(old_reason)), parse_mode=ParseMode.HTML) else: banner = update.effective_user # type: Optional[User] send_to_list(bot, SUDO_USERS + SUPPORT_USERS, "<b>Emendation of Global Ban</b>" \ "\n#GBAN" \ "\n<b>Status:</b> <code>New reason</code>" \ "\n<b>Sudo Admin:</b> {}" \ "\n<b>User:</b> {}" \ "\n<b>ID:</b> <code>{}</code>" \ "\n<b>New Reason:</b> {}".format(mention_html(banner.id, banner.first_name), mention_html(user_chat.id, user_chat.first_name or "Deleted Account"), user_chat.id, new_reason), html=True) message.reply_text( "This user is already gbanned, but had no reason set; I've gone and updated it!" ) return starting = "Initiating global ban for {}...".format( mention_html(user_chat.id, user_chat.first_name or "Deleted Account")) keyboard = [] # message.reply_text(starting, reply_markup=keyboard, parse_mode=ParseMode.HTML) banner = update.effective_user # type: Optional[User] send_to_list(bot, SUDO_USERS + SUPPORT_USERS, "<b>Global Ban</b>" \ "\n#GBAN" \ "\n<b>Status:</b> <code>Enforcing</code>" \ "\n<b>Sudo Admin:</b> {}" \ "\n<b>User:</b> {}" \ "\n<b>ID:</b> <code>{}</code>" \ "\n<b>Reason:</b> {}".format(mention_html(banner.id, banner.first_name), mention_html(user_chat.id, user_chat.first_name or "Deleted Account"), user_chat.id, reason or "No reason given"), html=True) sql.gban_user(user_id, user_chat.username or user_chat.first_name, reason) chats = get_all_chats() for chat in chats: chat_id = chat.chat_id # Check if this group has disabled gbans if not sql.does_chat_gban(chat_id): continue try: bot.kick_chat_member(chat_id, user_id) except BadRequest as excp: if excp.message in GBAN_ERRORS: pass else: message.reply_text("Could not gban due to: {}".format( excp.message)) send_to_list(bot, SUDO_USERS + SUPPORT_USERS, "Could not gban due to: {}".format(excp.message)) sql.ungban_user(user_id) return except TelegramError: pass send_to_list(bot, SUDO_USERS + SUPPORT_USERS, "{} has been successfully gbanned!".format( mention_html(user_chat.id, user_chat.first_name or "Deleted Account")), html=True) message.reply_text("Person has been gbanned.")
def temp_ban(bot: Bot, update: Update, args: List[str]) -> str: chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] message = update.effective_message # type: Optional[Message] user_id, reason = extract_user_and_text(message, args) if not user_id or int(user_id) == 777000: 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("404 - user not found") return "" else: raise if is_user_ban_protected(chat, user_id, member): message.reply_text("Can't do that, user is admin..") return "" if user_id == bot.id: message.reply_text("hahahahahahaha nice try.. nope") 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#TEMP BANNED" \ "\n<b>Admin:</b> {}" \ "\n<b>User:</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), member.user.id, time_val) if reason: log += "\n<b>Reason:</b> {}".format(reason) try: chat.kick_member(user_id, until_date=bantime) bot.send_sticker(chat.id, BAN_STICKER) # axel ban sticker message.reply_text( "Banned! User will be banned for {}.".format(time_val)) 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(bot: Bot, update: Update, args: List[str]) -> str: chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] message = update.effective_message # type: Optional[Message] user_id, reason = extract_user_and_text(message, args) if not user_id or int(user_id) == 777000: 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("404 - user not found") return "" else: raise if is_user_ban_protected(chat, user_id, member): message.reply_text("Can't do that, user is admin..") return "" if user_id == bot.id: message.reply_text("hahahahahahaha nice try.. nope") 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) keyboard = [] bot.send_sticker(update.effective_chat.id, BAN_STICKER) #axel ban sticker reply = "I've banned {} ".format( mention_html(member.user.id, member.user.first_name)) message.reply_text(reply, reply_markup=keyboard, 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 temp_mute(bot: Bot, update: Update, args: List[str]) -> str: chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] message = update.effective_message # type: Optional[Message] 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_admin(chat, user_id, member): message.reply_text("I really wish I could mute admins...") return "" if user_id == bot.id: message.reply_text("I'm not gonna MUTE myself, are you crazy?") return "" if not reason: message.reply_text( "You haven't specified a time to mute 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 = "" mutetime = extract_time(message, time_val) if not mutetime: return "" log = "<b>{}:</b>" \ "\n#TEMP MUTED" \ "\n<b>Admin:</b> {}" \ "\n<b>User:</b> {}" \ "\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), time_val) if reason: log += "\n<b>Reason:</b> {}".format(reason) try: if member.can_send_messages is None or member.can_send_messages: bot.restrict_chat_member(chat.id, user_id, until_date=mutetime, can_send_messages=False) message.reply_text("Muted for {}!".format(time_val)) return log else: message.reply_text("This user is already muted.") except BadRequest as excp: if excp.message == "Reply message not found": # Do not reply message.reply_text("Muted for {}!".format(time_val), quote=False) return log else: LOGGER.warning(update) LOGGER.exception("ERROR muting 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 mute that user.") return ""