def del_lockables(update: Update, context: CallbackContext): bot = context.bot 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': LOGGER.exception('ERROR in lockables') break
def lock(update: Update, context: CallbackContext) -> str: chat = update.effective_chat message = update.effective_message bot = context.bot if can_delete(chat, bot.id): args = context.args if len(args) >= 1: user = update.effective_user 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(update: Update, context: CallbackContext): 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, context.bot.id): try: msg.delete() except BadRequest as excp: if excp.message != 'Message to delete not found': LOGGER.exception('ERROR in restrictions') break
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 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 ""