def kick(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 if user_can_ban(chat, user, context.bot.id) is False: message.reply_text("You don't have enough rights to kick users!") return "" 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("Yeahh... let's start kicking admins?") 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: # context.bot.send_sticker(chat.id, BAN_STICKER) # banhammer marie sticker context.bot.sendMessage( chat.id, "Untill we meet again {}.".format( mention_html(member.user.id, member.user.first_name)), 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 set_title(update, context): args = context.args chat = update.effective_chat message = update.effective_message user_id, title = extract_user_and_text(message, args) try: user_member = chat.get_member(user_id) except Exception: return if not user_id: message.reply_text("You don't seem to be referring to a user.") return if user_member.status == "creator": message.reply_text( "This person CREATED the chat, how can i set custom title for him?" ) return if not user_member.status == "administrator": message.reply_text( "Can't set title for non-admins!\nPromote them first to set custom title!" ) return if user_id == context.bot.id: message.reply_text( "I can't set my own title myself! Get the one who made me admin to do it for me." ) return if not title: message.reply_text("Setting blank title doesn't do anything!") return if len(title) > 16: message.reply_text( "The title length is longer than 16 characters.\nTruncating it to 16 characters." ) try: context.bot.set_chat_administrator_custom_title(chat.id, user_id, title) message.reply_text( "Sucessfully set title for <b>{}</b> to <code>{}</code>!".format( user_member.user.first_name or user_id, title[:16] ), parse_mode=ParseMode.HTML, ) except BadRequest: message.reply_text("I can't set custom title for admins that I didn't promote!")
def unban(update, context): message = update.effective_message # type: Optional[Message] user = update.effective_user # type: Optional[User] chat = update.effective_chat # type: Optional[Chat] args = context.args if user_can_ban(chat, user, context.bot.id) is False: message.reply_text( "You don't have enough rights to unban people here!") return "" 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 user_id == context.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( "Why are you trying to unban someone who's already in this chat?") return "" chat.unban_member(user_id) message.reply_text("Done, they can join again!") 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 warn_user(update, context): message = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] warner = update.effective_user # type: Optional[User] args = context.args 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 temp_mute(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 if user_can_ban(chat, user, context.bot.id) == False: message.reply_text( "You don't have enough rights to restrict someone from talking!" ) return "" 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 == context.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: context.bot.restrict_chat_member( chat.id, user_id, until_date=mutetime, permissions=ChatPermissions(can_send_messages=False), ) message.reply_text("shut up! 🤐 Taped 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("shut up! 🤐 Taped 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 ""
def ban(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 if user_can_ban(chat, user, context.bot.id) is False: message.reply_text("You don't have enough rights to ban users!") return "" user_id, reason = extract_user_and_text(message, args) if not user_id: message.reply_text("Dude atleast refer some user to ban!") 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'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, are you crazy or wot?") 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.send_sticker(chat.id, BAN_STICKER) # banhammer marie sticker context.bot.sendMessage( chat.id, "let {} walk the plank.".format( mention_html(member.user.id, member.user.first_name)), 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_ban(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 if user_can_ban(chat, user, context.bot.id) is False: message.reply_text( "You don't have enough rights to temporarily ban someone!") return "" user_id, reason = extract_user_and_text(message, args) if not user_id: message.reply_text("Dude! atleast refer some user to ban...") 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("Wow! let's start banning Admins themselves?...") return "" if user_id == context.bot.id: message.reply_text("I'm not gonna BAN myself, are you crazy or wot?") 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) # context.bot.send_sticker(chat.id, BAN_STICKER) # banhammer marie 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( "Goodbye.. we'll meet after {}.".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 gban(update, context): message = update.effective_message chat = update.effective_chat args = context.args 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 if user_id == OWNER_ID: message.reply_text("Nice try -_- but I'm never gonna gban him.") return if int(user_id) in SUDO_USERS: message.reply_text( "I spy, with my little eye... a sudo user war! Why are you guys turning on 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 == context.bot.id: message.reply_text( "-_- So funny, lets gban myself why don't I? Nice try.") return try: user_chat = context.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 user_chat.first_name == "": message.reply_text( "This is a deleted account! no point to gban them...") 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] bannerid = banner.id bannername = banner.first_name new_reason = ( f"{new_reason} // GBanned by {bannername} banner id: {bannerid}" ) context.bot.sendMessage( MESSAGE_DUMP, "<b>Global Ban Reason Update</b>" "\n<b>Sudo Admin:</b> {}" "\n<b>User:</b> {}" "\n<b>ID:</b> <code>{}</code>" "\n<b>Previous Reason:</b> {}" "\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, old_reason, new_reason, ), parse_mode=ParseMode.HTML, ) 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: message.reply_text( "This user is already gbanned, but had no reason set; I've gone and updated it!" ) return message.reply_text( f"<b>Beginning of Global Ban for</b> {mention_html(user_chat.id, user_chat.first_name)}" f"\n<b>With ID</b>: <code>{user_chat.id}</code>" f"\n<b>Reason</b>: <code>{reason or 'No reason given'}</code>", parse_mode=ParseMode.HTML, ) banner = update.effective_user bannerid = banner.id bannername = banner.first_name reason = f"{reason} // GBanned by {bannername} banner id: {bannerid}" context.bot.sendMessage( MESSAGE_DUMP, "<b>New 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), user_chat.id, reason or "No reason given", ), parse_mode=ParseMode.HTML, ) try: context.bot.kick_chat_member(chat.id, user_chat.id) except BadRequest as excp: if excp.message in GBAN_ERRORS: pass sql.gban_user(user_id, user_chat.username or user_chat.first_name, reason)