def warns(update: Update, context: CallbackContext): args = context.args message: Optional[Message] = update.effective_message chat: Optional[Chat] = update.effective_chat user_id = extract_user(message, args) or update.effective_user.id result = sql.get_warns(user_id, chat.id) if result and result[0] != 0: num_warns, reasons = result limit, soft_warn = sql.get_warn_setting(chat.id) if reasons: text = ( f"This user has {num_warns}/{limit} warns, for the following reasons:" ) for reason in reasons: text += f"\n • {reason}" msgs = split_message(text) for msg in msgs: update.effective_message.reply_text(msg) else: update.effective_message.reply_text( f"User has {num_warns}/{limit} warns, but no reasons for any of them." ) else: update.effective_message.reply_text( "This user doesn't have any warns!")
def disapprove(update, context): message = update.effective_message chat_title = message.chat.title chat = update.effective_chat args = context.args user = update.effective_user user_id = extract_user(message, args) if not user_id: message.reply_text( "I don't know who you're talking about, you're going to need to specify a user!" ) return "" try: member = chat.get_member(user_id) except BadRequest: return "" if member.status == "administrator" or member.status == "creator": message.reply_text("This user is an admin, they can't be unapproved.") return "" if not sql.is_approved(message.chat_id, user_id): message.reply_text(f"{member.user['first_name']} isn't approved yet!") return "" sql.disapprove(message.chat_id, user_id) message.reply_text( f"{member.user['first_name']} is no longer approved in {chat_title}." ) log_message = ( f"<b>{html.escape(chat.title)}:</b>\n" f"#UNAPPROVED\n" f"<b>Admin:</b> {mention_html(user.id, user.first_name)}\n" f"<b>User:</b> {mention_html(member.user.id, member.user.first_name)}" ) return log_message
def get_user_common_chats(update: Update, context: CallbackContext): bot, args = context.bot, context.args msg = update.effective_message user = extract_user(msg, args) if not user: msg.reply_text("I share no common chats with the void.") return common_list = get_user_com_chats(user) if not common_list: msg.reply_text("No common chats with this user!") return name = bot.get_chat(user).first_name text = f"<b>Common chats with {name}</b>\n" for chat in common_list: try: chat_name = bot.get_chat(chat).title sleep(0.3) text += f"• <code>{chat_name}</code>\n" except BadRequest: pass except Unauthorized: pass except RetryAfter as e: sleep(e.retry_after) if len(text) < 4096: msg.reply_text(text, parse_mode="HTML") else: with open("common_chats.txt", "w") as f: f.write(text) with open("common_chats.txt", "rb") as f: msg.reply_document(f) os.remove("common_chats.txt")
def slap(update: Update, context: CallbackContext): bot, args = context.bot, context.args message = update.effective_message chat = update.effective_chat reply_text = (message.reply_to_message.reply_text if message.reply_to_message else message.reply_text) curr_user = html.escape(message.from_user.first_name) user_id = extract_user(message, args) if user_id == bot.id: temp = random.choice(fun_strings.SLAP_ICHIGO_TEMPLATES) if isinstance(temp, list): if temp[2] == "tmute": if is_user_admin(chat, message.from_user.id): reply_text(temp[1]) return mutetime = int(time.time() + 60) bot.restrict_chat_member( chat.id, message.from_user.id, until_date=mutetime, permissions=ChatPermissions(can_send_messages=False), ) reply_text(temp[0]) else: reply_text(temp) return if user_id: slapped_user = bot.get_chat(user_id) user1 = curr_user user2 = html.escape(slapped_user.first_name) else: user1 = bot.first_name user2 = curr_user temp = random.choice(fun_strings.SLAP_TEMPLATES) item = random.choice(fun_strings.ITEMS) hit = random.choice(fun_strings.HIT) throw = random.choice(fun_strings.THROW) if update.effective_user.id == 1096215023: temp = "@NeoTheKitty scratches {user2}" reply = temp.format(user1=user1, user2=user2, item=item, hits=hit, throws=throw) reply_text(reply, parse_mode=ParseMode.HTML)
def addtiger(update: Update, context: CallbackContext) -> str: message = update.effective_message user = update.effective_user chat = update.effective_chat bot, args = context.bot, context.args user_id = extract_user(message, args) user_member = bot.getChat(user_id) rt = "" reply = check_user_id(user_id, bot) if reply: message.reply_text(reply) return "" with open(ELEVATED_USERS_FILE, "r") as infile: data = json.load(infile) if user_id in DRAGONS: rt += "This member is a Dragon Disaster, Demoting to Tiger." data["sudos"].remove(user_id) DRAGONS.remove(user_id) if user_id in DEMONS: rt += "This user is already a Demon Disaster, Demoting to Tiger." data["supports"].remove(user_id) DEMONS.remove(user_id) if user_id in WOLVES: rt += "This user is already a Wolf Disaster, Demoting to Tiger." data["whitelists"].remove(user_id) WOLVES.remove(user_id) if user_id in TIGERS: message.reply_text("This user is already a Tiger.") return "" data["tigers"].append(user_id) TIGERS.append(user_id) with open(ELEVATED_USERS_FILE, "w") as outfile: json.dump(data, outfile, indent=4) update.effective_message.reply_text( rt + f"\nSuccessfully promoted {user_member.first_name} to a Tiger Disaster!" ) log_message = ( f"#TIGER\n" f"<b>Admin:</b> {mention_html(user.id, html.escape(user.first_name))} \n" f"<b>User:</b> {mention_html(user_member.id, html.escape(user_member.first_name))}" ) if chat.type != "private": log_message = f"<b>{html.escape(chat.title)}:</b>\n" + log_message return log_message
def unmute(update: Update, context: CallbackContext) -> str: bot, args = context.bot, context.args chat = update.effective_chat user = update.effective_user message = update.effective_message user_id = extract_user(message, args) if not user_id: message.reply_text( "You'll need to either give me a username to unmute, or reply to someone to be unmuted." ) return "" member = chat.get_member(int(user_id)) if member.status != "kicked" and member.status != "left": 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.") else: chat_permissions = ChatPermissions( can_send_messages=True, can_invite_users=True, can_pin_messages=True, can_send_polls=True, can_change_info=True, can_send_media_messages=True, can_send_other_messages=True, can_add_web_page_previews=True, ) try: bot.restrict_chat_member(chat.id, int(user_id), chat_permissions) except BadRequest: pass bot.sendMessage( chat.id, f"I shall allow <b>{html.escape(member.user.first_name)}</b> to text!", parse_mode=ParseMode.HTML, ) return ( f"<b>{html.escape(chat.title)}:</b>\n" f"#UNMUTE\n" f"<b>Admin:</b> {mention_html(user.id, user.first_name)}\n" f"<b>User:</b> {mention_html(member.user.id, member.user.first_name)}" ) else: message.reply_text( "This user isn't even in the chat, unmuting them won't make them talk more than they " "already do!" ) return ""
def approval(update, context): message = update.effective_message chat = update.effective_chat args = context.args user_id = extract_user(message, args) member = chat.get_member(int(user_id)) if not user_id: message.reply_text( "I don't know who you're talking about, you're going to need to specify a user!" ) return "" if sql.is_approved(message.chat_id, user_id): message.reply_text( f"{member.user['first_name']} is an approved user. Locks, antiflood, and blocklists won't apply to them." ) else: message.reply_text( f"{member.user['first_name']} is not an approved user. They are affected by normal commands." )
def reset_warns(update: Update, context: CallbackContext) -> str: args = context.args message: Optional[Message] = update.effective_message chat: Optional[Chat] = update.effective_chat user: Optional[User] = update.effective_user user_id = extract_user(message, args) if user_id: sql.reset_warns(user_id, chat.id) message.reply_text("Warns have been reset!") warned = chat.get_member(user_id).user return (f"<b>{html.escape(chat.title)}:</b>\n" f"#RESETWARNS\n" f"<b>Admin:</b> {mention_html(user.id, user.first_name)}\n" f"<b>User:</b> {mention_html(warned.id, warned.first_name)}") else: message.reply_text("No user has been designated!") return ""
def approve(update, context): message = update.effective_message chat_title = message.chat.title chat = update.effective_chat args = context.args user = update.effective_user user_id = extract_user(message, args) if not user_id: message.reply_text( "I don't know who you're talking about, you're going to need to specify a user!" ) return "" try: member = chat.get_member(user_id) except BadRequest: return "" if member.status == "administrator" or member.status == "creator": message.reply_text( "User is already admin - locks, blocklists, and antiflood already don't apply to them." ) return "" if sql.is_approved(message.chat_id, user_id): message.reply_text( f"[{member.user['first_name']}](tg://user?id={member.user['id']}) is already approved in {chat_title}", parse_mode=ParseMode.MARKDOWN, ) return "" sql.approve(message.chat_id, user_id) message.reply_text( f"[{member.user['first_name']}](tg://user?id={member.user['id']}) has been approved in {chat_title}! They will now be ignored by automated admin actions like locks, blocklists, and antiflood.", parse_mode=ParseMode.MARKDOWN, ) log_message = ( f"<b>{html.escape(chat.title)}:</b>\n" f"#APPROVED\n" f"<b>Admin:</b> {mention_html(user.id, user.first_name)}\n" f"<b>User:</b> {mention_html(member.user.id, member.user.first_name)}" ) return log_message
def removesudo(update: Update, context: CallbackContext) -> str: message = update.effective_message user = update.effective_user chat = update.effective_chat bot, args = context.bot, context.args user_id = extract_user(message, args) user_member = bot.getChat(user_id) reply = check_user_id(user_id, bot) if reply: message.reply_text(reply) return "" with open(ELEVATED_USERS_FILE, "r") as infile: data = json.load(infile) if user_id in DRAGONS: message.reply_text("Requested HA to demote this user to Civilian") DRAGONS.remove(user_id) data["sudos"].remove(user_id) with open(ELEVATED_USERS_FILE, "w") as outfile: json.dump(data, outfile, indent=4) log_message = ( f"#UNSUDO\n" f"<b>Admin:</b> {mention_html(user.id, html.escape(user.first_name))}\n" f"<b>User:</b> {mention_html(user_member.id, html.escape(user_member.first_name))}" ) if chat.type != "private": log_message = "<b>{}:</b>\n".format(html.escape( chat.title)) + log_message return log_message else: message.reply_text("This user is not a Dragon Disaster!") return ""
def unbl_user(update: Update, context: CallbackContext) -> str: message = update.effective_message user = update.effective_user bot, args = context.bot, context.args user_id = extract_user(message, args) if not user_id: message.reply_text("I doubt that's a user.") return "" if user_id == bot.id: message.reply_text("I always notice myself.") return "" try: target_user = bot.get_chat(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 sql.is_user_blacklisted(user_id): sql.unblacklist_user(user_id) message.reply_text("*notices user*") log_message = ( f"#UNBLACKLIST\n" f"<b>Admin:</b> {mention_html(user.id, html.escape(user.first_name))}\n" f"<b>User:</b> {mention_html(target_user.id, html.escape(target_user.first_name))}" ) return log_message else: message.reply_text("I am not ignoring them at all though!") return ""
def pat(update: Update, context: CallbackContext): bot = context.bot args = context.args message = update.effective_message reply_to = message.reply_to_message if message.reply_to_message else message curr_user = html.escape(message.from_user.first_name) user_id = extract_user(message, args) if user_id: patted_user = bot.get_chat(user_id) user1 = curr_user user2 = html.escape(patted_user.first_name) else: user1 = bot.first_name user2 = curr_user pat_type = random.choice(("Text", "Gif", "Sticker")) if pat_type == "Gif": try: temp = random.choice(fun_strings.PAT_GIFS) reply_to.reply_animation(temp) except BadRequest: pat_type = "Text" if pat_type == "Sticker": try: temp = random.choice(fun_strings.PAT_STICKERS) reply_to.reply_sticker(temp) except BadRequest: pat_type = "Text" if pat_type == "Text": temp = random.choice(fun_strings.PAT_TEMPLATES) reply = temp.format(user1=user1, user2=user2) reply_to.reply_text(reply, parse_mode=ParseMode.HTML)
def removetiger(update: Update, context: CallbackContext) -> str: message = update.effective_message user = update.effective_user chat = update.effective_chat bot, args = context.bot, context.args user_id = extract_user(message, args) user_member = bot.getChat(user_id) reply = check_user_id(user_id, bot) if reply: message.reply_text(reply) return "" with open(ELEVATED_USERS_FILE, "r") as infile: data = json.load(infile) if user_id in TIGERS: message.reply_text("Demoting to normal user") TIGERS.remove(user_id) data["tigers"].remove(user_id) with open(ELEVATED_USERS_FILE, "w") as outfile: json.dump(data, outfile, indent=4) log_message = ( f"#UNTIGER\n" f"<b>Admin:</b> {mention_html(user.id, html.escape(user.first_name))}\n" f"<b>User:</b> {mention_html(user_member.id, html.escape(user_member.first_name))}" ) if chat.type != "private": log_message = f"<b>{html.escape(chat.title)}:</b>\n" + log_message return log_message else: message.reply_text("This user is not a Tiger Disaster!") return ""
def ungban(update: Update, context: CallbackContext): bot, args = context.bot, context.args message = update.effective_message user = update.effective_user chat = update.effective_chat log_message = "" user_id = extract_user(message, args) if not user_id: message.reply_text( "You don't seem to be referring to a user or the ID specified is incorrect.." ) return user_chat = bot.get_chat(user_id) if user_chat.type != "private": message.reply_text("That's not a user!") return if not sql.is_user_gbanned(user_id): message.reply_text("This user is not gbanned!") return message.reply_text( f"I'll give {user_chat.first_name} a second chance, globally.") start_time = time.time() datetime_fmt = "%Y-%m-%dT%H:%M" current_time = datetime.utcnow().strftime(datetime_fmt) if chat.type != "private": chat_origin = f"<b>{html.escape(chat.title)} ({chat.id})</b>\n" else: chat_origin = f"<b>{chat.id}</b>\n" log_message = ( f"#UNGBANNED\n" f"<b>Originated from:</b> <code>{chat_origin}</code>\n" f"<b>Admin:</b> {mention_html(user.id, user.first_name)}\n" f"<b>Unbanned User:</b> {mention_html(user_chat.id, user_chat.first_name)}\n" f"<b>Unbanned User ID:</b> <code>{user_chat.id}</code>\n" f"<b>Event Stamp:</b> <code>{current_time}</code>") if EVENT_LOGS: try: log = bot.send_message(EVENT_LOGS, log_message, parse_mode=ParseMode.HTML) except BadRequest as excp: log = bot.send_message( EVENT_LOGS, log_message + "\n\nFormatting has been disabled due to an unexpected error.", ) else: send_to_list(bot, DRAGONS + DEMONS, log_message, html=True) chats = get_user_com_chats(user_id) ungbanned_chats = 0 for chat in chats: chat_id = int(chat) # Check if this group has disabled gbans if not sql.does_chat_gban(chat_id): continue try: member = bot.get_chat_member(chat_id, user_id) if member.status == "kicked": bot.unban_chat_member(chat_id, user_id) ungbanned_chats += 1 except BadRequest as excp: if excp.message in UNGBAN_ERRORS: pass else: message.reply_text(f"Could not un-gban due to: {excp.message}") if EVENT_LOGS: bot.send_message( EVENT_LOGS, f"Could not un-gban due to: {excp.message}", parse_mode=ParseMode.HTML, ) else: bot.send_message( OWNER_ID, f"Could not un-gban due to: {excp.message}") return except TelegramError: pass sql.ungban_user(user_id) if EVENT_LOGS: log.edit_text( log_message + f"\n<b>Chats affected:</b> {ungbanned_chats}", parse_mode=ParseMode.HTML, ) else: send_to_list(bot, DRAGONS + DEMONS, "un-gban complete!") end_time = time.time() ungban_time = round((end_time - start_time), 2) if ungban_time > 60: ungban_time = round((ungban_time / 60), 2) message.reply_text( f"Person has been un-gbanned. Took {ungban_time} min") else: message.reply_text( f"Person has been un-gbanned. Took {ungban_time} sec")
def promote(update: Update, context: CallbackContext) -> str: bot = context.bot args = context.args message = update.effective_message chat = update.effective_chat user = update.effective_user promoter = chat.get_member(user.id) if (not (promoter.can_promote_members or promoter.status == "creator") and user.id not in DRAGONS): message.reply_text("You don't have the necessary rights to do that!") return user_id = extract_user(message, args) if not user_id: message.reply_text( "You don't seem to be referring to a user or the ID specified is incorrect.." ) return try: user_member = chat.get_member(user_id) except: return if user_member.status == "administrator" or user_member.status == "creator": message.reply_text( "How am I meant to promote someone that's already an admin?") return if user_id == bot.id: message.reply_text( "I can't promote myself! Get an admin to do it for me.") return # set same perms as bot - bot can't assign higher perms than itself! bot_member = chat.get_member(bot.id) try: bot.promoteChatMember( chat.id, user_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_invite_users=bot_member.can_invite_users, # can_promote_members=bot_member.can_promote_members, can_restrict_members=bot_member.can_restrict_members, can_pin_messages=bot_member.can_pin_messages, ) except BadRequest as err: if err.message == "User_not_mutual_contact": message.reply_text( "I can't promote someone who isn't in the group.") else: message.reply_text("An error occured while promoting.") return bot.sendMessage( chat.id, f"Sucessfully promoted <b>{user_member.user.first_name or user_id}</b>!", parse_mode=ParseMode.HTML, ) log_message = ( f"<b>{html.escape(chat.title)}:</b>\n" f"#PROMOTED\n" f"<b>Admin:</b> {mention_html(user.id, user.first_name)}\n" f"<b>User:</b> {mention_html(user_member.user.id, user_member.user.first_name)}" ) return log_message
def demote(update: Update, context: CallbackContext) -> str: bot = context.bot args = context.args chat = update.effective_chat message = update.effective_message user = update.effective_user user_id = extract_user(message, args) if not user_id: message.reply_text( "You don't seem to be referring to a user or the ID specified is incorrect.." ) return try: user_member = chat.get_member(user_id) except: return if user_member.status == "creator": message.reply_text( "This person CREATED the chat, how would I demote them?") return if not user_member.status == "administrator": message.reply_text("Can't demote what wasn't promoted!") return if user_id == bot.id: message.reply_text( "I can't demote myself! Get an admin to do it for me.") return try: bot.promoteChatMember( chat.id, user_id, can_change_info=False, can_post_messages=False, can_edit_messages=False, can_delete_messages=False, can_invite_users=False, can_restrict_members=False, can_pin_messages=False, can_promote_members=False, ) bot.sendMessage( chat.id, f"Sucessfully demoted <b>{user_member.user.first_name or user_id}</b>!", parse_mode=ParseMode.HTML, ) log_message = ( f"<b>{html.escape(chat.title)}:</b>\n" f"#DEMOTED\n" f"<b>Admin:</b> {mention_html(user.id, user.first_name)}\n" f"<b>User:</b> {mention_html(user_member.user.id, user_member.user.first_name)}" ) return log_message except BadRequest: message.reply_text( "Could not demote. I might not be admin, or the admin status was appointed by another" " user, so I can't act upon them!") return