예제 #1
0
파일: welcome.py 프로젝트: Jp-31/bobobot
def delete_join(update: Update, context: CallbackContext):
    chat = update.effective_chat  # type: Optional[Chat]
    join = update.effective_message.new_chat_members
    if can_delete(chat, context.bot.id):
        del_join = sql.get_del_pref(chat.id)
        if del_join:
            try:
                update.message.delete()
            except:
                LOGGER.log(2, "Could not delete join message. Line: 609")
예제 #2
0
파일: locks.py 프로젝트: Jp-31/bobobot
def rest_handler(update: Update, context: CallbackContext):
    msg = update.effective_message  # type: Optional[Message]
    chat = update.effective_chat  # type: Optional[Chat]
    for restriction, filter in RESTRICTION_TYPES.items():
        if filter(update) and sql.is_restr_locked(
                chat.id, restriction) and can_delete(chat, context.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
예제 #3
0
def del_message(update: Update, context: CallbackContext) -> 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 ""
예제 #4
0
def purge(update: Update, context: CallbackContext) -> str:
    msg = update.effective_message  # type: Optional[Message]
    args = msg.text.split(" ")
    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
            delete_to = msg.message_id - 1
            if args and len(args) > 1 and args[1].isdigit():
                new_del = message_id + (int(args[1]) - 1)
                # No point deleting messages which haven't been written yet.
                if new_del < delete_to:
                    delete_to = new_del

            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":
                        context.bot.send_message(
                            chat.id,
                            "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":
                    context.bot.send_message(
                        chat.id,
                        "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.")

            purge_msg = context.bot.send_message(
                chat.id, "Purged {} messages. "
                "\nThis message will be self-destructed in 3 seconds.".format(
                    str((delete_to - message_id) + 1)))
            time.sleep(3)
            purge_msg.delete()
            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) + 1)

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

    return ""
예제 #5
0
파일: locks.py 프로젝트: Jp-31/bobobot
def del_lockables(update: Update, context: CallbackContext):
    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")
                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(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):
                            message.reply_text(
                                "I see a bot, and I've been told to stop them joining... "
                                "but I'm not admin!")
                            return

                        chat.kick_member(new_mem.id)
                        message.reply_text(
                            "Only admins are allowed to add bots to this chat! Get outta here."
                        )
            else:
                chat_id = chat.id
                whitelist = get_whitelisted_urls(chat_id)
                for i in whitelist:
                    if i.__eq__(message.text):
                        return
                try:
                    message.delete()
                    lock_message = context.bot.send_message(
                        chat.id,
                        "Message deleted because it contained a locked item: {}."
                        .format(lockable))
                    time.sleep(2)
                    lock_message.delete()
                except BadRequest as excp:
                    if excp.message == "Message to delete not found":
                        pass
                    else:
                        LOGGER.exception("ERROR in lockables bots")

            break
예제 #6
0
파일: locks.py 프로젝트: Jp-31/bobobot
def lock(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(" ")

    if can_delete(chat, context.bot.id):
        if len(args) >= 2:
            if args[1] in LOCK_TYPES:
                sql.update_lock(chat.id, args[1], locked=True)
                message.reply_text(
                    "Locked {} messages for all non-admins!".format(args[1]))

                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[1])
            elif args[1] in "all":
                context.bot.set_chat_permissions(update.message.chat.id,
                                                 LOCK_PERMISSIONS)
                sql.update_restriction(chat.id, args[1], locked=True)
                message.reply_text(
                    "Locked {} messages for all non-admins!".format(args[1]))
                context.bot.send_message(chat.id,
                                         "***Chat is currently muted.***",
                                         parse_mode=ParseMode.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[1])

            elif args[1] in RESTRICTION_TYPES:
                sql.update_restriction(chat.id, args[1], locked=True)
                if args[1] == "previews":
                    members = users_sql.get_chat_members(str(chat.id))
                    restr_members(context.bot,
                                  chat.id,
                                  members,
                                  messages=True,
                                  media=True,
                                  other=True)

                message.reply_text("Locked {} for all non-admins!".format(
                    args[1]))
                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[1])

            else:
                message.reply_text(
                    "You've entered an unknown locktypes, Try /locktypes for the list of lockables"
                )

    else:
        message.reply_text(
            "I'm not an administrator, or haven't got delete rights.")

    return ""