Esempio n. 1
0
def chats(update, context):
    all_chats = users_db.get_all_chats() or []
    chatfile = "Daftar obrolan.\n"
    for chat in all_chats:
        chatfile += "{} - ({})\n".format(chat["chat_name"], chat["chat_id"])

    with BytesIO(str.encode(chatfile)) as output:
        output.name = "chatlist.txt"
        update.effective_message.reply_document(
            document=output,
            filename="chatlist.txt",
            caption="Berikut adalah daftar obrolan di database saya.",
        )
Esempio n. 2
0
def get_muted_chats(bot: Bot, update: Update, leave: bool = False):
    chat_id = update.effective_chat.id
    chats = users_db.get_all_chats()
    muted_chats, progress = 0, 0
    chat_list = []
    progress_message = None

    for chat in chats:

        if ((100 * chats.index(chat)) / len(chats)) > progress:
            progress_bar = f"{progress}% selesai dalam mendapatkan obrolan yang dibisukan."
            if progress_message:
                try:
                    bot.editMessageText(
                        progress_bar, chat_id, progress_message.message_id
                    )
                except BaseException:
                    pass
            else:
                progress_message = bot.sendMessage(chat_id, progress_bar)
            progress += 5

        cid = chat["chat_id"]
        sleep(0.5)

        try:
            bot.send_chat_action(cid, "TYPING", timeout=120)
        except (BadRequest, Unauthorized):
            muted_chats += +1
            chat_list.append(cid)
        except BaseException:
            pass

    try:
        progress_message.delete()
    except BaseException:
        pass

    if not leave:
        return muted_chats
    else:
        for muted_chat in chat_list:
            sleep(0.5)
            try:
                bot.leaveChat(muted_chat, timeout=120)
            except BaseException:
                pass
            users_db.rem_chat(muted_chat)
        return muted_chats
Esempio n. 3
0
def get_invalid_chats(bot: Bot, update: Update, remove: bool = False):
    chat_id = update.effective_chat.id
    chats = users_db.get_all_chats()
    kicked_chats, progress = 0, 0
    chat_list = []
    progress_message = None

    for chat in chats:

        if ((100 * chats.index(chat)) / len(chats)) > progress:
            progress_bar = f"{progress}% completed in getting invalid chats."
            if progress_message:
                try:
                    bot.editMessageText(progress_bar, chat_id,
                                        progress_message.message_id)
                except BaseException:
                    pass
            else:
                progress_message = bot.sendMessage(chat_id, progress_bar)
            progress += 5

        cid = chat["chat_id"]
        sleep(0.5)
        try:
            bot.get_chat(cid, timeout=120)
        except (BadRequest, Unauthorized):
            kicked_chats += 1
            chat_list.append(cid)
        except BaseException:
            pass

    try:
        progress_message.delete()
    except BaseException:
        pass

    if not remove:
        return kicked_chats
    else:
        for muted_chat in chat_list:
            sleep(0.5)
            users_db.rem_chat(muted_chat)
        return kicked_chats
Esempio n. 4
0
def broadcast(update, context):
    to_send = update.effective_message.text.split(None, 1)
    if len(to_send) >= 2:
        chats = users_db.get_all_chats() or []
        failed = 0
        for chat in chats:
            try:
                context.bot.sendMessage(int(chat["chat_id"]), to_send[1])
                sleep(0.1)
            except TelegramError:
                failed += 1
                LOGGER.warning(
                    "Couldn't send broadcast to %s, group name %s",
                    str(chat["chat_id"]),
                    str(chat["chat_name"]),
                )

        update.effective_message.reply_text(
            "Siaran selesai. {} grup mungkin gagal menerima pesan "
            "karena ditendang.".format(failed))
Esempio n. 5
0
def broadcast(update, context):
    to_send = update.effective_message.text.split(None, 1)

    if len(to_send) >= 2:
        to_group = False
        to_user = False
        if to_send[0] == '/broadcastgroups':
            to_group = True
        if to_send[0] == '/broadcastusers':
            to_user = True
        else:
            to_group = to_user = True
        chats = users_db.get_all_chats() or []
        users = users_db.get_all_users()
        failed = 0
        failed_user = 0
        if to_group:
            for chat in chats:
                try:
                    context.bot.sendMessage(
                        int(chat["chat_id"]),
                        to_send[1],
                        parse_mode="MARKDOWN",
                        disable_web_page_preview=True)
                    sleep(0.1)
                except TelegramError:
                    failed += 1
        if to_user:
            for user in users:
                try:
                    context.bot.sendMessage(
                        int(user["_id"]),
                        to_send[1],
                        parse_mode="MARKDOWN",
                        disable_web_page_preview=True)
                    sleep(0.1)
                except TelegramError:
                    failed_user += 1
        update.effective_message.reply_text(
            f"Siaran selesai.\nSiaran ke grup gagal: {failed}.\nSiaran ke pengguna gagal: {failed_user}."
        )
Esempio n. 6
0
def ungban(update, context):
    message = update.effective_message
    args = context.args
    user_id = extract_user(message, args)
    if not user_id:
        message.reply_text("Anda sepertinya tidak mengacu pada pengguna.")
        return

    user_chat = context.bot.get_chat(user_id)
    if user_chat.type != "private":
        message.reply_text("Itu bukan pengguna!")
        return

    if not gban_db.is_user_gbanned(user_id):
        message.reply_text("Pengguna ini tidak dilarang!")
        return

    banner = update.effective_user

    message.reply_text(
        "Saya akan memberi {} kesempatan kedua, secara global.".format(
            user_chat.first_name))

    context.bot.sendMessage(
        GBAN_LOGS,
        "<b>Regression of Global Ban</b>"
        "\n#UNGBAN"
        "\n<b>Status:</b> <code>Ceased</code>"
        "\n<b>Sudo Admin:</b> {}"
        "\n<b>User:</b> {}"
        "\n<b>ID:</b> <code>{}</code>".format(
            mention_html(banner.id, banner.first_name),
            mention_html(user_chat.id, user_chat.first_name),
            user_chat.id,
        ),
        parse_mode=ParseMode.HTML,
    )

    chats = get_all_chats()
    for chat in chats:
        chat_id = chat["chat_id"]

        # Check if this group has disabled gbans
        if not gban_db.does_chat_gban(chat_id):
            continue

        try:
            member = context.bot.get_chat_member(chat_id, user_id)
            if member.status == "kicked":
                context.bot.unban_chat_member(chat_id, user_id)

        except BadRequest as excp:
            if excp.message in UNGBAN_ERRORS:
                pass
            else:
                message.reply_text(
                    "Tidak dapat membatalkan gban karena: {}".format(
                        excp.message))
                context.bot.send_message(
                    OWNER_ID,
                    "Tidak dapat membatalkan gban karena: {}".format(
                        excp.message),
                )
                return
        except TelegramError:
            pass

    gban_db.ungban_user(user_id)
    message.reply_text("Tidak di larang lagi.")