Exemple #1
0
def delete_join(bot: Bot, update: Update):
    chat = update.effective_chat  # type: Optional[Chat]
    join = update.effective_message.new_chat_members
    if can_delete(chat, bot.id):
        del_join = sql.get_del_pref(chat.id)
        if del_join and update.message:
            update.message.delete()
Exemple #2
0
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!."
                        )
            else:
                try:
                    message.delete()
                except BadRequest as excp:
                    if excp.message == "Message to delete not found":
                        pass
                    else:
                        LOGGER.exception("ERROR in lockables")

            break
Exemple #3
0
def lock(bot: Bot, update: Update, args: List[str]) -> str:

    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 "<b>{}:</b>" \
                       "\n#LOCK" \
                       "\n<b>Admin:</b> {}" \
                       "\nLocked <code>{}</code>.".format(html.escape(chat.title),
                                                          mention_html(user.id, user.first_name), args[0])

            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 "<b>{}:</b>" \
                       "\n#LOCK" \
                       "\n<b>Admin:</b> {}" \
                       "\nLocked <code>{}</code>.".format(html.escape(chat.title),
                                                          mention_html(user.id, user.first_name), args[0])

            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 ""
Exemple #4
0
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 ""
Exemple #7
0
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")
				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")
			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")
			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.")
			else:
				try:
					message.delete()
				except BadRequest as excp:
					if excp.message == "Message to delete not found":
						pass
					else:
						LOGGER.exception("ERROR in lockables")
 
				break
Exemple #8
0
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 all {} messages 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 all {} messages 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 ""