Example #1
0
def del_lockables(bot: Bot, update: Update):
    chat = update.effective_chat  # type: Optional[Chat]
    message = update.effective_message  # type: Optional[Message]

    for lockable, filter in LOCK_TYPES.items():
        if lockable == "rtl":
            if sql.is_locked(chat.id, lockable) and can_delete(chat, bot.id):
                if message.caption:
                    check = ad.detect_alphabet(u'{}'.format(message.caption))
                    if 'ARABIC' in check:
                        try:
                            message.delete()
                        except BadRequest as excp:
                            if excp.message == "Message to delete not found":
                                pass
                            else:
                                LOGGER.exception("ERROR in lockables")
                if message.text:
                    check = ad.detect_alphabet(u'{}'.format(message.text))
                    if 'ARABIC' in check:
                        try:
                            message.delete()
                        except BadRequest as excp:
                            if excp.message == "Message to delete not found":
                                pass
                            else:
                                LOGGER.exception("ERROR in lockables")
            break
        if filter(message) and sql.is_locked(chat.id, lockable) and can_delete(
                chat, bot.id):
            if lockable == "bots":
                new_members = update.effective_message.new_chat_members
                for new_mem in new_members:
                    if new_mem.is_bot:
                        if not is_bot_admin(chat, bot.id):
                            message.reply_text(
                                "Saya melihat bot, dan saya diberitahu untuk menghentikan mereka bergabung... "
                                "tapi saya bukan admin!")
                            return

                        chat.kick_member(new_mem.id)
                        message.reply_text(
                            "Hanya admin yang diizinkan menambahkan bot ke obrolan ini! Keluar dari sini!"
                        )
            else:
                try:
                    message.delete()
                except BadRequest as excp:
                    if excp.message == "Message to delete not found":
                        pass
                    else:
                        LOGGER.exception("ERROR in lockables")

            break
def purge(bot: Bot, update: Update, args: List[str]) -> str:
    spam = spamfilters(update.effective_message.text,
                       update.effective_message.from_user.id,
                       update.effective_chat.id, update.effective_message)
    if spam == True:
        return
    msg = update.effective_message  # type: Optional[Message]
    if msg.reply_to_message:
        user = update.effective_user  # type: Optional[User]
        chat = update.effective_chat  # type: Optional[Chat]
        if can_delete(chat, bot.id):
            message_id = msg.reply_to_message.message_id
            if args and args[0].isdigit():
                delete_to = message_id + int(args[0])
            else:
                delete_to = msg.message_id - 1
            for m_id in range(delete_to, message_id - 1,
                              -1):  # Reverse iteration over message ids
                try:
                    bot.deleteMessage(chat.id, m_id)
                except BadRequest as err:
                    if err.message == "Message can't be deleted":
                        bot.send_message(
                            chat.id,
                            "Tidak dapat menghapus semua pesan. Pesannya mungkin terlalu lama, saya mungkin "
                            "tidak memiliki hak menghapus, atau ini mungkin bukan supergrup."
                        )

                    elif err.message != "Message to delete not found":
                        LOGGER.exception("Error while purging chat messages.")

            try:
                msg.delete()
            except BadRequest as err:
                if err.message == "Message can't be deleted":
                    bot.send_message(
                        chat.id,
                        "Tidak dapat menghapus semua pesan. Pesannya mungkin terlalu lama, saya mungkin "
                        "tidak memiliki hak menghapus, atau ini mungkin bukan supergrup."
                    )

                elif err.message != "Message to delete not found":
                    LOGGER.exception("Error while purging chat messages.")

            bot.send_message(chat.id, "Pembersihan selesai.")
            return "<b>{}:</b>" \
                   "\n#PURGE" \
                   "\n<b>Admin:</b> {}" \
                   "\nDibersihkan <code>{}</code> pesan.".format(html.escape(chat.title),
                                                               mention_html(user.id, user.first_name),
                                                               delete_to - message_id)

    else:
        msg.reply_text("Balas pesan untuk memilih tempat mulai membersihkan.")

    return ""
Example #3
0
def rest_handler(bot: Bot, update: Update):
    msg = update.effective_message  # type: Optional[Message]
    chat = update.effective_chat  # type: Optional[Chat]
    for restriction, filter in RESTRICTION_TYPES.items():
        if filter(msg) and sql.is_restr_locked(chat.id, restriction) and can_delete(chat, bot.id):
            try:
                msg.delete()
            except BadRequest as excp:
                if excp.message == "Message to delete not found":
                    pass
                else:
                    LOGGER.exception("ERROR in restrictions")
            break
Example #4
0
def purge(update,context):
    args = context.args
    msg = update.effective_message  # type: Optional[Message]
    if msg.reply_to_message:
        user = update.effective_user  # type: Optional[User]
        chat = update.effective_chat  # type: Optional[Chat]
        if can_delete(chat, context.bot.id):
            message_id = msg.reply_to_message.message_id
            if args and args[0].isdigit():
                if int(args[0]) < int(1):
                     return

                delete_to = message_id + int(args[0])
            else:
                delete_to = msg.message_id - 1
            for m_id in range(delete_to, message_id - 1, -1):  # Reverse iteration over message ids
                try:
                    context.bot.deleteMessage(chat.id, m_id)
                except BadRequest as err:
                    if err.message == "Message can't be deleted":
                        send_message(update.effective_message, "Cannot delete all messages. The messages may be too old, I might "
                                                  "not have delete rights, or this might not be a supergroup.")

                    elif err.message != "Message to delete not found":
                        LOGGER.exception("Error while purging chat messages.")

            try:
                msg.delete()
            except BadRequest as err:
                if err.message == "Message can't be deleted":
                    send_message(update.effective_message, "Cannot delete all messages. The messages may be too old, I might "
                                              "not have delete rights, or this might not be a supergroup.")

                elif err.message != "Message to delete not found":
                    LOGGER.exception("Error while purging chat messages.")

            send_message(update.effective_message, "Purge complete.")
            return "<b>{}:</b>" \
                   "\n#PURGE" \
                   "\n<b>• Admin:</b> {}" \
                   "\nPurged <code>{}</code> messages.".format(html.escape(chat.title),
                                                               mention_html(user.id, user.first_name),
                                                               delete_to - message_id)

    else:
        msg.reply_text("Reply to a message to select where to start purging from.")

    return ""
Example #5
0
def del_message(update,context) -> str:
    if update.effective_message.reply_to_message:
        user = update.effective_user  # type: Optional[User]
        chat = update.effective_chat  # type: Optional[Chat]
        if can_delete(chat, context.bot.id):
            update.effective_message.reply_to_message.delete()
            update.effective_message.delete()
            return "<b>{}:</b>" \
                   "\n#DEL" \
                   "\n<b>• Admin:</b> {}" \
                   "\nMessage deleted.".format(html.escape(chat.title),
                                               mention_html(user.id, user.first_name))
    else:
        update.effective_message.reply_text("Whadya want to delete?")

    return ""
Example #6
0
def del_message(bot: Bot, update: Update) -> str:
    spam = spamfilters(update.effective_message.text, update.effective_message.from_user.id, update.effective_chat.id, update.effective_message)
    if spam == True:
        return
    if update.effective_message.reply_to_message:
        user = update.effective_user  # type: Optional[User]
        chat = update.effective_chat  # type: Optional[Chat]
        if can_delete(chat, bot.id):
            update.effective_message.reply_to_message.delete()
            update.effective_message.delete()
            return "<b>{}:</b>" \
                   "\n#DEL" \
                   "\n<b>Admin:</b> {}" \
                   "\nMessage deleted.".format(html.escape(chat.title),
                                               mention_html(user.id, user.first_name))
    else:
        send_message(update.effective_message, tl(update.effective_message, "Apa yang ingin di hapus?"))

    return ""
Example #7
0
def del_message(bot: Bot, update: Update) -> str:
    spam = spamfilters(update.effective_message.text, update.effective_message.from_user.id, update.effective_chat.id)
    if spam == True:
        return update.effective_message.reply_text("Saya kecewa dengan anda, saya tidak akan mendengar kata-kata anda sekarang!")
    if update.effective_message.reply_to_message:
        user = update.effective_user  # type: Optional[User]
        chat = update.effective_chat  # type: Optional[Chat]
        if can_delete(chat, bot.id):
            update.effective_message.reply_to_message.delete()
            update.effective_message.delete()
            return "<b>{}:</b>" \
                   "\n#DEL" \
                   "\n<b>Admin:</b> {}" \
                   "\nPesan dihapus.".format(html.escape(chat.title),
                                               mention_html(user.id, user.first_name))
    else:
        update.effective_message.reply_text("Apa yang ingin di hapus?")

    return ""
Example #8
0
def del_lockables(update, context):
    chat = update.effective_chat  # type: Optional[Chat]
    message = update.effective_message  # type: Optional[Message]

    for lockable, filter in LOCK_TYPES.items():
        if lockable == "rtl":
            if sql.is_locked(chat.id, lockable) and can_delete(
                    chat, context.bot.id):
                if message.caption:
                    check = ad.detect_alphabet(u'{}'.format(message.caption))
                    if 'ARABIC' in check:
                        try:
                            message.delete()
                        except BadRequest as excp:
                            if excp.message == "Message to delete not found":
                                pass
                            else:
                                LOGGER.exception("ERROR in lockables")
                        getconf = sql.get_lockconf(chat.id)
                        if getconf:
                            warn(
                                update.effective_user,
                                chat,
                                tl(
                                    update.effective_message,
                                    "Mengirim 'Teks RTL' yang sedang di kunci saat ini"
                                ),
                                message,
                                update.effective_user,
                                conn=False)
                        break
                if message.text:
                    check = ad.detect_alphabet(u'{}'.format(message.text))
                    if 'ARABIC' in check:
                        try:
                            message.delete()
                        except BadRequest as excp:
                            if excp.message == "Message to delete not found":
                                pass
                            else:
                                LOGGER.exception("ERROR in lockables")
                        getconf = sql.get_lockconf(chat.id)
                        if getconf:
                            warn(
                                update.effective_user,
                                chat,
                                tl(
                                    update.effective_message,
                                    "Mengirim 'Teks RTL' yang sedang di kunci saat ini"
                                ),
                                message,
                                update.effective_user,
                                conn=False)
                        break
            continue
        if lockable == "button":
            if sql.is_locked(chat.id, lockable) and can_delete(
                    chat, context.bot.id):
                if message.reply_markup and message.reply_markup.inline_keyboard:
                    try:
                        message.delete()
                    except BadRequest as excp:
                        if excp.message == "Message to delete not found":
                            pass
                        else:
                            LOGGER.exception("ERROR in lockables")
                    getconf = sql.get_lockconf(chat.id)
                    if getconf:
                        warn(
                            update.effective_user,
                            chat,
                            tl(
                                update.effective_message,
                                "Mengirim 'Pesan Tombol' yang sedang di kunci saat ini"
                            ),
                            message,
                            update.effective_user,
                            conn=False)
                    break
            continue
        if filter(update) and sql.is_locked(chat.id, lockable) and can_delete(
                chat, context.bot.id):
            if lockable == "bots":
                new_members = update.effective_message.new_chat_members
                for new_mem in new_members:
                    if new_mem.is_bot:
                        if not is_bot_admin(chat, context.bot.id):
                            send_message(
                                update.effective_message,
                                tl(
                                    update.effective_message,
                                    "Saya melihat bot, dan saya diberitahu untuk menghentikan mereka bergabung... "
                                    "tapi saya bukan admin!"))
                            return

                        chat.kick_member(new_mem.id)
                        send_message(
                            update.effective_message,
                            tl(
                                update.effective_message,
                                "Hanya admin yang diizinkan menambahkan bot ke obrolan ini! Keluar dari sini!"
                            ))
                        getconf = sql.get_lockconf(chat.id)
                        if getconf:
                            warn(
                                update.effective_user,
                                chat,
                                tl(
                                    update.effective_message,
                                    "Memasukan 'Bot' yang sedang di kunci saat ini"
                                ),
                                message,
                                update.effective_user,
                                conn=False)
                        break
            else:
                try:
                    message.delete()
                except BadRequest as excp:
                    if excp.message == "Message to delete not found":
                        pass
                    else:
                        LOGGER.exception("ERROR in lockables")
                getconf = sql.get_lockconf(chat.id)
                if getconf:
                    warn(update.effective_user,
                         chat,
                         tl(update.effective_message,
                            "Mengirim '{}' yang sedang di kunci saat ini").
                         format(lockable),
                         message,
                         update.effective_user,
                         conn=False)

                break
Example #9
0
def lock(update, context) -> str:
    args = context.args
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    message = update.effective_message  # type: Optional[Message]

    if can_delete(
            chat,
            context.bot.id) or update.effective_message.chat.type == "private":
        if len(args) >= 1:
            ltype = args[0].lower()
            if ltype in LOCK_TYPES:
                # Connection check
                conn = connected(context.bot,
                                 update,
                                 chat,
                                 user.id,
                                 need_admin=True)
                if conn:
                    chat = dispatcher.bot.getChat(conn)
                    chat_id = conn
                    chat_name = chat.title
                    text = tl(
                        update.effective_message,
                        "Terkunci pesan *{}* untuk semua non-admin pada *{}*!"
                    ).format(ltype, chat_name)
                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
                    text = tl(
                        update.effective_message,
                        "Terkunci pesan *{}* untuk semua non-admin!").format(
                            ltype)
                sql.update_lock(chat.id, ltype, locked=True)
                send_message(update.effective_message,
                             text,
                             parse_mode="markdown")

                return "<b>{}:</b>" \
                    "\n#LOCK" \
                    "\n<b>Admin:</b> {}" \
                    "\nLocked <code>{}</code>.".format(html.escape(chat.title),
                            mention_html(user.id, user.first_name), ltype)

            elif ltype in LOCK_CHAT_RESTRICTION:
                # Connection check
                conn = connected(context.bot,
                                 update,
                                 chat,
                                 user.id,
                                 need_admin=True)
                if conn:
                    chat = dispatcher.bot.getChat(conn)
                    chat_id = conn
                    chat_name = chat.title
                    text = tl(
                        update.effective_message,
                        "Izin terkunci pesan *{}* untuk semua non-admin pada *{}*!"
                    ).format(ltype, chat_name)
                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
                    text = tl(
                        update.effective_message,
                        "Izin terkunci pesan *{}* untuk semua non-admin!"
                    ).format(ltype)

                current_permission = context.bot.getChat(chat_id).permissions
                context.bot.set_chat_permissions(
                    chat_id=chat_id,
                    permissions=get_permission_list(
                        eval(str(current_permission)),
                        LOCK_CHAT_RESTRICTION[ltype.lower()]))

                send_message(update.effective_message,
                             text,
                             parse_mode="markdown")
                return "<b>{}:</b>" \
                    "\n#Permission_LOCK" \
                    "\n<b>Admin:</b> {}" \
                    "\nLocked <code>{}</code>.".format(html.escape(chat.title),
                            mention_html(user.id, user.first_name), ltype)

            else:
                send_message(
                    update.effective_message,
                    tl(
                        update.effective_message,
                        "Apa yang Anda coba untuk kunci...? Coba /locktypes untuk daftar kunci yang dapat dikunci"
                    ))
        else:
            send_message(
                update.effective_message,
                tl(update.effective_message, "Apa yang Anda ingin kunci...?"))

    else:
        send_message(
            update.effective_message,
            tl(update.effective_message,
               "Saya bukan admin, atau tidak punya hak menghapus."))

    return ""
Example #10
0
def lock(bot: Bot, update: Update, args: List[str]) -> str:
    spam = spamfilters(update.effective_message.text,
                       update.effective_message.from_user.id,
                       update.effective_chat.id, update.effective_message)
    if spam == True:
        return
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    message = update.effective_message  # type: Optional[Message]
    if can_delete(chat,
                  bot.id) or update.effective_message.chat.type == "private":
        if len(args) >= 1:
            if args[0] in LOCK_TYPES:
                # Connection check
                conn = connected(bot, update, chat, user.id, need_admin=True)
                if conn:
                    chat = dispatcher.bot.getChat(conn)
                    chat_id = conn
                    chat_name = chat.title
                    text = tl(
                        update.effective_message,
                        "Terkunci *{}* pesan untuk semua non-admin pada *{}*!"
                    ).format(args[0], chat_name)
                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
                    text = tl(
                        update.effective_message,
                        "Terkunci *{}* pesan untuk semua non-admin!").format(
                            args[0])
                sql.update_lock(chat.id, args[0], locked=True)
                send_message(update.effective_message,
                             text,
                             parse_mode="markdown")

                return "<b>{}:</b>" \
                       "\n#LOCK" \
                       "\n<b>Admin:</b> {}" \
                       "\nLocked <code>{}</code>.".format(html.escape(chat.title),
                                                          mention_html(user.id, user.first_name), args[0])

            elif args[0] in RESTRICTION_TYPES:
                # Connection check
                conn = connected(bot, update, chat, user.id, need_admin=True)
                if conn:
                    chat = dispatcher.bot.getChat(conn)
                    chat_id = conn
                    chat_name = chat.title
                    text = tl(
                        update.effective_message,
                        "Terkunci *{}* pesan untuk semua non-admin pada *{}*!"
                    ).format(args[0], chat_name)
                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
                    text = tl(
                        update.effective_message,
                        "Terkunci *{}* pesan untuk semua non-admin!").format(
                            args[0])
                sql.update_restriction(chat.id, args[0], locked=True)
                if args[0] == "previews":
                    members = users_sql.get_chat_members(str(chat.id))
                    restr_members(bot,
                                  chat.id,
                                  members,
                                  messages=True,
                                  media=True,
                                  other=True)

                send_message(update.effective_message,
                             text,
                             parse_mode="markdown")
                return "<b>{}:</b>" \
                       "\n#LOCK" \
                       "\n<b>Admin:</b> {}" \
                       "\nLocked <code>{}</code>.".format(html.escape(chat.title),
                                                          mention_html(user.id, user.first_name), args[0])

            else:
                send_message(
                    update.effective_message,
                    tl(
                        update.effective_message,
                        "Apa yang Anda coba untuk kunci...? Coba /locktypes untuk daftar kunci yang dapat dikunci"
                    ))
        else:
            send_message(
                update.effective_message,
                tl(update.effective_message, "Apa yang Anda ingin kunci...?"))

    else:
        send_message(
            update.effective_message,
            tl(update.effective_message,
               "Saya bukan admin, atau tidak punya hak menghapus."))

    return ""