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

    if len(args) >= 1:
        var = args[1]
        if var[:1] == "0":
            mutetime = "0"
            sql.set_flood_time(chat.id, str(var))
            text = "Flood time updated to permanent."
            log =  "<b>{}:</b>\n" \
                   "#FLOOD_TIME\n" \
                   "<b>• Admin:</b> {}\n" \
                   "Has disabled flood time and users will be permanently muted.".format(html.escape(chat.title),
                                                                                  mention_html(user.id,
                                                                                               user.first_name))
            message.reply_text(text)
            return log

        else:
            mutetime = extract_time(message, var)
            if mutetime == "":
                return ""
            sql.set_flood_time(chat.id, str(var))
            text = "Flood time updated to {}, Users will be restricted temporarily.".format(
                var)
            log =  "<b>{}:</b>\n" \
                   "#FLOOD_TIME\n" \
                   "<b>• Admin:</b> {}\n" \
                   "Has updated flood time and users will be muted for {}.".format(html.escape(chat.title),
                                                                                   mention_html(user.id,
                                                                                               user.first_name),
                                                                                               var)
            message.reply_text(text)
            return log
    else:
        if str(flood_time) == "0":
            message.reply_text(
                "Current settings: flood restrict time is permanent.")
            return ""
        else:
            message.reply_text(
                "Current settings: flood restrict time currently is {}.".
                format(flood_time))
            return ""
Exemple #2
0
def temp_nomedia(update: Update, context: CallbackContext) -> str:
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    message = update.effective_message  # type: Optional[Message]
    args = message.text.split(" ")

    user_id, reason = extract_user_and_text(message, args)

    if not user_id:
        message.reply_text("You don't seem to be referring to a user.")
        return ""

    try:
        member = chat.get_member(user_id)
    except BadRequest as excp:
        if excp.message == "User not found":
            message.reply_text("I can't seem to find this user")
            return ""
        else:
            raise

    if is_user_admin(chat, user_id, member):
        message.reply_text("I really wish I could restrict admins...")
        return ""

    if user_id == context.bot.id:
        message.reply_text("I'm not gonna RESTRICT myself, are you crazy?")
        return ""

    if not reason:
        message.reply_text(
            "You haven't specified a time to restrict this user for!")
        return ""

    split_reason = reason.split(None, 1)

    time_val = split_reason[0].lower()
    if len(split_reason) > 1:
        reason = split_reason[1]
    else:
        reason = ""

    mutetime = extract_time(message, time_val)

    if not mutetime:
        return ""

    log = "<b>{}:</b>" \
          "\n#TEMP RESTRICTED" \
          "\n<b>• Admin:</b> {}" \
          "\n<b>• User:</b> {}" \
          "\n<b>• ID:</b> <code>{}</code>" \
          "\n<b>• Time:</b> {}".format(html.escape(chat.title), mention_html(user.id, user.first_name),
                                       mention_html(member.user.id, member.user.first_name), user_id, time_val)
    if reason:
        log += "\n<b>• Reason:</b> {}".format(reason)

    try:
        if member.can_send_messages is None or member.can_send_messages:
            context.bot.restrict_chat_member(chat.id,
                                             user_id,
                                             NOMEDIA_PERMISSIONS,
                                             until_date=mutetime)
            message.reply_text(
                "{} restricted from sending media for {}!".format(
                    mention_html(member.user.id, member.user.first_name),
                    time_val),
                parse_mode=ParseMode.HTML)
            return log
        else:
            message.reply_text("This user is already restricted.")

    except BadRequest as excp:
        if excp.message == "Reply message not found":
            # Do not reply
            message.reply_text(
                "{} restricted from sending media for {}!".format(
                    mention_html(member.user.id, member.user.first_name),
                    time_val),
                parse_mode=ParseMode.HTML,
                quote=False)
            return log
        else:
            LOGGER.warning(update)
            LOGGER.exception("ERROR muting user %s in chat %s (%s) due to %s",
                             user_id, chat.title, chat.id, excp.message)
            message.reply_text("Well damn, I can't restrict that user.")

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

    user_id, reason = extract_user_and_text(message, args)

    if not user_id:
        message.reply_text("You don't seem to be referring to a user.")
        return ""

    try:
        member = chat.get_member(user_id)
    except BadRequest as excp:
        if excp.message == "User not found":
            message.reply_text("I can't seem to find this user")
            return ""
        else:
            raise

    if is_user_ban_protected(chat, user_id, member):
        message.reply_text("I really wish I could ban admins...")
        return ""

    if user_id == context.bot.id:
        message.reply_text("I'm not gonna BAN myself, are you crazy?")
        return ""

    if not reason:
        message.reply_text(
            "You haven't specified a time to ban this user for!")
        return ""

    split_reason = reason.split(None, 1)

    time_val = split_reason[0].lower()
    if len(split_reason) > 1:
        reason = split_reason[1]
    else:
        reason = ""

    bantime = extract_time(message, time_val)

    if not bantime:
        return ""

    log = "<b>{}:</b>" \
          "\n#TEMPBAN" \
          "\n<b>• Admin:</b> {}" \
          "\n<b>• User:</b> {}" \
          "\n<b>• ID:</b> <code>{}</code>" \
          "\n<b>• Time:</b> {}".format(html.escape(chat.title), mention_html(user.id, user.first_name),
                                                                mention_html(member.user.id, member.user.first_name),
                                                                             user_id, time_val)
    if reason:
        log += "\n<b>• Reason:</b> {}".format(reason)

    try:
        chat.kick_member(user_id, until_date=bantime)
        context.bot.send_sticker(update.effective_chat.id,
                                 BAN_STICKER)  # banhammer marie sticker
        reply = "{} has been temporarily banned for {}!".format(
            mention_html(member.user.id, member.user.first_name), time_val)
        message.reply_text(reply, parse_mode=ParseMode.HTML)
        return log

    except BadRequest as excp:
        if excp.message == "Reply message not found":
            # Do not reply
            message.reply_text(
                "Banned! User will be banned for {}.".format(time_val),
                quote=False)
            return log
        else:
            LOGGER.warning(update)
            LOGGER.exception("ERROR banning user %s in chat %s (%s) due to %s",
                             user_id, chat.title, chat.id, excp.message)
            message.reply_text("Well damn, I can't ban that user.")

    return ""
Exemple #4
0
def check_flood(update: Update, context: CallbackContext) -> str:
    user = update.effective_user  # type: Optional[User]
    chat = update.effective_chat  # type: Optional[Chat]
    msg = update.effective_message  # type: Optional[Message]
    limit = sql.get_flood_limit(chat.id)
    flood_time = sql.get_flood_time(chat.id)

    if not user:  # ignore channels
        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:  # mute
        if flood_time[:1] == "0":
            reply_perm = "I don't like the flooding, Remain quiet!" \
                    "\n{} has been muted!".format(mention_html(user.id, user.first_name))
            context.bot.restrict_chat_member(chat.id, user.id, permissions)
            msg.reply_text(reply_perm, parse_mode=ParseMode.HTML)
            msg.delete()
        else:
            mutetime = extract_time(update.effective_message, flood_time)
            reply_temp = "I don't like the flooding, Remain quiet for {}!" \
                     "\n{} has been muted!".format(flood_time, mention_html(user.id, user.first_name))
            context.bot.restrict_chat_member(chat.id,
                                             user.id,
                                             permissions,
                                             until_date=mutetime)
            msg.reply_text(reply_temp, parse_mode=ParseMode.HTML)
            msg.delete()

    else:  # ban
        chat.kick_member(user.id)
        reply_ban = "Frankly, I like to leave the flooding to natural disasters." \
                    "\n{} has been banned!".format(mention_html(user.id, user.first_name))
        msg.reply_text(reply_ban, parse_mode=ParseMode.HTML)
        msg.delete()
    try:
        log = "<b>{}:</b>" \
              "\n#FLOOD_CONTROL" \
              "\n<b>• User:</b> {}" \
              "\n<b>• ID:</b> <code>{}</code>".format(html.escape(chat.title), mention_html(user.id, user.first_name), user.id)

        if soft_flood:
            log += "\n<b>• Action:</b> muted"
            log += "\n<b>• Time:</b> {}".format(flood_time)

            if flood_time[:1] == "0":
                log += "\n<b>• Time:</b> permanently"

        else:
            log += "\n<b>• Action:</b> banned"

        log += "\n<b>• Reason:</b> Exceeded flood limit of {} consecutive messages.".format(
            limit)

        return log

    except BadRequest:
        msg.reply_text(
            "I can't kick people here, give me permissions 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)
Exemple #5
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