Example #1
0
def check_flood(bot: Bot, update: Update) -> 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 ""

    if is_approved(chat.id, user.id):
        sql.update_flood(chat.id, None)
        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 ""

    soft_flood = sql.get_flood_strength(chat.id)
    if soft_flood:  # kick
        chat.unban_member(user.id)
        reply = "Sad, I don't like your flooding. Get out! {} has been kicked!".format(
            mention_html(user.id, user.first_name))

    else:  # ban
        chat.kick_member(user.id)
        reply = "Frankly, I like to leave the flooding to natural disasters. {} has been banned!".format(
            mention_html(user.id, user.first_name))
    try:
        keyboard = []
        msg.reply_text(reply, reply_markup=keyboard, parse_mode=ParseMode.HTML)
        msg.delete()
        return "<b>{}:</b>" \
               "\n#FLOOD_CTL" \
               "\n<b>• User:</b> {}" \
               "\nFlooded the group.".format(html.escape(chat.title),
                                             mention_html(user.id, user.first_name))

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

    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("Anti-flood has been disabled.")

        elif val.isdigit():
            amount = int(val)
            if amount <= 0:
                sql.set_flood(chat.id, 0)
                message.reply_text("Anti-flood has been disabled.")
                return "<b>{}:</b>" \
                       "\n#SETFLOOD" \
                       "\n<b>• Admin:</b> {}" \
                       "\nDisabled Anti-flood.".format(html.escape(chat.title), mention_html(user.id, user.first_name))

            elif amount < 1:
                message.reply_text(
                    "Anti-flood has to be either 0 (disabled) or least 1")
                return ""

            else:
                sql.set_flood(chat.id, amount)
                message.reply_text(
                    "Anti-flood has been updated and set to {}".format(amount))
                return "<b>{}:</b>" \
                       "\n#SETFLOOD" \
                       "\n<b>• Admin:</b> {}" \
                       "\nSet anti-flood to <code>{}</code>.".format(html.escape(chat.title),
                                                                    mention_html(user.id, user.first_name), amount)

        else:
            message.reply_text(
                "Unrecognised argument - please use a number, 'off', or 'no'.")
    else:
        message.reply_text("Give me an argument! Set a number to enforce against consecutive spams.\n" \
                           "i.e `/setflood 5`: to control consecutive of messages.", parse_mode=ParseMode.MARKDOWN)
    return ""