Ejemplo n.º 1
0
def check_flood(bot: Bot, update: Update) -> str:
    user = update.effective_user
    chat = update.effective_chat
    msg = update.effective_message
    log_message = ""

    if not user:  # ignore channels
        return log_message

    # ignore admins and whitelists
    if (is_user_admin(chat, user.id) or user.id in WHITELIST_USERS
            or user.id in TIGER_USERS):
        sql.update_flood(chat.id, None)
        return log_message

    should_mute = sql.update_flood(chat.id, user.id)
    if not should_mute:
        return ""

    try:
        bot.restrict_chat_member(chat.id, user.id, can_send_messages=False)

        keyboard = InlineKeyboardMarkup([[
            InlineKeyboardButton("Unmute",
                                 callback_data="unmute_flooder({})".format(
                                     user.id))
        ]])
        bot.send_message(
            chat.id,
            f"{mention_html(user.id, user.first_name)} has been muted for flooding the group!",
            reply_markup=keyboard,
            parse_mode="HTML")


        return "<b>{}:</b>" \
               "\n#MUTED" \
               "\n<b>User:</b> {}" \
               "\nFlooded the group.\nMuted until an admin unmutes".format(html.escape(chat.title),
                                             mention_html(user.id, user.first_name))

    except BadRequest:
        msg.reply_text(
            "I can't mute people here, give me permissions first! Until then, I'll disable antiflood."
        )
        sql.set_flood(chat.id, 0)
        log_message = (
            "<b>{chat.title}:</b>\n"
            "#INFO\n"
            "Don't have kick permissions, so automatically disabled antiflood."
        )

        return log_message
Ejemplo n.º 2
0
def check_flood(update, context) -> str:
    user = update.effective_user  # type: Optional[User]
    chat = update.effective_chat  # type: Optional[Chat]
    msg = update.effective_message  # type: Optional[Message]
    if not user:  # ignore channels
        return ""

    # ignore admins and whitelists
    if is_user_admin(chat, user.id) or user.id in WOLVES or user.id in TIGERS:
        sql.update_flood(chat.id, None)
        return ""
    # ignore approved users
    if is_approved(chat.id, user.id):
        sql.update_flood(chat.id, None)
        return
    should_ban = sql.update_flood(chat.id, user.id)
    if not should_ban:
        return ""

    try:
        getmode, getvalue = sql.get_flood_setting(chat.id)
        if getmode == 1:
            chat.ban_member(user.id)
            execstrings = "Banned"
            tag = "BANNED"
        elif getmode == 2:
            chat.ban_member(user.id)
            chat.unban_member(user.id)
            execstrings = "Kicked"
            tag = "KICKED"
        elif getmode == 3:
            context.bot.restrict_chat_member(
                chat.id,
                user.id,
                permissions=ChatPermissions(can_send_messages=False),
            )
            execstrings = "Muted"
            tag = "MUTED"
        elif getmode == 4:
            bantime = extract_time(msg, getvalue)
            chat.ban_member(user.id, until_date=bantime)
            execstrings = "Banned for {}".format(getvalue)
            tag = "TBAN"
        elif getmode == 5:
            mutetime = extract_time(msg, getvalue)
            context.bot.restrict_chat_member(
                chat.id,
                user.id,
                until_date=mutetime,
                permissions=ChatPermissions(can_send_messages=False),
            )
            execstrings = "Muted for {}".format(getvalue)
            tag = "TMUTE"
        send_message(
            update.effective_message,
            "Beep Boop! Boop Beep!\n{}!".format(execstrings),
        )

        return ("<b>{}:</b>"
                "\n#{}"
                "\n<b>User:</b> {}"
                "\nFlooded the group.".format(
                    tag,
                    html.escape(chat.title),
                    mention_html(user.id, html.escape(user.first_name)),
                ))

    except BadRequest:
        msg.reply_text(
            "I can't restrict people here, give me permissions first! Until then, I'll disable anti-flood.",
        )
        sql.set_flood(chat.id, 0)
        return (
            "<b>{}:</b>"
            "\n#INFO"
            "\nDon't have enough permission to restrict users so automatically disabled anti-flood"
            .format(chat.title, ))
Ejemplo n.º 3
0
def set_flood(update, context) -> str:
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    message = update.effective_message  # type: Optional[Message]
    args = context.args

    conn = connected(context.bot, update, chat, user.id, need_admin=True)
    if conn:
        chat_id = conn
        chat_name = dispatcher.bot.getChat(conn).title
    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_id = update.effective_chat.id
        chat_name = update.effective_message.chat.title

    if len(args) >= 1:
        val = args[0].lower()
        if val in ["off", "no", "0"]:
            sql.set_flood(chat_id, 0)
            if conn:
                text = message.reply_text(
                    "Antiflood has been disabled in {}.".format(chat_name), )
            else:
                text = message.reply_text("Antiflood has been disabled.")

        elif val.isdigit():
            amount = int(val)
            if amount <= 0:
                sql.set_flood(chat_id, 0)
                if conn:
                    text = message.reply_text(
                        "Antiflood has been disabled in {}.".format(chat_name),
                    )
                else:
                    text = message.reply_text("Antiflood has been disabled.")
                return ("<b>{}:</b>"
                        "\n#SETFLOOD"
                        "\n<b>Admin:</b> {}"
                        "\nDisable antiflood.".format(
                            html.escape(chat_name),
                            mention_html(user.id,
                                         html.escape(user.first_name)),
                        ))

            if amount <= 3:
                send_message(
                    update.effective_message,
                    "Antiflood must be either 0 (disabled) or number greater than 3!",
                )
                return ""
            sql.set_flood(chat_id, amount)
            if conn:
                text = message.reply_text(
                    "Anti-flood has been set to {} in chat: {}".format(
                        amount,
                        chat_name,
                    ), )
            else:
                text = message.reply_text(
                    "Successfully updated anti-flood limit to {}!".format(
                        amount), )
            return ("<b>{}:</b>"
                    "\n#SETFLOOD"
                    "\n<b>Admin:</b> {}"
                    "\nSet antiflood to <code>{}</code>.".format(
                        html.escape(chat_name),
                        mention_html(user.id, html.escape(user.first_name)),
                        amount,
                    ))

        else:
            message.reply_text(
                "Invalid argument please use a number, 'off' or 'no'")
    else:
        message.reply_text(
            ("Use `/setflood number` to enable anti-flood.\nOr use `/setflood off` to disable antiflood!."
             ),
            parse_mode="markdown",
        )
    return ""
Ejemplo n.º 4
0
def check_flood(update, context) -> str:
    user = update.effective_user  # type: Optional[User]
    chat = update.effective_chat  # type: Optional[Chat]
    msg = update.effective_message  # type: Optional[Message]

    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

    if not user:  # ignore channels
        return ""

    # ignore admins
    if is_user_admin(chat, user.id):
        sql.update_flood(chat.id, None)
        return ""

    should_ban = sql.update_flood(chat.id, user.id)
    if not should_ban:
        return ""

    try:
        getmode, getvalue = sql.get_flood_setting(chat.id)
        if getmode == 1:
            chat.kick_member(user.id)
            execstrings = "Banned"
            tag = "BANNED"
        elif getmode == 2:
            chat.kick_member(user.id)
            chat.unban_member(user.id)
            execstrings = "Kicked"
            tag = "KICKED"
        elif getmode == 3:
            context.bot.restrict_chat_member(
                chat.id,
                user.id,
                permissions=ChatPermissions(can_send_messages=False))
            execstrings = "Muted"
            tag = "MUTED"
        elif getmode == 4:
            bantime = extract_time(msg, getvalue)
            chat.kick_member(user.id, until_date=bantime)
            execstrings = "Banned for {}".format(getvalue)
            tag = "TBAN"
        elif getmode == 5:
            mutetime = extract_time(msg, getvalue)
            context.bot.restrict_chat_member(
                chat.id,
                user.id,
                until_date=mutetime,
                permissions=ChatPermissions(can_send_messages=False),
            )
            execstrings = "Muted for {}".format(getvalue)
            tag = "TMUTE"
        send_message(
            update.effective_message,
            "Great, I like to leave flooding to staff members but you, "
            "you were just a disappointment. {}!".format(execstrings),
        )

        return ("<b>{}:</b>"
                "\n#{}"
                "\n<b>User:</b> {}"
                "\nFlooded the group.".format(
                    html.escape(chat.title), tag,
                    mention_html(user.id, user.first_name)))

    except BadRequest:
        msg.reply_text(
            "I can't restrict people here, give me permissions first! Until then, I'll disable anti-flood."
        )
        sql.set_flood(chat.id, 0)
        return (
            "<b>{}:</b>"
            "\n#INFO"
            "\nDon't have enough permission to restrict users so automatically disabled anti-flood"
            .format(chat.title))
Ejemplo n.º 5
0
def check_flood(update, context) -> str:
    user = update.effective_user  # type: Optional[User]
    chat = update.effective_chat  # type: Optional[Chat]
    msg = update.effective_message  # type: Optional[Message]
    if not user:  # ignore channels
        return ""

    # ignore admins and whitelists
    if (is_user_admin(chat, user.id) or user.id in WHITELIST_USERS
            or user.id in TIGER_USERS):
        sql.update_flood(chat.id, None)
        return ""

    should_ban = sql.update_flood(chat.id, user.id)
    if not should_ban:
        return ""

    try:
        getmode, getvalue = sql.get_flood_setting(chat.id)
        if getmode == 1:
            chat.kick_member(user.id)
            execstrings = ("Banned")
            tag = "BANNED"
        elif getmode == 2:
            chat.kick_member(user.id)
            chat.unban_member(user.id)
            execstrings = ("Kicked")
            tag = "KICKED"
        elif getmode == 3:
            context.bot.restrict_chat_member(
                chat.id,
                user.id,
                permissions=ChatPermissions(can_send_messages=False))
            execstrings = ("Muted")
            tag = "MUTED"
        elif getmode == 4:
            bantime = extract_time(msg, getvalue)
            chat.kick_member(user.id, until_date=bantime)
            execstrings = ("Banned for {}".format(getvalue))
            tag = "TBAN"
        elif getmode == 5:
            mutetime = extract_time(msg, getvalue)
            context.bot.restrict_chat_member(
                chat.id,
                user.id,
                until_date=mutetime,
                permissions=ChatPermissions(can_send_messages=False))
            execstrings = ("Muted for {}".format(getvalue))
            tag = "TMUTE"
        send_message(
            update.effective_message,
            "Wonderful, I like to leave flooding to natural disasters but you, "
            "you were just a disappointment {}!".format(execstrings))

        return "<b>{}:</b>" \
               "\n#{}" \
               "\n<b>User:</b> {}" \
               "\nFlooded the group.".format(tag, html.escape(chat.title),
                                             mention_html(user.id, user.first_name))

    except BadRequest:
        msg.reply_text(
            "I can't restrict people here, give me permissions first! Until then, I'll disable anti-flood."
        )
        sql.set_flood(chat.id, 0)
        return "<b>{}:</b>" \
               "\n#INFO" \
               "\nDon't have enough permission to restrict users so automatically disabled anti-flood".format(chat.title)
Ejemplo n.º 6
0
def set_flood(bot: Bot, update: Update, args: List[str]) -> str:
    chat = update.effective_chat
    user = update.effective_user
    message = update.effective_message
    log_message = ""

    update_chat_title = chat.title
    message_chat_title = update.effective_message.chat.title

    if update_chat_title == message_chat_title:
        chat_name = ""
    else:
        chat_name = f" in <b>{update_chat_title}</b>"

    if len(args) >= 1:

        val = args[0].lower()

        if val == "off" or val == "no" or val == "0":
            sql.set_flood(chat.id, 0)
            message.reply_text(
                "Antiflood has been disabled{}.".format(chat_name),
                parse_mode=ParseMode.HTML)

        elif val.isdigit():
            amount = int(val)
            if amount <= 0:
                sql.set_flood(chat.id, 0)
                message.reply_text(
                    "Antiflood has been disabled{}.".format(chat_name),
                    parse_mode=ParseMode.HTML)
                log_message = (
                    f"<b>{html.escape(chat.title)}:</b>\n"
                    f"#SETFLOOD\n"
                    f"<b>Admin</b>: {mention_html(user.id, user.first_name)}\n"
                    f"Disabled antiflood.")

                return log_message
            elif amount < 3:
                message.reply_text(
                    "Antiflood has to be either 0 (disabled), or a number bigger than 3!"
                )
                return log_message

            else:
                sql.set_flood(chat.id, amount)
                message.reply_text(
                    "Antiflood has been updated and set to {}{}".format(
                        amount, chat_name),
                    parse_mode=ParseMode.HTML)
                log_message = (
                    f"<b>{html.escape(chat.title)}:</b>\n"
                    f"#SETFLOOD\n"
                    f"<b>Admin</b>: {mention_html(user.id, user.first_name)}\n"
                    f"Set antiflood to <code>{amount}</code>.")

                return log_message
        else:
            message.reply_text(
                "Unrecognised argument - please use a number, 'off', or 'no'.")

    return log_message
Ejemplo n.º 7
0
def check_flood(update, context) -> str:
    user = update.effective_user  # type: Optional[User]
    chat = update.effective_chat  # type: Optional[Chat]
    msg = update.effective_message  # type: Optional[Message]
    if not user:  # ignore channels
        return ""

    # ignore admins and whitelists
    if is_user_admin(chat, user.id) or user.id in WOLVES or user.id in TIGERS:
        sql.update_flood(chat.id, None)
        return ""
    # ignore approved users
    if is_approved(chat.id, user.id):
        sql.update_flood(chat.id, None)
        return
    should_ban = sql.update_flood(chat.id, user.id)
    if not should_ban:
        return ""

    try:
        getmode, getvalue = sql.get_flood_setting(chat.id)
        if getmode == 1:
            chat.kick_member(user.id)
            execstrings = "Banned"
            tag = "BANNED"
        elif getmode == 2:
            chat.kick_member(user.id)
            chat.unban_member(user.id)
            execstrings = "Kicked"
            tag = "KICKED"
        elif getmode == 3:
            context.bot.restrict_chat_member(
                chat.id,
                user.id,
                permissions=ChatPermissions(can_send_messages=False),
            )
            execstrings = "Muted"
            tag = "MUTED"
        elif getmode == 4:
            bantime = extract_time(msg, getvalue)
            chat.kick_member(user.id, until_date=bantime)
            execstrings = "Banned for {}".format(getvalue)
            tag = "TBAN"
        elif getmode == 5:
            mutetime = extract_time(msg, getvalue)
            context.bot.restrict_chat_member(
                chat.id,
                user.id,
                until_date=mutetime,
                permissions=ChatPermissions(can_send_messages=False),
            )
            execstrings = "Muted for {}".format(getvalue)
            tag = "TMUTE"
        send_message(
            update.effective_message,
            "Beep Boop! Boop Beep!\n{}!".format(execstrings),
        )

        return ("<b>{}:</b>"
                "\n#{}"
                "\n<b>User:</b> {}"
                "\nFlooded the group.".format(
                    tag,
                    html.escape(chat.title),
                    mention_html(user.id, html.escape(user.first_name)),
                ))

    except BadRequest:
        msg.reply_text(
            "Mình không thể hạn chế mọi người ở đây, hãy cho mình quyền trước! Cho đến lúc đó, mình sẽ tắt tính năng anti-flood",
        )
        sql.set_flood(chat.id, 0)
        return (
            "<b>{}:</b>"
            "\n#INFO"
            "\nKhông có đủ quyền hạn chế người dùng nên tính năng anti-flood sẽ tự động tắt"
            .format(chat.title, ))
Ejemplo n.º 8
0
def set_flood(update, context) -> str:
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    message = update.effective_message  # type: Optional[Message]
    args = context.args

    conn = connected(context.bot, update, chat, user.id, need_admin=True)
    if conn:
        chat_id = conn
        chat_name = dispatcher.bot.getChat(conn).title
    else:
        if update.effective_message.chat.type == "private":
            send_message(
                update.effective_message,
                "Lệnh này được sử dụng trong nhóm không phải trong PM",
            )
            return ""
        chat_id = update.effective_chat.id
        chat_name = update.effective_message.chat.title

    if len(args) >= 1:
        val = args[0].lower()
        if val in ["off", "no", "0"]:
            sql.set_flood(chat_id, 0)
            if conn:
                text = message.reply_text(
                    "Antiflood đã bị vô hiệu hóa trong {}.".format(chat_name),
                )
            else:
                text = message.reply_text("Antiflood đã bị vô hiệu hóa.")

        elif val.isdigit():
            amount = int(val)
            if amount <= 0:
                sql.set_flood(chat_id, 0)
                if conn:
                    text = message.reply_text(
                        "Antiflood đã bị vô hiệu hóa trong {}.".format(
                            chat_name), )
                else:
                    text = message.reply_text("Antiflood đã bị vô hiệu hóa.")
                return ("<b>{}:</b>"
                        "\n#SETFLOOD"
                        "\n<b>Admin:</b> {}"
                        "\nĐã vô hiệu hóa antiflood.".format(
                            html.escape(chat_name),
                            mention_html(user.id,
                                         html.escape(user.first_name)),
                        ))

            elif amount <= 3:
                send_message(
                    update.effective_message,
                    "Antiflood phải là 0 (bị vô hiệu hóa) hoặc số lớn hơn 3!",
                )
                return ""

            else:
                sql.set_flood(chat_id, amount)
                if conn:
                    text = message.reply_text(
                        "Antiflood đã được đặt thành {} trong trò chuyện: {}".
                        format(
                            amount,
                            chat_name,
                        ), )
                else:
                    text = message.reply_text(
                        "Đã cập nhật thành công giới hạn Antiflood thành {}!".
                        format(amount), )
                return ("<b>{}:</b>"
                        "\n#SETFLOOD"
                        "\n<b>Admin:</b> {}"
                        "\nĐặt antiflood thành <code>{}</code>.".format(
                            html.escape(chat_name),
                            mention_html(user.id,
                                         html.escape(user.first_name)),
                            amount,
                        ))

        else:
            message.reply_text(
                "Đối số không hợp lệ, vui lòng sử dụng một số, 'off' or 'no'")
    else:
        message.reply_text(
            ("Sử dụng `/setflood number` để kích hoạt anti-flood.\nHoặc sử dụng `/setflood off` để vô hiệu hóa antiflood!."
             ),
            parse_mode="markdown",
        )
    return ""
Ejemplo n.º 9
0
def check_flood(update, context) -> str:
    user = update.effective_user  # type: Optional[User]
    chat = update.effective_chat  # type: Optional[Chat]
    msg = update.effective_message  # type: Optional[Message]
    if not user:  # ignore channels
        return ""

    # ignore admins and whitelists
    if (is_user_admin(chat, user.id) or user.id in WOLVES or user.id in TIGERS):
        sql.update_flood(chat.id, None)
        return ""

    should_ban = sql.update_flood(chat.id, user.id)
    if not should_ban:
        return ""

    try:
        getmode, getvalue = sql.get_flood_setting(chat.id)
        if getmode == 1:
            chat.kick_member(user.id)
            execstrings = ("Banlandı")
            tag = "BANNED"
        elif getmode == 2:
            chat.kick_member(user.id)
            chat.unban_member(user.id)
            execstrings = ("Atıldı")
            tag = "KICKED"
        elif getmode == 3:
            context.bot.restrict_chat_member(
                chat.id,
                user.id,
                permissions=ChatPermissions(can_send_messages=False))
            execstrings = ("Susduruldu")
            tag = "MUTED"
        elif getmode == 4:
            bantime = extract_time(msg, getvalue)
            chat.kick_member(user.id, until_date=bantime)
            execstrings = ("{} müddətlik susduruldu".format(getvalue))
            tag = "TBAN"
        elif getmode == 5:
            mutetime = extract_time(msg, getvalue)
            context.bot.restrict_chat_member(
                chat.id,
                user.id,
                until_date=mutetime,
                permissions=ChatPermissions(can_send_messages=False))
            execstrings = ("{} müddətlik susduruldu".format(getvalue))
            tag = "TMUTE"
        send_message(update.effective_message,
                     "Beep Boop! Boop Beep!\n{}!".format(execstrings))

        return "<b>{}:</b>" \
               "\n#{}" \
               "\n<b>User:</b> {}" \
               "\nFlooded the group.".format(tag, html.escape(chat.title),
                                             mention_html(user.id, html.escape(user.first_name)))

    except BadRequest:
        msg.reply_text(
            "Mən burada insanları məhdudlaşdıra bilmirəm, mənə lazımi səlahiyyətləri ver!"
        )
        sql.set_flood(chat.id, 0)
        return "<b>{}:</b>" \
               "\n#INFO" \
               "\nDon't have enough permission to restrict users so automatically disabled anti-flood".format(chat.title)
Ejemplo n.º 10
0
def set_flood(update, context) -> str:
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    message = update.effective_message  # type: Optional[Message]
    args = context.args

    conn = connected(context.bot, update, chat, user.id, need_admin=True)
    if conn:
        chat_id = conn
        chat_name = dispatcher.bot.getChat(conn).title
    else:
        if update.effective_message.chat.type == "private":
            send_message(update.effective_message,
                         "Bu əmr qrupda işlədilə bilər PM-də yox")
            return ""
        chat_id = update.effective_chat.id
        chat_name = update.effective_message.chat.title

    if len(args) >= 1:
        val = args[0].lower()
        if val == "off" or val == "no" or val == "0":
            sql.set_flood(chat_id, 0)
            if conn:
                text = message.reply_text(
                    "{} qrupunda antiflood aktiv edildi.".format(chat_name))
            else:
                text = message.reply_text("Antiflood deaktiv edildi.")

        elif val.isdigit():
            amount = int(val)
            if amount <= 0:
                sql.set_flood(chat_id, 0)
                if conn:
                    text = message.reply_text(
                        "{} qrupunda antiflood deaktiv edildi.".format(chat_name))
                else:
                    text = message.reply_text("Antiflood deaktiv edildi.")
                return "<b>{}:</b>" \
                       "\n#SETFLOOD" \
                       "\n<b>Admin:</b> {}" \
                       "\nDisable antiflood.".format(html.escape(chat_name), mention_html(user.id, html.escape(user.first_name)))

            elif amount <= 3:
                send_message(
                    update.effective_message,
                    "Antiflood ya 0 (qeyri aktiv) ya da 3-dən böyük olmalıdır!"
                )
                return ""

            else:
                sql.set_flood(chat_id, amount)
                if conn:
                    text = message.reply_text(
                        "{} qrupunda antiflood ayarı {} edildi".format(
                            chat_name, amount))
                else:
                    text = message.reply_text(
                        "Antiflood ayarı {} edildi!".format(
                            amount))
                return "<b>{}:</b>" \
                       "\n#SETFLOOD" \
                       "\n<b>Admin:</b> {}" \
                       "\nSet antiflood to <code>{}</code>.".format(html.escape(chat_name),
                                                                    mention_html(user.id, html.escape(user.first_name)), amount)

        else:
            message.reply_text(
                "Yanlış arqument verildi. Yalnız ədədlər və ya 'off' 'no' istifadə edin")
    else:
        message.reply_text((
            "Antiflood aktiv etmək üçün `/setflood ədəd` istifadə edin.\nDeaktiv etmək üçün `/setflood off` istifadə edin!."
        ),
                           parse_mode="markdown")
    return ""