Ejemplo n.º 1
0
def warns(bot: Bot, update: Update, args: List[str]):
    message = update.effective_message
    chat = update.effective_chat
    user_id = extract_user(message, args) or update.effective_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 = sql.get_warn_setting(chat.id)

        if reasons:
            text = tld(chat.id, 'warns_list_warns').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)
        else:
            update.effective_message.reply_text(
                tld(chat.id,
                    'warns_list_warns_no_reason').format(num_warns, limit))
    else:
        update.effective_message.reply_text(
            tld(chat.id, 'warns_list_warns_none'))
Ejemplo n.º 2
0
def set_warn_limit(update, context) -> str:
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    msg = update.effective_message  # type: Optional[Message]
    args = context.args

    conn = connected(context.bot, update, chat, user.id, need_admin=True)
    if conn:
        chat = dispatcher.bot.getChat(conn)
        chat_id = conn
        chat_name = dispatcher.bot.getChat(conn).title
    else:
        if update.effective_message.chat.type == "private":
            send_message(update.effective_message,
                         "You can do this command in groups, not PM")
            return ""
        chat = update.effective_chat
        chat_id = update.effective_chat.id
        chat_name = update.effective_message.chat.title

    if args:
        if args[0].isdigit():
            if int(args[0]) < 3:
                send_message(update.effective_message,
                             "The minimum warn limit is 3!")
            else:
                sql.set_warn_limit(chat.id, int(args[0]))
                if conn:
                    text = "Updated the warn limit to {} at *{}*".format(
                        args[0], chat_name)
                else:
                    text = "Updated the warn limit to {}".format(args[0])
                send_message(update.effective_message,
                             text,
                             parse_mode=ParseMode.HTML)
                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[0])
        else:
            send_message(update.effective_message,
                         "Give me a number as an arg!")
    else:
        limit, soft_warn, warn_mode = sql.get_warn_setting(chat.id)
        if conn:
            text = "The current warn limit is {} at *{}*".format(
                limit, chat_name)
        else:
            text = "The current warn limit is {}".format(limit)
        send_message(update.effective_message, text, parse_mode=ParseMode.HTML)
    return ""
Ejemplo n.º 3
0
def set_warn_limit(bot: Bot, update: Update, args: List[str]) -> str:
    chat = update.effective_chat
    user = update.effective_user
    msg = update.effective_message

    if args:
        if args[0].isdigit():
            if int(args[0]) < 1:
                msg.reply_text(tld(chat.id, 'warns_warnlimit_min_1'))
            else:
                sql.set_warn_limit(chat.id, int(args[0]))
                msg.reply_text(
                    tld(chat.id,
                        'warns_warnlimit_updated_success').format(args[0]))
                return tld(chat.id, 'warns_set_warn_limit_log_channel').format(
                    html.escape(chat.title),
                    mention_html(user.id, user.first_name), args[0])
        else:
            msg.reply_text(tld(chat.id, 'warns_warnlimit_invalid_arg'))
    else:
        limit, soft_warn = sql.get_warn_setting(chat.id)

        msg.reply_text(tld(chat.id, 'warns_warnlimit_current').format(limit))
    return ""
Ejemplo n.º 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, "kick" if soft_warn else "ban")
Ejemplo n.º 5
0
def set_warn_mode(update, context):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    msg = update.effective_message  # type: Optional[Message]
    args = context.args

    conn = connected(context.bot, update, chat, user.id, need_admin=True)
    if conn:
        chat = dispatcher.bot.getChat(conn)
        chat_id = conn
        chat_name = dispatcher.bot.getChat(conn).title
    else:
        if update.effective_message.chat.type == "private":
            send_message(update.effective_message,
                         "You can do this command in groups, not PM")
            return ""
        chat = update.effective_chat
        chat_id = update.effective_chat.id
        chat_name = update.effective_message.chat.title

    if args:
        if args[0].lower() in ("kick", "soft"):
            sql.set_warn_mode(chat.id, 1)
            if conn:
                text = "Too many warns will now result in a kick in *{}*! Users will be able to join again after.".format(
                    chat_name)
            else:
                text = "Too many warns will now result in a kick! Users will be able to join again after."
            send_message(update.effective_message,
                         text,
                         parse_mode=ParseMode.HTML)
            return "<b>{}:</b>\n" \
                   "<b>Admin:</b> {}\n" \
                   "Has changed the final warning to kick.".format(html.escape(chat.title),
                                                                            mention_html(user.id, user.first_name))

        elif args[0].lower() in ("ban", "banned", "hard"):
            sql.set_warn_mode(chat.id, 2)
            if conn:
                text = "Too many warns will now result in a ban on *{}*!".format(
                    chat_name)
            else:
                text = "Too many warns will now result in a ban!"
            send_message(update.effective_message,
                         text,
                         parse_mode=ParseMode.HTML)
            return "<b>{}:</b>\n" \
                   "<b>Admin:</b> {}\n" \
                   "Has changed the final warning to banned.".format(html.escape(chat.title),
                                                                                  mention_html(user.id,
                                                                                               user.first_name))

        elif args[0].lower() in ("mute"):
            sql.set_warn_mode(chat.id, 3)
            if conn:
                text = "Too many warns will now result in a ban on *{}*!".format(
                    chat_name)
            else:
                text = "Too many warns will now result in a ban!"
            send_message(update.effective_message,
                         text,
                         parse_mode=ParseMode.HTML)
            return "<b>{}:</b>\n" \
                   "<b>Admin:</b> {}\n" \
                   "Has changed the final warning to mute.".format(html.escape(chat.title),
                                                                                  mention_html(user.id,
                                                                                               user.first_name))

        else:
            send_message(update.effective_message,
                         "I only understood kick/ban/mute!")
    else:
        limit, soft_warn, warn_mode = sql.get_warn_setting(chat.id)
        if not soft_warn:
            if not warn_mode:
                if conn:
                    text = "Warns are currently set to *kick* users when they exceed the limits on *{}*.".format(
                        chat_name)
                else:
                    text = "Warns are currently set to *kick* users when they exceed the limits."
            elif warn_mode == 1:
                if conn:
                    text = "Warns are currently set to *kick* users when they exceed the limits on *{}*.".format(
                        chat_name)
                else:
                    text = "Warns are currently set to *kick* users when they exceed the limits."
            elif warn_mode == 2:
                if conn:
                    text = "Warns are currently set to *ban* users when they exceed the limits on *{}*.".format(
                        chat_name)
                else:
                    text = "Warns are currently set to *ban* users when they exceed the limits."
            elif warn_mode == 3:
                if conn:
                    text = "Warns are currently set to *mute* users when they exceed the limits on *{}*.".format(
                        chat_name)
                else:
                    text = "Warns are currently set to *mute* users when they exceed the limits."
            send_message(update.effective_message,
                         text,
                         parse_mode=ParseMode.MARKDOWN)
        else:
            if conn:
                text = "Warns are currently set to *ban* users when they exceed the limits on *{}*.".format(
                    chat_name)
            else:
                text = "Warns are currently set to *ban* users when they exceed the limits."
            send_message(update.effective_message,
                         text,
                         parse_mode=ParseMode.MARKDOWN)
    return ""
Ejemplo n.º 6
0
def set_warn_strength(update, context):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    msg = update.effective_message  # type: Optional[Message]
    args = context.args

    conn = connected(context.bot, update, chat, user.id, need_admin=True)
    if conn:
        chat = dispatcher.bot.getChat(conn)
        chat_id = conn
        chat_name = dispatcher.bot.getChat(conn).title
    else:
        if update.effective_message.chat.type == "private":
            send_message(update.effective_message,
                         "You can do this command in groups, not PM")
            return ""
        chat = update.effective_chat
        chat_id = update.effective_chat.id
        chat_name = update.effective_message.chat.title

    if args:
        if args[0].lower() in ("on", "yes"):
            sql.set_warn_strength(chat.id, False)
            if conn:
                text = "Too many warns will now result in a ban on *{}*!".format(
                    chat_name)
            else:
                text = "Too many warns will now result in a ban!"
            send_message(update.effective_message,
                         text,
                         parse_mode=ParseMode.HTML)
            return "<b>{}:</b>\n" \
                   "<b>Admin:</b> {}\n" \
                   "activated a strong warning. Users will be banned.".format(html.escape(chat.title),
                                                                            mention_html(user.id, user.first_name))

        elif args[0].lower() in ("off", "no"):
            sql.set_warn_strength(chat.id, True)
            if conn:
                text = "Too many warns will now result in a kick on *{}*! Users will be able to join again.".format(
                    chat_name)
            else:
                text = "Too many warns will now result in a kick Users will be able to join again."
            send_message(update.effective_message,
                         text,
                         parse_mode=ParseMode.HTML)
            return "<b>{}:</b>\n" \
                   "<b>Admin:</b> {}\n" \
                   "disabled strong warning. Users will only be kicked.".format(html.escape(chat.title),
                                                                                  mention_html(user.id,
                                                                                               user.first_name))

        else:
            send_message(update.effective_message,
                         "I only understood on/yes/no/off!")
    else:
        limit, soft_warn, warn_mode = sql.get_warn_setting(chat.id)
        if soft_warn:
            if conn:
                text = "Warns are currently set to *kick* users when they exceed the limits on *{}*.".format(
                    chat_name)
            else:
                text = "Warns are currently set to *kick* users when they exceed the limits."
            send_message(update.effective_message,
                         text,
                         parse_mode=ParseMode.MARKDOWN)
        else:
            if conn:
                text = "Warns are currently set to *ban* users when they exceed the limits on *{}*.".format(
                    chat_name)
            else:
                text = "Warns are currently set to *ban* users when they exceed the limits."
            send_message(update.effective_message,
                         text,
                         parse_mode=ParseMode.MARKDOWN)
    return ""
Ejemplo n.º 7
0
def warns(update, context):
    message = update.effective_message  # type: Optional[Message]
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    args = context.args

    conn = connected(context.bot, update, chat, user.id, need_admin=False)
    if conn:
        chat = dispatcher.bot.getChat(conn)
        chat_id = conn
        chat_name = dispatcher.bot.getChat(conn).title
    else:
        if update.effective_message.chat.type == "private":
            send_message(update.effective_message,
                         "You can do this command in groups, not PM")
            return ""
        chat = update.effective_chat
        chat_id = update.effective_chat.id
        chat_name = update.effective_message.chat.title

    user_id = extract_user(message, args) or update.effective_user.id
    result = sql.get_warns(user_id, chat.id)

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

        if reasons:
            if conn:
                text = "This user has {}/{} warnings at *{}*, 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(reason)

            msgs = split_message(text)
            for msg in msgs:
                send_message(update.effective_message,
                             msg,
                             parse_mode=ParseMode.HTML)
        else:
            if conn:
                send_message(
                    update.effective_message,
                    "User has {}/{} warnings at *{}*, but no reasons for any of them."
                    .format(num_warns, limit, chat_name),
                    parse_mode=ParseMode.HTML)
            else:
                send_message(
                    update.effective_message,
                    "User has {}/{} warnings, but no reasons for any of them.".
                    format(num_warns, limit))
    else:
        if conn:
            send_message(
                update.effective_message,
                "This user hasn't got any warnings in *{}*!".format(chat_name),
                parse_mode=ParseMode.HTML)
        else:
            send_message(update.effective_message,
                         "This user hasn't got any warnings!")
Ejemplo n.º 8
0
def warn(user: User,
         chat: Chat,
         reason: str,
         message: Message,
         warner: User = None,
         conn=False) -> str:
    bot = dispatcher.bot
    if is_user_admin(chat, user.id):
        send_message(update.effective_message, "this is admin of this group")
        return ""

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

    limit, soft_warn, warn_mode = 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 not soft_warn:
            if not warn_mode:
                chat.unban_member(user.id)
                reply = "{} warnings, {} has been kicked!".format(
                    limit, mention_html(user.id, user.first_name))
            elif warn_mode == 1:
                chat.unban_member(user.id)
                reply = "{} warnings, {} has been kicked!".format(
                    limit, mention_html(user.id, user.first_name))
            elif warn_mode == 2:
                chat.kick_member(user.id)
                reply = "{} warnings, {} has been banned!".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))
        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(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 = [[
            InlineKeyboardButton(text="Remove warn",
                                 callback_data="rm_warn({})".format(user.id))
        ]]
        rules = rules_sql.get_rules(chat.id)
        if rules:
            keyboard[0].append(
                InlineKeyboardButton(text="Rules",
                                     url="t.me/{}?start={}".format(
                                         bot.username, chat.id)))

        if num_warns + 1 == limit:
            if not warn_mode:
                action_mode = "Kicked"
            elif warn_mode == 1:
                action_mode = "Kicked"
            elif warn_mode == 2:
                action_mode = "banned"
            elif warn_mode == 3:
                action_mode = "muted"
            reply = "{} has {}/{} warnings... If you are warned again, you will be {}!".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> {} (<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:
        if conn:
            send_message_raw(chat.id,
                             reply,
                             reply_markup=InlineKeyboardMarkup(keyboard),
                             parse_mode=ParseMode.HTML)
        else:
            send_message_raw(chat.id,
                             reply,
                             reply_to_message_id=message.message_id,
                             reply_markup=InlineKeyboardMarkup(keyboard),
                             parse_mode=ParseMode.HTML)
        #send_message(update.effective_message, reply, reply_markup=InlineKeyboardMarkup(keyboard), parse_mode=ParseMode.HTML)
    except BadRequest as excp:
        if excp.message == "Reply message not found":
            # Do not reply
            if conn:
                message.bot.sendMessage(
                    chat.id,
                    reply,
                    reply_markup=InlineKeyboardMarkup(keyboard),
                    parse_mode=ParseMode.HTML)
            else:
                try:
                    message.bot.sendMessage(
                        chat.id,
                        reply,
                        reply_to_message_id=message.message_id,
                        reply_markup=InlineKeyboardMarkup(keyboard),
                        parse_mode=ParseMode.HTML,
                        quote=False)
                except BadRequest:
                    message.bot.sendMessage(
                        chat.id,
                        reply,
                        reply_markup=InlineKeyboardMarkup(keyboard),
                        parse_mode=ParseMode.HTML,
                        quote=False)
            #send_message(update.effective_message, reply, reply_markup=InlineKeyboardMarkup(keyboard), parse_mode=ParseMode.HTML, quote=False)
        else:
            raise
    return log_reason
Ejemplo n.º 9
0
def warn(user: User,
         chat: Chat,
         reason: str,
         message: Message,
         warner: User = None) -> str:
    bot = dispatcher.bot

    if is_user_admin(chat, user.id):
        message.reply_text(tld(chat.id, 'warns_warn_admin_no_warn'))
        return ""

    if warner:
        warner_tag = mention_html(warner.id, warner.first_name)
    else:
        warner_tag = tld(chat.id, 'warns_warn_admin_autofilter')

    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 = tld(chat.id, 'warns_max_warn_kick').format(
                limit, mention_html(user.id, user.first_name))

        else:  # ban
            chat.kick_member(user.id)
            reply = tld(chat.id, 'warns_max_warn_ban').format(
                limit, mention_html(user.id, user.first_name))

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

        keyboard = []
        log_reason = tld(chat.id, 'warns_warn_ban_log_channel').format(
            html.escape(chat.title), warner_tag,
            mention_html(user.id, user.first_name), user.id, reason, num_warns,
            limit)

    else:
        keyboard = [[
            InlineKeyboardButton(tld(chat.id, 'warns_btn_remove_warn'),
                                 callback_data="rm_warn({})".format(user.id))
        ]]
        rules = rules_sql.get_rules(chat.id)

        if rules:
            keyboard[0].append(
                InlineKeyboardButton(tld(chat.id, 'warns_btn_rules'),
                                     url="t.me/{}?start={}".format(
                                         bot.username, chat.id)))

        reply = tld(chat.id, 'warns_user_warned').format(
            mention_html(user.id, user.first_name), num_warns, limit)
        if reason:
            reply += tld(chat.id, 'warns_latest_warn_reason').format(
                html.escape(reason))

        log_reason = tld(chat.id, 'warns_warn_log_channel').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=InlineKeyboardMarkup(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=InlineKeyboardMarkup(keyboard),
                               parse_mode=ParseMode.HTML,
                               quote=False)
        else:
            raise
    return log_reason