Пример #1
0
def set_flood_mode(update, context):
    chat = update.effective_chat
    user = update.effective_user
    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,
                tl(update.effective_message,
                   "Anda bisa lakukan command ini pada grup, bukan pada 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() == 'ban':
            settypeflood = tl(update.effective_message, 'blokir')
            sql.set_flood_strength(chat_id, 1, "0")
        elif args[0].lower() == 'kick':
            settypeflood = tl(update.effective_message, 'tendang')
            sql.set_flood_strength(chat_id, 2, "0")
        elif args[0].lower() == 'mute':
            settypeflood = tl(update.effective_message, 'bisukan')
            sql.set_flood_strength(chat_id, 3, "0")
        elif args[0].lower() == 'tban':
            if len(args) == 1:
                teks = tl(
                    update.effective_message,
                    """Sepertinya Anda mencoba menetapkan nilai sementara untuk anti-banjir, tetapi belum menentukan waktu; gunakan `/setfloodmode tban <timevalue>`.

Contoh nilai waktu: 4m = 4 menit, 3h = 3 jam, 6d = 6 hari, 5w = 5 minggu.""")
                send_message(update.effective_message,
                             teks,
                             parse_mode="markdown")
                return
            settypeflood = tl(update.effective_message,
                              "blokir sementara selama {}").format(args[1])
            sql.set_flood_strength(chat_id, 4, str(args[1]))
        elif args[0].lower() == 'tmute':
            if len(args) == 1:
                teks = tl(
                    update.effective_message,
                    """Sepertinya Anda mencoba menetapkan nilai sementara untuk anti-banjir, tetapi belum menentukan waktu; gunakan `/setfloodmode tban <timevalue>`.

Contoh nilai waktu: 4m = 4 menit, 3h = 3 jam, 6d = 6 hari, 5w = 5 minggu.""")
                send_message(update.effective_message,
                             teks,
                             parse_mode="markdown")
                return
            settypeflood = tl(update.effective_message,
                              'bisukan sementara selama {}').format(args[1])
            sql.set_flood_strength(chat_id, 5, str(args[1]))
        else:
            send_message(
                update.effective_message,
                tl(update.effective_message,
                   "Saya hanya mengerti ban/kick/mute/tban/tmute!"))
            return
        if conn:
            text = tl(
                update.effective_message,
                "Terlalu banyak mengirim pesan sekarang akan menghasilkan `{}` pada *{}*!"
            ).format(settypeflood, chat_name)
        else:
            text = tl(
                update.effective_message,
                "Terlalu banyak mengirim pesan sekarang akan menghasilkan `{}`!"
            ).format(settypeflood)
        send_message(update.effective_message, text, parse_mode="markdown")
        return "<b>{}:</b>\n" \
               "<b>Admin:</b> {}\n" \
               "Has changed antiflood mode. User will {}.".format(settypeflood, html.escape(chat.title),
                                                                  mention_html(user.id, user.first_name))
    else:
        getmode, getvalue = sql.get_flood_setting(chat.id)
        if getmode == 1:
            settypeflood = tl(update.effective_message, 'blokir')
        elif getmode == 2:
            settypeflood = tl(update.effective_message, 'tendang')
        elif getmode == 3:
            settypeflood = tl(update.effective_message, 'bisukan')
        elif getmode == 4:
            settypeflood = tl(update.effective_message,
                              'blokir sementara selama {}').format(getvalue)
        elif getmode == 5:
            settypeflood = tl(update.effective_message,
                              'bisukan sementara selama {}').format(getvalue)
        if conn:
            text = tl(
                update.effective_message,
                "Jika member mengirim pesan beruntun, maka dia akan *di {}* pada *{}*."
            ).format(settypeflood, chat_name)
        else:
            text = tl(
                update.effective_message,
                "Jika member mengirim pesan beruntun, maka dia akan *di {}*."
            ).format(settypeflood)
        send_message(update.effective_message,
                     text,
                     parse_mode=ParseMode.MARKDOWN)
    return ""
Пример #2
0
def check_flood(update, context) -> str:
    user = update.effective_user
    chat = update.effective_chat
    msg = update.effective_message

    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 ""

    try:
        getmode, getvalue = sql.get_flood_setting(chat.id)
        if getmode == 1:
            chat.kick_member(user.id)
            execstrings = tl(update.effective_message, "Keluar!")
            tag = "BANNED"
        elif getmode == 2:
            chat.kick_member(user.id)
            chat.unban_member(user.id)
            execstrings = tl(update.effective_message, "Keluar!")
            tag = "KICKED"
        elif getmode == 3:
            context.bot.restrict_chat_member(
                chat.id,
                user.id,
                permissions=ChatPermissions(can_send_messages=False))
            execstrings = tl(update.effective_message, "Sekarang kamu diam!")
            tag = "MUTED"
        elif getmode == 4:
            bantime = extract_time(msg, getvalue)
            chat.kick_member(user.id, until_date=bantime)
            execstrings = tl(update.effective_message,
                             "Keluar selama {}!").format(getvalue)
            tag = "TBAN"
        elif getmode == 5:
            mutetime = extract_time(msg, getvalue)
            context.bot.restrict_chat_member(
                chat.id,
                user.id,
                until_date=mutetime,
                permissions=ChatPermissions(can_send_messages=False))
            execstrings = tl(update.effective_message,
                             "Sekarang kamu diam selama {}!").format(getvalue)
            tag = "TMUTE"
        send_message(
            update.effective_message,
            tl(
                update.effective_message,
                "Saya tidak suka orang yang mengirim pesan beruntun. Tapi kamu hanya membuat "
                "saya kecewa. {}").format(execstrings))

        return "<b>{}:</b>" \
               "\n#{}" \
               "\n<b>User:</b> {}" \
               "\nFlooded the group.".format(tag, html.escape(chat.title),
                                             mention_html(user.id, user.first_name))

    except BadRequest:
        send_message(
            update.effective_message,
            tl(
                update.effective_message,
                "Saya tidak bisa menendang orang di sini, beri saya izin terlebih dahulu! Sampai saat itu, saya akan menonaktifkan antiflood."
            ))
        sql.set_flood(chat.id, 0)
        return "<b>{}:</b>" \
               "\n#INFO" \
               "\n{}".format(chat.title, tl(update.effective_message,
                                            "Tidak memiliki izin kick, jadi secara otomatis menonaktifkan antiflood."))