Пример #1
0
def check_flood(update: Update, context: CallbackContext) -> 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 SARDEGNA_USERS):
        sql.update_flood(chat.id, None)
        return log_message

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

    try:
        context.bot.restrict_chat_member(chat.id,
                                         user.id,
                                         can_send_messages=False)
        context.bot.send_message(
            chat.id,
            f"*mutes {mention_html(user.id, user.first_name)} permanently*\nStop flooding the group!",
            parse_mode=ParseMode.HTML,
        )
        log_message = (
            f"<b>{html.escape(chat.title)}:</b>\n"
            f"#MUTED\n"
            f"<b>User:</b> {mention_html(user.id, user.first_name)}\n"
            f"Flooded the group.\nMuted until an admin unmutes")

        return log_message

    except BadRequest:
        msg.reply_text(
            "I can't kick 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
Пример #2
0
def set_flood(update: Update, context: CallbackContext) -> str:
    chat = update.effective_chat
    user = update.effective_user
    message = update.effective_message
    args = context.args
    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 in ('off', 'no', '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.")

            elif amount < 3:
                message.reply_text(
                    "Antiflood has to be either 0 (disabled), or a number bigger than 3!"
                )
            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