def del_lockables(bot: Bot, update: Update): chat = update.effective_chat message = update.effective_message for lockable, filter in LOCK_TYPES.items(): if filter(message) and sql.is_locked(chat.id, lockable) and can_delete( chat, 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, bot.id): message.reply_text( "I see a bot, and I've been told to stop them joining... " "but I'm not admin!") return chat.kick_member(new_mem.id) message.reply_text( "Only admins are allowed to add bots to this chat! Behave or I'll punch you." ) 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 antiarabic(bot: Bot, update: Update): chat = update.effective_chat msg = update.effective_message to_match = extract_text(msg) user = update.effective_user has_arabic = False if not sql.chat_antiarabic(chat.id): return "" if not user: # ignore channels return "" if user.id == 777000: # ignore telegram return "" if not to_match: return if chat.type != chat.PRIVATE: for c in to_match: if ('\u0600' <= c <= '\u06FF' or '\u0750' <= c <= '\u077F' or '\u08A0' <= c <= '\u08FF' or '\uFB50' <= c <= '\uFDFF' or '\uFE70' <= c <= '\uFEFF' or '\U00010E60' <= c <= '\U00010E7F' or '\U0001EE00' <= c <= '\U0001EEFF'): if can_delete(chat, bot.id): update.effective_message.delete() return ""
def dwarn_user(update: Update, context: CallbackContext) -> str: args = context.args message = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] warner = update.effective_user # type: Optional[User] user = update.effective_user user_id, reason = extract_user_and_text(message, args) time.sleep(2) message.delete() if can_delete(chat, context.bot.id): try: update.effective_message.reply_to_message.delete() except AttributeError: pass 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("That looks like an invalid User ID to me.") return ""
def lock(update: Update, context: CallbackContext) -> str: bot, args = context.bot, context.args chat = update.effective_chat user = update.effective_user message = update.effective_message if can_delete(chat, bot.id): if len(args) >= 1: if args[0] in LOCK_TYPES: sql.update_lock(chat.id, args[0], locked=True) message.reply_text( "Locked {} messages for all non-admins!".format(args[0])) return ( f"<b>{html.escape(chat.title)}:</b>\n" f"#LOCK\n" f"<b>Admin:</b> {mention_html(user.id, user.first_name)}\n" f"Locked <code>{args[0]}</code>.") elif args[0] in RESTRICTION_TYPES: sql.update_restriction(chat.id, args[0], locked=True) """ if args[0] == "messages": chat.set_permissions(can_send_messages=False) elif args[0] == "media": chat.set_permissions(can_send_media_messages=False) elif args[0] == "other": chat.set_permissions(can_send_other_messages=False) elif args[0] == "previews": chat.set_permissions(can_add_web_page_previews=False) elif args[0] == "all": chat.set_permissions(can_send_messages=False) """ message.reply_text("Locked {} for all non-admins!".format( args[0])) return ( f"<b>{html.escape(chat.title)}:</b>\n" f"#LOCK\n" f"<b>Admin:</b> {mention_html(user.id, user.first_name)}\n" f"Locked <code>{args[0]}</code>.") else: message.reply_text( "What are you trying to lock...? Try /locktypes for the list of lockables" ) else: message.reply_text( "I'm not an administrator, or haven't got delete rights.") return ""
def rest_handler(bot: Bot, update: Update): msg = update.effective_message chat = update.effective_chat for restriction, _filter in RESTRICTION_TYPES.items(): if _filter(msg) and sql.is_restr_locked( chat.id, restriction) and can_delete(chat, bot.id): try: msg.delete() except BadRequest as excp: if excp.message == "Message to delete not found": pass else: LOGGER.exception("ERROR in restrictions") break
def del_message(bot: Bot, update: Update) -> str: if update.effective_message.reply_to_message: user = update.effective_user chat = update.effective_chat if can_delete(chat, bot.id): update.effective_message.reply_to_message.delete() update.effective_message.delete() return (f"<b>{html.escape(chat.title)}:</b>\n" f"#DEL\n" f"<b>Admin:</b> {mention_html(user.id, user.first_name)}\n" f"Message deleted.") else: update.effective_message.reply_text("Whadya want to delete?") return ""
def rest_handler(update: Update, context: CallbackContext): bot = context.bot msg = update.effective_message chat = update.effective_chat #filter() expects Update object when filters are merged, otherwise Message object _chk = msg for restriction, _filter in RESTRICTION_TYPES.items(): if restriction != 'all': # all others are merged filters _chk = update if _filter(update) and sql.is_restr_locked( chat.id, restriction) and can_delete(chat, bot.id): try: msg.delete() except BadRequest as excp: if excp.message == "Message to delete not found": pass else: LOGGER.exception("ERROR in restrictions") break
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( "Good! Now If Now My Creator Gets Angry, He Will Be The One Who Will Ban You Let Me Get My Popcorns" ) elif user_id in DEV_USERS: message.reply_text( "I Can't Ban The Ones Who We're The One Who Gave Me Shelter And Food So,Nah I Am Not Gonna Do That." ) elif user_id in DRAGONS: message.reply_text("I Can't Ban My Precious Friends,You Fool.", ) elif user_id in DEMONS: message.reply_text("I Would Never Ban My Supporters.", ) elif user_id in TIGERS: message.reply_text( "Bring an order from Akatsuki Organization to fight This Uchiha.", ) elif user_id in WOLVES: message.reply_text( "Konoha Villager 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 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 ""
def del_lockables(update, context): chat = update.effective_chat # type: Optional[Chat] message = update.effective_message # type: Optional[Message] user = update.effective_user chat_id = str(chat.id)[1:] approve_list = list(REDIS.sunion(f'approve_list_{chat_id}')) target_user = mention_html(user.id, user.first_name) if target_user in approve_list: return 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 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, "Qrupa bot gəldiyini gördüm amma admin olmadığıma görə onu qrupdan ata bilmədim!", ) return chat.kick_member(new_mem.id) send_message( update.effective_message, "Yalnız adminlər bu qrupa bot əlavə edə bilər! Rədd ol.", ) 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 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 = "{} qrupunda {} kilidləndi!".format( chat_name, ltype) else: if update.effective_message.chat.type == "private": send_message( update.effective_message, "Bu əmri qrupda işlədin", ) return "" chat = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title text = "{} kilidləndi!".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 = "{} qrupunda {} kilidləndi!".format( chat_name, ltype) else: if update.effective_message.chat.type == "private": send_message( update.effective_message, "Bu əmri qrupda işlədin.", ) return "" chat = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title text = "{} kilidləndi!".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, "Nəyi kilidləmək istəyirsənki...? /locktypes yazaraq mövcud kilidlərə baxa bilərsən.", ) else: send_message(update.effective_message, "Nəyi kilidləmək istəyirsənki...?") else: send_message( update.effective_message, "Admin deyiləm və ya lazımi səlahiyyətlərim yoxdur.", ) return ""
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( "Nyahhh!!!!... why do you want to ban me noob huh? ") 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( "His power level is TrySweet Infinite, can't defeat him!!") elif user_id in DEV_USERS: message.reply_text("I can't act against Falcy.") elif user_id in DRAGONS: message.reply_text( "Power Level ultimate, tried but i got drenched!! Nyah!! .") elif user_id in DEMONS: message.reply_text( "If i kill that user TrySweet sama will be angry nyahhh!!.") elif user_id in TIGERS: message.reply_text("TrySweet sama is watching i can't do that!! .") elif user_id in WOLVES: message.reply_text( "That is foreverness, TrySweet sama gave him powers.") else: message.reply_text( "This user has a shield that i can't destroy... ") 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 Carnival</b>\n" f"<code> </code><b>• The prey:</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 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.ban_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) 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 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 purge(bot: Bot, update: Update, args: List[str]) -> str: msg = update.effective_message user = update.effective_user chat = update.effective_chat if can_delete(chat, bot.id): if msg.reply_to_message: message_id = msg.reply_to_message.message_id start_message_id = message_id - 1 delete_to = msg.message_id - 1 if args and args[0].isdigit(): new_del = message_id + int(args[0]) # No point deleting messages which haven't been written yet. if new_del < delete_to: delete_to = new_del else: if args and args[0].isdigit(): messages_to_delete = int(args[0]) if messages_to_delete < 1: msg.reply_text("1 iletiden daha azını temizleyemiyorum.") return "" delete_to = msg.message_id - 1 start_message_id = delete_to - messages_to_delete for m_id in range(delete_to, start_message_id, -1): # Reverse iteration over message ids try: bot.deleteMessage(chat.id, m_id) except BadRequest as err: if err.message == "Mesaj silinemez": bot.send_message( chat.id, "Tüm mesajlar silinemiyor. Mesajlar çok eski olabilir, " "silme haklarına sahip değilsiniz veya bu bir üst grup olmayabilir." ) elif err.message != "Silinecek mesaj bulunamadı": LOGGER.exception( "Sohbet mesajlarını temizlerken hata oluştu.") try: msg.delete() except BadRequest as err: if err.message == "Mesaj silinemez": bot.send_message( chat.id, "Tüm mesajlar silinemiyor. Mesajlar çok eski olabilir, " "silme haklarına sahip değilsiniz veya bu bir super grup olmayabilir." ) elif err.message != "Silinecek mesaj bulunamadı": LOGGER.exception("Sohbet mesajlarını temizlerken hata oluştu.") bot.send_message( chat.id, f"Temizle <code>{delete_to - start_message_id}</code> mesajları.", parse_mode=ParseMode.HTML) return ( f"<b>{html.escape(chat.title)}:</b>\n" f"#PURGE\n" f"<b>Admin:</b> {mention_html(user.id, user.first_name)}\n" f"Temizlendi <code>{delete_to - start_message_id}</code> mesajlar." ) return ""
def purge(bot: Bot, update: Update, args: List[str]) -> str: msg = update.effective_message user = update.effective_user chat = update.effective_chat if can_delete(chat, bot.id): if msg.reply_to_message: message_id = msg.reply_to_message.message_id start_message_id = message_id - 1 delete_to = msg.message_id - 1 if args and args[0].isdigit(): new_del = message_id + int(args[0]) # No point deleting messages which haven't been written yet. if new_del < delete_to: delete_to = new_del else: if args and args[0].isdigit(): messages_to_delete = int(args[0]) if messages_to_delete < 1: msg.reply_text("Can't purge less than 1 message.") return "" delete_to = msg.message_id - 1 start_message_id = delete_to - messages_to_delete for m_id in range(delete_to, start_message_id, -1): # Reverse iteration over message ids try: bot.deleteMessage(chat.id, m_id) except BadRequest as err: if err.message == "Message can't be deleted": bot.send_message( chat.id, "Cannot delete all messages. The messages may be too old, I might " "not have delete rights, or this might not be a supergroup." ) elif err.message != "Message to delete not found": LOGGER.exception("Error while purging chat messages.") try: msg.delete() except BadRequest as err: if err.message == "Message can't be deleted": bot.send_message( chat.id, "Cannot delete all messages. The messages may be too old, I might " "not have delete rights, or this might not be a supergroup." ) elif err.message != "Message to delete not found": LOGGER.exception("Error while purging chat messages.") bot.send_message( chat.id, f"Purge <code>{delete_to - start_message_id}</code> messages.", parse_mode=ParseMode.HTML) return ( f"<b>{html.escape(chat.title)}:</b>\n" f"#PURGE\n" f"<b>Admin:</b> {mention_html(user.id, user.first_name)}\n" f"Purged <code>{delete_to - start_message_id}</code> messages.") return ""
def punch(update: Update, context: CallbackContext) -> str: chat = update.effective_chat user = update.effective_user message = update.effective_message log_message = "" bot, args = context.bot, 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("I can't seem to find this user.") return log_message if user_id == bot.id: message.reply_text("Yeahhh I'm not gonna do that.") return log_message if is_user_ban_protected(chat, user_id): message.reply_text("I really wish I could punch this user....") return log_message res = chat.unban_member(user_id) # unban on current user = kick if res: if message.text.startswith("/s"): silent = True if not can_delete(chat, context.bot.id): return "" else: silent = False if silent: if message.reply_to_message: message.reply_to_message.delete() message.delete() else: bot.sendMessage( chat.id, f"Kicked {mention_html(member.user.id, html.escape(member.user.first_name))} out of the group!", parse_mode=ParseMode.HTML, ) log = ( f"<b>{html.escape(chat.title)}:</b>\n" f"#KICKED\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 += f"\n<b>Reason:</b> {reason}" return log else: message.reply_text("Well damn, I can't punch that user.") return log_message