def warns(update, context): message = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] args = context.args user_id = extract_user(message, args) or update.effective_user.id result = sql.get_warns(user_id, chat.id) num = 1 if result and result[0] != 0: num_warns, reasons = result limit, soft_warn = sql.get_warn_setting(chat.id) if reasons: text = "This user has {}/{} warnings, for the following reasons:".format( num_warns, limit) for reason in reasons: text += "\n {}. {}".format(num, reason) num += 1 msgs = split_message(text) for msg in msgs: update.effective_message.reply_text(msg) else: update.effective_message.reply_text( "User has {}/{} warnings, but no reasons for any of them.". format(num_warns, limit)) else: update.effective_message.reply_text( "This user hasn't got any warnings!")
def gbam(update, context): user = update.effective_user chat = update.effective_chat bot, args = context.bot, context.args message = update.effective_message curr_user = html.escape(message.from_user.first_name) user_id = extract_user(message, args) if user_id: gbam_user = bot.get_chat(user_id) user1 = curr_user user2 = html.escape(gbam_user.first_name) else: user1 = curr_user user2 = bot.first_name if update.effective_message.chat.type == "private": return if int(user.id) in SUDO_USERS or int(user.id) in SUPPORT_USERS: gbamm = fun.GBAM reason = random.choice(fun.GBAM_REASON) gbam = gbamm.format(user1=user1, user2=user2, chatid=chat.id, reason=reason) context.bot.sendMessage(chat.id, gbam, parse_mode=ParseMode.HTML)
def demote(update, context): chat = update.effective_chat message = update.effective_message user = update.effective_user args = context.args if user_can_promote(chat, user, context.bot.id) is False: message.reply_text("You don't have enough rights to demote someone!") return "" user_id = extract_user(message, args) if not user_id: message.reply_text("You don't seem to be referring to a user.") return "" user_member = chat.get_member(user_id) if user_member.status == "creator": message.reply_text("I'm not gonna demote Creator this group....") return "" if not user_member.status == "administrator": message.reply_text( "How I'm supposed to demote someone who is not even an admin!") return "" if user_id == context.bot.id: message.reply_text("Yeahhh... Not gonna demote myself!") return "" try: context.bot.promoteChatMember( int(chat.id), int(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, ) message.reply_text("Successfully demoted!") return ("<b>{}:</b>" "\n#DEMOTED" "\n<b>Admin:</b> {}" "\n<b>User:</b> {}".format( html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(user_member.user.id, user_member.user.first_name), )) except BadRequest: message.reply_text( "Failed to demote. I might not be admin, or the admin status was appointed by another " "user, so I can't act upon them!") return ""
def unmute(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 unmute people") return "" user_id = extract_user(message, args) if not user_id: message.reply_text("You don't seem to be referring to a user.") 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: context.bot.restrict_chat_member( chat.id, int(user_id), 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, ), ) message.reply_text( f"Yep! *{member.user.first_name}* (`{member.user.id}`) can start talking again!", parse_mode=ParseMode.MARKDOWN) return ("<b>{}:</b>" "\n#UNMUTE" "\n<b>Admin:</b> {}" "\n<b>User:</b> {}".format( html.escape(chat.title), mention_html(user.id, user.first_name), 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 promote(update, context): chat_id = update.effective_chat.id message = update.effective_message chat = update.effective_chat user = update.effective_user args = context.args if user_can_promote(chat, user, context.bot.id) is False: message.reply_text("You don't have enough rights to promote someone!") return "" user_id = extract_user(message, args) if not user_id: message.reply_text("You don't seem to be referring to a user.") return "" user_member = chat.get_member(user_id) if user_member.status == "administrator" or user_member.status == "creator": message.reply_text("This person is already an admin...!") return "" if user_id == context.bot.id: message.reply_text("I hope, if i could promote myself!") return "" # set same perms as bot - bot can't assign higher perms than itself! bot_member = chat.get_member(context.bot.id) context.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_restrict_members=bot_member.can_restrict_members, can_pin_messages=bot_member.can_pin_messages, ) message.reply_text("Promoted") return ("<b>{}:</b>" "\n#PROMOTED" "\n<b>Admin:</b> {}" "\n<b>User:</b> {}".format( html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(user_member.user.id, user_member.user.first_name), ))
def slap(update, context): args = context.args msg = update.effective_message # reply to correct message reply_text = (msg.reply_to_message.reply_text if msg.reply_to_message else msg.reply_text) # get user who sent message if msg.from_user.username: curr_user = "******" + escape_markdown(msg.from_user.username) else: curr_user = "******".format(msg.from_user.first_name, msg.from_user.id) user_id = extract_user(update.effective_message, args) if user_id: slapped_user = context.bot.get_chat(user_id) user1 = curr_user if slapped_user.username: user2 = "@" + escape_markdown(slapped_user.username) else: user2 = "[{}](tg://user?id={})".format(slapped_user.first_name, slapped_user.id) # if no target found, bot targets the sender else: user1 = "[{}](tg://user?id={})".format(context.bot.first_name, context.bot.id) user2 = curr_user temp = random.choice(fun.SLAP_TEMPLATES) item = random.choice(fun.ITEMS) hit = random.choice(fun.HIT) throw = random.choice(fun.THROW) repl = temp.format(user1=user1, user2=user2, item=item, hits=hit, throws=throw) reply_text(repl, parse_mode=ParseMode.MARKDOWN)
def hug(update, context): args = context.args msg = update.effective_message # type: Optional[Message] # reply to correct message reply_text = ( msg.reply_to_message.reply_text if msg.reply_to_message else msg.reply_text ) # get user who sent message if msg.from_user.username: curr_user = "******" + escape_markdown(msg.from_user.username) else: curr_user = "******".format( msg.from_user.first_name, msg.from_user.id ) user_id = extract_user(update.effective_message, args) if user_id: hugged_user = context.bot.get_chat(user_id) user1 = curr_user if hugged_user.username: user2 = "@" + escape_markdown(hugged_user.username) else: user2 = "[{}](tg://user?id={})".format( hugged_user.first_name, hugged_user.id ) # if no target found, bot targets the sender else: user1 = "Awwh! [{}](tg://user?id={})".format( context.bot.first_name, context.bot.id ) user2 = curr_user temp = random.choice(fun.HUG_TEMPLATES) hug = random.choice(fun.HUG) repl = temp.format(user1=user1, user2=user2, hug=hug) reply_text(repl, parse_mode=ParseMode.MARKDOWN)
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.PAT_GIFS) reply_to.reply_animation(temp) except BadRequest: pat_type = "Text" if pat_type == "Sticker": try: temp = random.choice(fun.PAT_STICKERS) reply_to.reply_sticker(temp) except BadRequest: pat_type = "Text" if pat_type == "Text": temp = random.choice(fun.PAT_TEMPLATES) reply = temp.format(user1=user1, user2=user2) reply_to.reply_text(reply, parse_mode=ParseMode.HTML)
def reset_warns(update, context): message = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] args = context.args user_id = extract_user(message, args) if user_id: sql.reset_warns(user_id, chat.id) message.reply_text("Warnings have been reset!") warned = chat.get_member(user_id).user return ("<b>{}:</b>" "\n#RESETWARNS" "\n<b>Admin:</b> {}" "\n<b>User:</b> {} (<code>{}</code>)".format( html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(warned.id, warned.first_name), warned.id, )) else: message.reply_text("No user has been designated!") return ""
def about_bio(update, context): message = update.effective_message # type: Optional[Message] args = context.args user_id = extract_user(message, args) if user_id: user = context.bot.get_chat(user_id) else: user = message.from_user info = sql.get_user_bio(user.id) if info: update.effective_message.reply_text( "*{}*:\n{}".format(user.first_name, escape_markdown(info)), parse_mode=ParseMode.MARKDOWN, ) elif message.reply_to_message: username = user.first_name update.effective_message.reply_text( "{} No details about him have been saved yet !".format(username)) else: update.effective_message.reply_text( " Your bio about you has been saved !")
def about_me(update, context): message = update.effective_message # type: Optional[Message] args = context.args user_id = extract_user(message, args) if user_id: user = context.bot.get_chat(user_id) else: user = message.from_user info = sql.get_user_me_info(user.id) if info: update.effective_message.reply_text( "*{}*:\n{}".format(user.first_name, escape_markdown(info)), parse_mode=ParseMode.MARKDOWN, ) elif message.reply_to_message: username = message.reply_to_message.from_user.first_name update.effective_message.reply_text( username + "Information about him is currently unavailable !") else: update.effective_message.reply_text( "You have not added any information about yourself yet !")
def book(update: Update, context: CallbackContext): bot, args = context.bot, context.args message = update.effective_message chat = update.effective_chat user_id = extract_user(update.effective_message, args) if user_id: user = bot.get_chat(user_id) elif not message.reply_to_message and not args: user = message.from_user elif not message.reply_to_message and ( not args or (len(args) >= 1 and not args[0].startswith("@") and not args[0].isdigit() and not message.parse_entities([MessageEntity.TEXT_MENTION]))): message.reply_text("I Can't Extract A User From This.") return elif user_id == bot.id: return else: return text = (f"<b>User Status :</b>" f"<code> {user.id} </code>") if chat.type != "private" and user_id != bot.id: _stext = "\n\n<b>• Current Chat Status : </b>{}" status = status = bot.get_chat_member(chat.id, user.id).status if status: if status in "kicked": text += _stext.format("Banned!") elif status == "restricted": text += _stext.format("Muted!") text += "\n\n<b>• Other Status</b>" text += "\n<b>CAS Banned : </b>" result = cas.banchecker(user.id) text += str(result) try: spamwtc = spamwtc.get_ban(int(user.id)) if spamwtc: text += "\n<b>Spamwatch Banned :</b> True" text += f"\n<b>Reason :</b> <pre>{spamwtc.reason}</pre>" else: text += "\n<b>Spamwatch Banned :</b> False" except: pass # don't crash if api is down somehow... text += f"\n\n<b>• {dispatcher.bot.first_name} Status</b>" for mod in USER_BOOK: if mod.__mod_name__ == "Users": continue try: mod_book = mod.__user_book__(user.id) except TypeError: mod_book = mod.__user_book__(user.id, chat.id) if mod_book: text += "\n" + mod_book message.reply_text(text, parse_mode=ParseMode.HTML, disable_web_page_preview=True)
def gkick(update, context): message = update.effective_message args = context.args bot = context.bot user_id = extract_user(message, args) try: user_chat = context.bot.get_chat(user_id) except BadRequest as excp: if excp.message in GKICK_ERRORS: pass else: message.reply_text("Unexpected Error!") context.bot.send_message( ERROR_DUMP, "User cannot be Globally kicked because: {}".format( excp.message)) return except TelegramError: pass 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 OFFICERS: message.reply_text("Error! (^_-)") return if user_id == context.bot.id: message.reply_text("Error! (¯―¯٥)") return chats = get_all_chats() banner = update.effective_user # type: Optional[User] message.reply_text("Globally kicking user @{}".format(user_chat.username)) sql.gkick_user(user_id, user_chat.username, 1) for chat in chats: try: member = context.bot.get_chat_member(chat.chat_id, user_id) if member.can_send_messages is False: context.bot.unban_chat_member( chat.chat_id, user_id) # Unban_member = kick (and not ban) context.bot.restrict_chat_member( chat.chat_id, user_id, permissions=ChatPermissions(can_send_messages=False)) else: context.bot.unban_chat_member(chat.chat_id, user_id) except BadRequest as excp: if excp.message in GKICK_ERRORS: pass else: message.reply_text("Unexpected Error!") context.bot.send_message( ERROR_DUMP, "User cannot be Globally kicked because: {}".format( excp.message)) return except TelegramError: pass
def ungmute(update, context): message = update.effective_message # type: Optional[Message] bot = context.bot args = context.args user_id = extract_user(message, args) if not user_id: message.reply_text("You don't seem to be referring to a user.") 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_gmuted(user_id): message.reply_text("This user is not gmuted!") return muter = update.effective_user # type: Optional[User] message.reply_text("I'll let {} speak again, globally.".format( user_chat.first_name)) chats = get_all_chats() for chat in chats: chat_id = chat.chat_id # Check if this group has disabled gmutes if not sql.does_chat_gmute(chat_id): continue try: member = context.bot.get_chat_member(chat_id, user_id) if member.status == 'restricted': context.bot.restrict_chat_member( chat_id, int(user_id), 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, )) except BadRequest as excp: if excp.message == "User is an administrator of the chat": pass elif excp.message == "Chat not found": pass elif excp.message == "Not enough rights to restrict/unrestrict chat member": pass elif excp.message == "User_not_participant": pass elif excp.message == "Method is available for supergroup and channel chats only": pass elif excp.message == "Not in the chat": pass elif excp.message == "Channel_private": pass elif excp.message == "Chat_admin_required": pass else: message.reply_text("Unexpected Error!") bot.send_message( ERROR_DUMP, "Could not un-gmute due to: {}".format(excp.message)) return except TelegramError: pass sql.ungmute_user(user_id) message.reply_text("Person has been un-gmuted.")
def 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 = extract_user(message, args) if not user_id: message.reply_text("You don't seem to be referring to a user.") return "" if user_id == context.bot.id: message.reply_text("Yeahh... I'm not muting myself!") return "" if user_id == 777000 or user_id == 1087968824: message.reply_text( str(user_id) + " is an account reserved for telegram, I cannot mute it!") return "" member = chat.get_member(int(user_id)) if member: if is_user_admin(chat, user_id, member=member): message.reply_text( "Well I'm not gonna stop an admin from talking!") elif member.can_send_messages is None or member.can_send_messages: context.bot.restrict_chat_member( chat.id, user_id, permissions=ChatPermissions(can_send_messages=False)) reply_msg = "*{}* (`{}`) has been muted in *{}*.".format( member.user.first_name, member.user.id, chat.title) message.reply_text( reply_msg, reply_markup=InlineKeyboardMarkup( [[ InlineKeyboardButton( text="Unmute", callback_data="muteb_mute={}".format(user_id)), InlineKeyboardButton( text="Delete", callback_data="del_{}=delete={}={}".format( chat.id, user_id, message.reply_to_message.message_id)) ], [ InlineKeyboardButton(text="Close", callback_data="mute_close") ]]), parse_mode=ParseMode.MARKDOWN) return ("<b>{}:</b>" "\n#MUTE" "\n<b>Admin:</b> {}" "\n<b>User:</b> {}".format( html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), )) else: message.reply_text("This user is already muted.") else: message.reply_text("This user isn't in the chat!") return ""
def info(update: Update, context: CallbackContext): bot, args = context.bot, context.args message = update.effective_message chat = update.effective_chat user_id = extract_user(update.effective_message, args) if user_id: user = bot.get_chat(user_id) elif not message.reply_to_message and not args: user = message.from_user elif not message.reply_to_message and ( not args or (len(args) >= 1 and not args[0].startswith("@") and not args[0].isdigit() and not message.parse_entities([MessageEntity.TEXT_MENTION]))): message.reply_text("I can't extract a user from this.") return else: return text = (f"<b>• User Information :-</b>\n\n" f"∘ ID: <code>{user.id}</code>\n" f"∘ First Name: {html.escape(user.first_name)}") if user.last_name: text += f"\n∘ Last Name: {html.escape(user.last_name)}" if user.username: text += f"\n∘ Username: @{html.escape(user.username)}" isafk = is_user_afk(user.id) try: text += "\n\n∘ Currently AFK: " if user.id == bot.id: text += "<code>???</code>" else: text += str(isafk) except: pass try: if user.id == bot.id: num_chats = "???" else: num_chats = get_user_num_chats(user.id) text += f"\n∘ Mutual Chats: <code>{num_chats}</code> " except BadRequest: pass try: status = status = bot.get_chat_member(chat.id, user.id).status if status: if status in "left": text += "\n∘ Chat Status: <em>Not Here!</em>" elif status == "member": text += "\n∘ Chat Status: <em>Is Here!</em>" elif status in "administrator": text += "\n∘ Chat Status: <em>Admin!</em>" elif status in "creator": text += "\n∘ Chat Status: <em>Creator!</em>" except BadRequest: pass try: user_member = chat.get_member(user.id) if user_member.status == 'administrator': result = requests.post( f"https://api.telegram.org/bot{TOKEN}/getChatMember?chat_id={chat.id}&user_id={user.id}" ) result = result.json()["result"] if "custom_title" in result.keys(): custom_title = result['custom_title'] text += f"\n∘ Admin Title: <code>{custom_title}</code> \n" except BadRequest: pass if user_id not in [bot.id, 777000, 1087968824]: userhp = hpmanager(user) text += f"\n∘ Health : <code>{userhp['earnedhp']}/{userhp['totalhp']}</code> ∙ <code>{userhp['percentage']}% </code> \n( {make_bar(int(userhp['percentage']))} ) \n " if user.id == OWNER_ID: text += "\n<b>This Person Is My Master!</b>" elif user.id in DEV_USERS: text += "\n∘ <b>DEV USER: </b><i>Yes!</i> " elif user.id in SUDO_USERS: text += "\n∘ <b>SUDO USER: </b><i>Yes!</i> " elif user.id in SUPPORT_USERS: text += "\n∘ <b>SUPPORT USER: </b><i>Yes!</i> " elif user.id in WHITELIST_USERS: text += "\n∘ <b>WHITELIST USER: </b><i>Yes!</i> " elif user.id == bot.id: text += "\n\nI've Seen Them In... Wow. Are They Stalking Me? They're In All The Same Places I Am... Oh. It's Me.\n" for mod in USER_INFO: if mod.__mod_name__ == "Users": continue try: mod_info = mod.__user_info__(user.id) except TypeError: mod_info = mod.__user_info__(user.id, chat.id) if mod_info: text += "\n" + mod_info message.reply_text(text, parse_mode=ParseMode.HTML)