Exemple #1
0
def set_warn_limit(update: Update, context: CallbackContext) -> str:
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    msg = update.effective_message  # type: Optional[Message]
    args = msg.text.split(" ")

    if args:
        if args[1].isdigit():
            if int(args[1]) < 3:
                msg.reply_text("The minimum warn limit is 3!")
            else:
                sql.set_warn_limit(chat.id, int(args[1]))
                msg.reply_text("Updated the warn limit to {}".format(args[1]))
                return "<b>{}:</b>" \
                       "\n#SET_WARN_LIMIT" \
                       "\n<b>• Admin:</b> {}" \
                       "\nSet the warn limit to <code>{}</code>".format(html.escape(chat.title),
                                                                        mention_html(user.id, user.first_name), args[1])
        else:
            msg.reply_text("Give me a number as an arg!")
    else:
        limit, soft_warn, warn_mode = sql.get_warn_setting(chat.id)

        msg.reply_text("The current warn limit is {}".format(limit))
    return ""
Exemple #2
0
def warns(update: Update, context: CallbackContext):
    message = update.effective_message  # type: Optional[Message]
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    user_id = extract_user(message,
                           message.text.split(" ")) or update.effective_user.id
    member = chat.get_member(user_id)
    result = sql.get_warns(user_id, chat.id)
    num = 1

    if result and result[0] != 0:
        num_warns, reasons = result
        limit, soft_warn, warn_mode = sql.get_warn_setting(chat.id)

        if reasons:
            text = "User {} has {}/{} warnings, for the following reasons:".format(
                mention_html(member.user.id, member.user.first_name),
                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=ParseMode.HTML)

    else:
        update.effective_message.reply_text(
            "User {} hasn't got any warnings!".format(
                mention_html(member.user.id, member.user.first_name)),
            parse_mode=ParseMode.HTML)
Exemple #3
0
def set_warn_strength(update: Update, context: CallbackContext):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    msg = update.effective_message  # type: Optional[Message]
    args = msg.text.split(" ")

    if args:
        if args[1].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(html.escape(chat.title),
                                                                            mention_html(user.id, user.first_name))

        elif args[1].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(html.escape(chat.title),
                                                                                  mention_html(user.id,
                                                                                               user.first_name))

        else:
            msg.reply_text("I only understand on/yes/no/off!")
    else:
        limit, soft_warn, warn_mode = 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 ""
Exemple #4
0
def __chat_settings__(chat_id, user_id):
    num_warn_filters = sql.num_warn_chat_filters(chat_id)
    limit, soft_warn, warn_mode = 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")
Exemple #5
0
def set_warn_mode(update: Update, context: CallbackContext):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    msg = update.effective_message  # type: Optional[Message]
    args = msg.text.split(" ")

    if len(args) > 1:
        if args[1].lower() in ("ban"):
            sql.set_warn_mode(chat.id, 1)
            msg.reply_text("Updated warning mode to: ban")

            return "<b>{}:</b>\n" \
                   "<b>• Admin:</b> {}\n" \
                   "Has updated warn mode to ban.".format(html.escape(chat.title),
                                                                            mention_html(user.id, user.first_name))

        elif args[1].lower() in ("kick"):
            sql.set_warn_mode(chat.id, 2)
            msg.reply_text("Updated warning mode to: kick")
            return "<b>{}:</b>\n" \
                   "<b>• Admin:</b> {}\n" \
                   "Has updated warn mode to kick.".format(html.escape(chat.title),
                                                                                  mention_html(user.id,
                                                                                               user.first_name))

        elif args[1].lower() in ("mute"):
            sql.set_warn_mode(chat.id, 3)
            msg.reply_text("Updated warning mode to: mute")
            return "<b>{}:</b>\n" \
                   "<b>• Admin:</b> {}\n" \
                   "Has updated warn mode to mute.".format(html.escape(chat.title), mention_html(user.id,
                                                                                               user.first_name))

        elif args[1].lower() in ("tmute"):
            try:
                val = args[2].lower()
                sql.set_warn_mode(chat.id, 4)
                sql.set_warn_time(chat.id, str(val))
                msg.reply_text(
                    "Updated warning mode to: tmute for {}".format(val))

                return "<b>{}:</b>\n" \
                       "<b>• Admin:</b> {}\n" \
                       "Has updated warn mode to tmute for {}.".format(html.escape(chat.title), mention_html(user.id,
                                                                                                   user.first_name), val)
            except:
                msg.reply_text("You haven't specified a time!")
                return ""

        elif args[1].lower() == "tban":
            try:
                val = args[2].lower()
                sql.set_warn_mode(chat.id, 5)
                sql.set_warn_time(chat.id, str(val))
                msg.reply_text(
                    "Updated warning mode to: tban for {}".format(val))

                return "<b>{}:</b>\n" \
                       "<b>• Admin:</b> {}\n" \
                       "Has updated warn mode to tban for {}.".format(html.escape(chat.title), mention_html(user.id,
                                                                                               user.first_name), val)

            except:
                msg.reply_text("You haven't specified a time!")
                return ""

        else:
            msg.reply_text(
                "Unknown Type command. I only understand ban/kick/mute/tmute/tban!"
            )

    else:
        limit, soft_warn, warn_mode = sql.get_warn_setting(chat.id)
        warn_time = sql.get_warn_time(chat.id)
        if not soft_warn:
            if not warn_mode:
                text = "You need to specify an action to take upon too many warns." \
                       "Current modes are: ban/kick/mute/tmute/tban"

            elif warn_mode == 1:
                text = "You need to specify an action to take upon too many warns.\n" \
                       "Current mode: `ban`"

            elif warn_mode == 2:
                text = "You need to specify an action to take upon too many warns.\n" \
                       "Current mode: `kick`"

            elif warn_mode == 3:
                text = "You need to specify an action to take upon too many warns.\n" \
                       "Current mode: `mute`"

            elif warn_mode == 4:
                text = "You need to specify an action to take upon too many warns.\n" \
                       "Current mode: `tmute for {}`".format(warn_time)

            elif warn_mode == 5:
                text = "You need to specify an action to take upon too many warns.\n" \
                       "Current mode: `tban for {}`".format(warn_time)
            msg.reply_text(text, parse_mode=ParseMode.MARKDOWN)

        else:
            msg.reply_text(
                "You need to specify an action to take upon too many warns. "
                "Current modes are: ban/kick/mute/tmute/tban",
                parse_mode=ParseMode.MARKDOWN)
    return ""
Exemple #6
0
def warn(user: User,
         chat: Chat,
         reason: str,
         message: Message,
         warner: User = None) -> str:
    if is_user_admin(chat, user.id):  #ignore for admins
        #         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."

    keyboard = InlineKeyboardMarkup([[
        InlineKeyboardButton("Remove warn (admin only)",
                             callback_data="rm_warn({})".format(user.id))
    ]])

    limit, soft_warn, warn_mode = sql.get_warn_setting(chat.id)
    num_warns, reasons = sql.warn_user(user.id, chat.id, reason)
    num = 1
    warn_time = sql.get_warn_time(chat.id)
    if num_warns >= limit:
        sql.reset_warns(user.id, chat.id)
        if not soft_warn:
            if not warn_mode:
                chat.kick_member(user.id)
                reply = "{} warnings, {} has been banned!".format(
                    limit, mention_html(user.id, user.first_name))

            elif warn_mode == 1:
                chat.kick_member(user.id)
                reply = "{} warnings, {} has been banned!".format(
                    limit, mention_html(user.id, user.first_name))

            elif warn_mode == 2:
                chat.unban_member(user.id)
                reply = "{} warnings, {} has been kicked!".format(
                    limit, mention_html(user.id, user.first_name))

            elif warn_mode == 3:
                message.bot.restrict_chat_member(chat.id,
                                                 user.id,
                                                 can_send_messages=False)
                reply = "{} warnings, {} has been muted!".format(
                    limit, mention_html(user.id, user.first_name))

            elif warn_mode == 4:
                warn_time = sql.get_warn_time(chat.id)
                mutetime = extract_time(message, warn_time)
                message.bot.restrict_chat_member(chat.id,
                                                 user.id,
                                                 until_date=mutetime,
                                                 can_send_messages=False)
                reply = "{} warnings, {} has been temporarily muted for {}!".format(
                    limit, mention_html(user.id, user.first_name), warn_time)
            elif warn_mode == 5:
                warn_time = sql.get_warn_time(chat.id)
                tbantime = extract_time(message, warn_time)
                chat.kick_member(user.id, until_date=tbantime)
                reply = "{} warnings, {} has been temporarily banned for {}!".format(
                    limit, mention_html(user.id, user.first_name), warn_time)
        else:
            chat.kick_member(user.id)
            reply = "{} warnings, {} has been banned!".format(
                limit, mention_html(user.id, user.first_name))

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

        message.bot.send_sticker(chat.id,
                                 BAN_STICKER)  # banhammer natalie sticker

        log_reason = "<b>{}:</b>" \
                     "\n#WARN_ACTION" \
                     "\n<b>• Admin:</b> {}" \
                     "\n<b>• User:</b> {}" \
                     "\n<b>• ID:</b> <code>{}</code>".format(html.escape(chat.title),
                                                            warner_tag,
                                                            mention_html(user.id, user.first_name), user.id)
        if not warn_mode:
            log_reason += "\n<b>• Action:</b> banned"

        elif warn_mode == 1:
            log_reason += "\n<b>• Action:</b> banned"

        elif warn_mode == 2:
            log_reason += "\n<b>• Action:</b> kicked"

        elif warn_mode == 3:
            log_reason += "\n<b>• Action:</b> muted"

        elif warn_mode == 4:
            log_reason += "\n<b>• Action:</b> tmuted" \
                          "\n<b>• Time:</b> {}".format(warn_time)

        elif warn_mode == 5:
            log_reason += "\n<b>• Action:</b> tbanned" \
                          "\n<b>• Time:</b> {}".format(warn_time)

        log_reason += "\n<b>• Counts:</b> <code>{}/{}</code>".format(
            num_warns, limit)

        if reason:
            log_reason += "\n<b>• Reason:</b> {}".format(reason)

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

        if num_warns + 1 == limit:
            if not warn_mode:
                action_mode = "banned"
            elif warn_mode == 1:
                action_mode = "banned"
            elif warn_mode == 2:
                action_mode = "kicked"
            elif warn_mode == 3:
                action_mode = "muted"
            elif warn_mode == 4:
                action_mode = "temporarily muted"
            elif warn_mode == 5:
                action_mode = "temporarily banned"
            reply = "{} has {}/{} warnings... watch out, you'll be {} in the last warn!".format(
                mention_html(user.id, user.first_name), num_warns, limit,
                action_mode)
        else:
            reply = "{} 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> {}" \
                     "\n<b>• ID:</b> <code>{}</code>" \
                     "\n<b>• Counts:</b> <code>{}/{}</code>".format(html.escape(chat.title),
                                                                    warner_tag,
                                                                    mention_html(user.id, user.first_name), user.id,
                                                                    num_warns, limit)
        if reason:
            log_reason += "\n<b>• Reason:</b> {}".format(reason)

    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