Exemplo n.º 1
0
def unrestrict_group(bot: Bot, update: Update, args: List[str]) -> str:
    message = update.effective_message  # type: Optional[Message]

    # Check if there is only one argument
    if not len(args) == 1:
        message.reply_text(
            "Incorrect number of arguments. Please use `/unrestrict chat_id`.",
            parse_mode=ParseMode.MARKDOWN)
        return

    chat_id = args[0]

    # Check if chat_id is valid
    if not chat_id.startswith('-') or chat_id.isdigit():
        message.reply_text(
            "Invalid chat id! "
            "Ensure that the chat id is an integer and you included the '-' sign in the chat id."
        )
        return

    # Check if chat_id is in bot database
    chat_title = sql.get_chatname_by_chatid(chat_id)
    if chat_title is None:
        message.reply_text(
            "I can't seem to find the chat in my database. "
            "Use /chatlist to obtain a list of chats in my database.")
        return

    chat_restricted = sql.get_restriction(chat_id)
    if chat_restricted:
        chat_title = html.escape(chat_title)
        sql.set_restriction(chat_id, chat_title, restricted=False)

        message.reply_text(
            "Successfully removed all restrictions on the chat <b>{}</b>!".
            format(chat_title),
            parse_mode=ParseMode.HTML)

        # Report to sudo users
        unrestrictor = update.effective_user  # type: Optional[User]
        send_to_list(
            bot,
            SUDO_USERS,
            "{} has removed my restrictions on the chat <b>{}</b>.".format(
                mention_html(unrestrictor.id, unrestrictor.first_name),
                chat_title),
            html=True)

    else:
        message.reply_text("I'm not restricted from that chat!")
Exemplo n.º 2
0
def chats(bot: Bot, update: Update):
    all_chats = sql.get_all_chats() or []
    chatfile = 'List of chats.\n'
    for chat in all_chats:
        chat_restricted = sql.get_restriction(chat.chat_id)

        if chat_restricted:
            chatfile += "{} - ({}) [R]\n".format(chat.chat_name, chat.chat_id)
        else:
            chatfile += "{} - ({})\n".format(chat.chat_name, chat.chat_id)

    if any(chat.restricted == True for chat in all_chats):
        chatfile += "[R] - Restricted chat"

    with BytesIO(str.encode(chatfile)) as output:
        output.name = "chatlist.txt"
        update.effective_message.reply_document(
            document=output,
            filename="chatlist.txt",
            caption="Here is the list of chats in my database.")
Exemplo n.º 3
0
def restrict_group(bot: Bot, update: Update, args: List[str]) -> str:
    message = update.effective_message  # type: Optional[Message]

    # Check if there is only one argument
    if not len(args) == 1:
        message.reply_text(
            "Incorrect number of arguments. Please use `/restrict chat_id`.",
            parse_mode=ParseMode.MARKDOWN)
        return

    chat_id = args[0]

    # Check if chat_id is valid
    if not chat_id.startswith('-') or chat_id.isdigit():
        message.reply_text(
            "Invalid chat id! "
            "Ensure that the chat id is an integer and you included the '-' sign in the chat id."
        )
        return

    # Check if chat_id is in bot database
    chat_title = sql.get_chatname_by_chatid(chat_id)
    if chat_title is None:
        message.reply_text(
            "I can't seem to find the chat in my database. "
            "Use /chatlist to obtain a list of chats in my database.")
        return

    chat_restricted = sql.get_restriction(chat_id)
    if not chat_restricted:
        chat_title = html.escape(chat_title)

        sudo_users_list = "<b>My Admins:</b>"
        for user in SUDO_USERS:
            name = mention_html(user, bot.get_chat(user).first_name)
            sudo_users_list += "\n - {}".format(name)

        try:
            bot.send_message(
                chat_id=chat_id,
                text="I have been restricted by my admins from this chat. "
                "Request any of my admins to add me to this chat.\n\n"
                "{}".format(sudo_users_list),
                parse_mode=ParseMode.HTML)
        except Unauthorized as excp:
            if excp.message == "Forbidden: bot is not a member of the supergroup chat":
                message.reply_text(
                    "Looks like I'm no longer a part of that chat!")
                return
            else:
                LOGGER.exception("Error while sending message to chat.")

        bot.leave_chat(chat_id)

        sql.set_restriction(chat_id, chat_title, restricted=True)

        message.reply_text(
            "Successfully left chat <b>{}</b>!".format(chat_title),
            parse_mode=ParseMode.HTML)

        # Report to sudo users
        restrictor = update.effective_user  # type: Optional[User]
        send_to_list(
            bot,
            SUDO_USERS,
            "{} has restricted me from being added to the chat <b>{}</b>.".
            format(mention_html(restrictor.id, restrictor.first_name),
                   chat_title),
            html=True)

    else:
        message.reply_text("I'm already restricted from that chat!")
Exemplo n.º 4
0
 def filter(self, message: Message):
     return get_restriction(message.chat.id)