Example #1
0
def set_warn_strength(update, context):
    chat = update.effective_chat
    user = update.effective_user
    msg = update.effective_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 chat.type == "private":
            return
        else:
            chat_id = update.effective_chat.id
            chat_name = chat.title

    if args:
        if args[0].lower() in ("on", "yes"):
            sql.set_warn_strength(chat_id, False)
            msg.reply_text("Too many warns will now result in a ban!")
            return ("<b>{}:</b>\n"
                    "<b>Admin:</b> {}\n"
                    "Has enabled strong warns. Users will be banned.".format(
                        chat_name, mention_html(user.id, user.first_name)))

        elif args[0].lower() in ("off", "no"):
            sql.set_warn_strength(chat_id, True)
            msg.reply_text(
                "Too many warns will now result in a kick! Users will be able to join again after."
            )
            return (
                "<b>{}:</b>\n"
                "<b>Admin:</b> {}\n"
                "Has disabled strong warns. Users will only be kicked.".format(
                    chat_name, mention_html(user.id, user.first_name)))

        else:
            msg.reply_text("I only understand on/yes/no/off!")
    else:
        _, soft_warn = sql.get_warn_setting(chat_id)
        if soft_warn:
            msg.reply_text(
                "Warns are currently set to *kick* users when they exceed the limits.",
                parse_mode=ParseMode.MARKDOWN,
            )
        else:
            msg.reply_text(
                "Warns are currently set to *ban* users when they exceed the limits.",
                parse_mode=ParseMode.MARKDOWN,
            )
    return ""
Example #2
0
def warns(update, context):
    message = update.effective_message
    chat = update.effective_chat
    args = context.args
    user = update.effective_user
    user_id = extract_user(message, args) or update.effective_user.id

    conn = connected(context.bot, update, chat, user.id, need_admin=False)
    if conn:
        chat_id = conn
        chat_name = dispatcher.bot.getChat(conn).title
    else:
        if chat.type == "private":
            return
        else:
            chat_id = update.effective_chat.id
            chat_name = chat.title

    result = sql.get_warns(user_id, chat_id)

    num = 1
    if result and result[0] != 0:
        num_warns, reasons = result
        limit, _ = sql.get_warn_setting(chat_id)

        if reasons:
            if conn:
                text = "This user has {}/{} warnings, in *{}* for the following reasons:".format(
                    num_warns, limit, chat_name)
            else:
                text = (
                    "This user has {}/{} warnings, for the following reasons:".
                    format(
                        num_warns,
                        limit,
                    ))
            for reason in reasons:
                text += "\n {}. {}".format(num, reason)
                num += 1

            msgs = split_message(text)
            for msg in msgs:
                update.effective_message.reply_text(msg, parse_mode="markdown")
        else:
            update.effective_message.reply_text(
                "User has {}/{} warnings, but no reasons for any of them.".
                format(num_warns, limit),
                parse_mode="markdown",
            )
    else:
        update.effective_message.reply_text(
            "This user hasn't got any warnings!")
Example #3
0
def set_warn_limit(update, context) -> str:
    chat = update.effective_chat
    user = update.effective_user
    msg = update.effective_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 chat.type == "private":
            return
        else:
            chat_id = update.effective_chat.id
            chat_name = chat.title

    if args:
        if args[0].isdigit():
            if int(args[0]) < 3:
                msg.reply_text("The minimum warn limit is 3!")
            else:
                sql.set_warn_limit(chat_id, int(args[0]))
                msg.reply_text(
                    "Updated the warn limit to `{}` in *{}*".format(
                        escape_markdown(args[0]), chat_name),
                    parse_mode="markdown",
                )
                return ("<b>{}:</b>"
                        "\n#SET_WARN_LIMIT"
                        "\n<b>Admin:</b> {}"
                        "\nSet the warn limit to <code>{}</code>".format(
                            html.escape(chat_name),
                            mention_html(user.id, user.first_name),
                            args[0],
                        ))
        else:
            msg.reply_text("Give me a number as an arg!")
    else:
        limit, _ = sql.get_warn_setting(chat_id)

        msg.reply_text("The current warn in {} limit is {}".format(
            chat_name, limit))
    return ""
Example #4
0
def __chat_settings__(chat_id, user_id):
    num_warn_filters = sql.num_warn_chat_filters(chat_id)
    limit, soft_warn = sql.get_warn_setting(chat_id)
    return ("This chat has `{}` warn filters. It takes `{}` warns "
            "before the user gets *{}*.".format(
                num_warn_filters, limit, "kicked" if soft_warn else "banned"))
Example #5
0
def warn(user: User,
         chat: Chat,
         reason: str,
         message: Message,
         warner: User = None) -> str:
    if is_user_admin(chat, user.id):
        message.reply_text("Damn admins, can't even be warned!")
        return ""

    if warner:
        warner_tag = mention_html(warner.id, warner.first_name)
    else:
        warner_tag = "Automated warn filter."

    limit, soft_warn = sql.get_warn_setting(chat.id)
    num_warns, reasons = sql.warn_user(user.id, chat.id, reason)
    if num_warns >= limit:
        sql.reset_warns(user.id, chat.id)
        if soft_warn:  # kick
            chat.unban_member(user.id)
            reply = "That's {} warnings, {} has been kicked!".format(
                limit, mention_html(user.id, user.first_name))

        else:  # ban
            chat.kick_member(user.id)
            reply = "That's{} warnings, {} has been banned!".format(
                limit, mention_html(user.id, user.first_name))

        for warn_reason in reasons:
            reply += "\n - {}".format(html.escape(warn_reason))

        # message.bot.send_sticker(chat.id, BAN_STICKER)  # banhammer marie
        # sticker
        keyboard = None
        log_reason = ("<b>{}:</b>"
                      "\n#WARN_BAN"
                      "\n<b>Admin:</b> {}"
                      "\n<b>User:</b> {} (<code>{}</code>)"
                      "\n<b>Reason:</b> {}"
                      "\n<b>Counts:</b> <code>{}/{}</code>".format(
                          html.escape(chat.title),
                          warner_tag,
                          mention_html(user.id, user.first_name),
                          user.id,
                          reason,
                          num_warns,
                          limit,
                      ))

    else:
        keyboard = InlineKeyboardMarkup([[
            InlineKeyboardButton("Remove warn ⚠️",
                                 callback_data="rm_warn({})".format(user.id))
        ]])

        reply = "User {} has {}/{} warnings... watch out!".format(
            mention_html(user.id, user.first_name), num_warns, limit)
        if reason:
            reply += "\nReason for last warn:\n{}".format(html.escape(reason))

        log_reason = ("<b>{}:</b>"
                      "\n#WARN"
                      "\n<b>Admin:</b> {}"
                      "\n<b>User:</b> {} (<code>{}</code>)"
                      "\n<b>Reason:</b> {}"
                      "\n<b>Counts:</b> <code>{}/{}</code>".format(
                          html.escape(chat.title),
                          warner_tag,
                          mention_html(user.id, user.first_name),
                          user.id,
                          reason,
                          num_warns,
                          limit,
                      ))

    try:
        message.reply_text(reply,
                           reply_markup=keyboard,
                           parse_mode=ParseMode.HTML)
    except BadRequest as excp:
        if excp.message == "Reply message not found":
            # Do not reply
            message.reply_text(reply,
                               reply_markup=keyboard,
                               parse_mode=ParseMode.HTML,
                               quote=False)
        else:
            raise
    return log_reason