Example #1
0
def handle_lift(message: telegram.Message, job_queue, chat: Chat):
    assert isinstance(message, telegram.Message)
    reply_to = message.reply_to_message
    user_id = message.from_user.id
    _ = partial(get_by_user, user=message.from_user)
    if not isinstance(reply_to, telegram.Message):
        return error_message(message, job_queue, _(Text.NEED_REPLY))
    elif reply_to.from_user.id == message.bot.id:
        return error_message(message, job_queue,
                             _(Text.NEED_REPLY_PLAYER_RECORD))
    elif reply_to.from_user.id != user_id and not is_gm(
            message.chat_id, user_id):
        return error_message(message, job_queue, _(Text.HAVE_NOT_PERMISSION))
    name = get_name(reply_to)
    if not name:
        return error_message(message, job_queue, _(Text.INVALID_TARGET))
    with_photo = handle_photo(reply_to)
    handle_say(message.bot,
               chat,
               job_queue,
               reply_to,
               name,
               RpgMessage(reply_to),
               with_photo=with_photo)
    delete_message(reply_to)
    delete_message(message)
Example #2
0
def handle_edit(bot,
                chat,
                job_queue,
                message: telegram.Message,
                start: int,
                with_photo=None):
    target = message.reply_to_message

    _ = partial(get_by_user, user=message.from_user)
    if not isinstance(target, telegram.Message):
        return error_message(message, job_queue, _(Text.NEED_REPLY))

    assert isinstance(message.from_user, telegram.User)
    user_id = message.from_user.id
    log = Log.objects.filter(chat=chat, message_id=target.message_id).first()
    if log is None:
        error_message(message, job_queue, _(Text.RECORD_NOT_FOUND))
    elif log.user_id == user_id:
        rpg_message = RpgMessage(message, start)
        handle_say(bot,
                   chat,
                   job_queue,
                   message,
                   log.character_name,
                   rpg_message,
                   edit_log=log,
                   with_photo=with_photo)
        delete_message(message)
    else:
        error_message(message, job_queue, _(Text.HAVE_NOT_PERMISSION))
Example #3
0
def handle_lift(message: telegram.Message, chat: Chat, **_kwargs):
    assert isinstance(message, telegram.Message)
    reply_to = message.reply_to_message
    user_id = message.from_user.id
    _ = partial(get_by_user, user=message.from_user)
    if not chat.recording:
        return error_message(message, _(Text.RECORD_NOT_FOUND))
    elif not isinstance(reply_to, telegram.Message):
        return error_message(message, _(Text.NEED_REPLY))
    elif reply_to.from_user.id == message.bot.id:
        return error_message(message, _(Text.NEED_REPLY_PLAYER_RECORD))
    elif reply_to.from_user.id != user_id and not is_gm(message.chat_id, user_id):
        return error_message(message, _(Text.HAVE_NOT_PERMISSION))
    name = get_name(reply_to)
    if not name:
        return error_message(message, _(Text.INVALID_TARGET))
    with_photo = get_maximum_photo(reply_to)
    handle_say(chat, reply_to, name, with_photo=with_photo)
    delete_message(reply_to.chat_id, reply_to.message_id)
    delete_message(message.chat_id, message.message_id)
Example #4
0
def handle_edit(job_queue: JobQueue,
                chat,
                bot,
                message: telegram.Message,
                start: int,
                with_photo=None,
                **_kwargs):
    target = message.reply_to_message
    assert isinstance(message.from_user, telegram.User)
    user_id = message.from_user.id

    _ = partial(get_by_user, user=message.from_user)

    if not chat.recording:
        return error_message(job_queue, message, _(Text.RECORD_NOT_FOUND))

    if not isinstance(target, telegram.Message):
        log = Log.objects.filter(chat=chat,
                                 user_id=user_id).order_by('-created').first()
        if not log:
            return error_message(job_queue, message, _(Text.NEED_REPLY))
    elif target.from_user.id == bot.id:
        log = Log.objects.filter(chat=chat,
                                 message_id=target.message_id).first()
        if not log:
            return error_message(job_queue, message, _(Text.RECORD_NOT_FOUND))
    else:
        return error_message(job_queue, message,
                             _(Text.NEED_REPLY_PLAYER_RECORD))

    if log.user_id != user_id:
        return error_message(job_queue, message, _(Text.HAVE_NOT_PERMISSION))
    handle_say(job_queue,
               chat,
               message,
               log.character_name,
               edit_log=log,
               with_photo=with_photo,
               start=start)
Example #5
0
def handle_message(update: telegram.Update, context: CallbackContext):
    bot = context .bot
    message: telegram.Message = update.message

    edit_log = None
    if update.edited_message:
        message = update.edited_message
        edit_log = Log.objects.filter(chat__chat_id=message.chat_id, source_message_id=message.message_id).first()
        cancel_delete_message(message.chat_id, message. message_id)
    elif not isinstance(message, telegram.Message):
        return
    elif not isinstance(message.from_user, telegram.User):
        return
    language_code: str = message.from_user.language_code

    def _(x: Text):
        return get(x, language_code)

    with_photo = get_maximum_photo(message)
    text = message.text
    if with_photo:
        text = message.caption

    if not isinstance(text, str):
        return
    # ignore ellipsis
    elif is_ellipsis(text):
        return

    if not is_group_chat(message.chat):
        message.reply_text(_(Text.NOT_GROUP))
        return

    chat = get_chat(message.chat)
    player = get_player_by_id(message.chat_id, message.from_user.id)

    # handle GM mode
    if player and player.is_gm and (is_start_gm_mode(text) or is_finish_gm_mode(text)):
        if is_start_gm_mode(text):
            start_gm_mode(bot, message, chat)
            if len(text.rstrip()) == 1:
                delete_message(message.chat_id, message.message_id)
                return
        elif is_finish_gm_mode(text):
            finish_gm_mode(message, chat)
            return
    # not start with . / 。, ignore
    elif not is_command(text):
        return

    # user hasn't set name
    if not player:
        error_message(message, _(Text.NOT_SET_NAME))
        return

    # in the GM mode
    if chat.gm_mode and not player.is_gm and not edit_log:
        if is_command(text):
            send_message(
                chat.chat_id,
                _(Text.PLAYER_IN_THE_GM_MODE),
                reply_to=message.message_id,
                delete_after=5
            )
        return

    name = player.character_name

    for pattern, handler in message_handlers:
        result = patterns.split(pattern, text)
        if not result:
            continue
        command, start = result
        rest = text[start:]
        if handler is not handle_as_say and edit_log:
            after_edit_delete_previous_message(edit_log.id)

        handler(
            bot=bot,
            chat=chat,
            player=player,
            command=command,
            start=start,
            name=name,
            text=rest,
            message=message,
            job_queue=context.job_queue,
            with_photo=with_photo,
            language_code=language_code,
            edit_log=edit_log,
            context=Context(bot, chat, player, command, name, start, rest, message,
                            context.job_queue, language_code, with_photo, edit_log)
        )
        return
    handle_say(chat, message, name, edit_log=edit_log, with_photo=with_photo, start=1)
Example #6
0
def handle_message(bot, update, job_queue):
    message = update.message
    assert isinstance(message, telegram.Message)
    _ = partial(get_by_user, user=message.from_user)

    language_code: str = message.from_user.language_code
    with_photo = handle_photo(message)
    text = message.text
    if with_photo:
        text = message.caption
    if not text or not text.startswith(('.', '。')):
        return
    if not is_group_chat(message.chat):
        message.reply_text(_(Text.NOT_GROUP))
        return
    elif not isinstance(message.from_user, telegram.User):
        return
    chat = get_chat(message.chat)
    player = get_player_by_id(message.chat_id, message.from_user.id)
    if not player:
        error_message(message, job_queue, _(Text.NOT_SET_NAME))
        return
    name = player.character_name

    handlers = [
        (re.compile(r'^[.。](rh?)\b'), handle_normal_roll),
        (re.compile(r'^[.。](hd)\b'), handle_normal_roll),
        (re.compile(r'^[.。](loh?)\b'), handle_loop_roll),
        (re.compile(r'^[.。](coch?[+\-]?h?)\s*'), handle_coc_roll),
        (re.compile(r'^[.。](init)\b'), handle_initiative),
        (re.compile(r'^[.。](set)\b'), handle_variable_assign),
        (re.compile(r'^[.。](list)\b'), handle_list_variables),
        (re.compile(r'^[.。](clear)\b'), handle_clear_variables),
        (re.compile(r'^[.。](as)\b'), handle_as_say),
        (re.compile(r'^[.。](del)\b'), handle_delete),
    ]

    for pat, handler in handlers:
        result = pattern.split(pat, text)
        if not result:
            continue
        command, start = result
        rest = text[start:]
        handler(
            bot=bot,
            chat=chat,
            player=player,
            command=command,
            start=start,
            name=name,
            text=rest,
            message=message,
            job_queue=job_queue,
            with_photo=with_photo,
            language_code=language_code,
        )
        return

    edit_command_matched = pattern.EDIT_COMMANDS_REGEX.match(text.lower())
    if edit_command_matched:
        command = edit_command_matched.group(1).lower()
        reply_to = message.reply_to_message
        if not chat.recording:
            error_message(message, job_queue, _(Text.RECORD_NOT_FOUND))
        elif not isinstance(reply_to, telegram.Message):
            error_message(message, job_queue, _(Text.NEED_REPLY))
        elif command == 'lift':
            handle_lift(message, job_queue, chat)
        elif reply_to.from_user.id != bot.id:
            error_message(message, job_queue, _(Text.NEED_REPLY_PLAYER_RECORD))
        elif command == 'edit':
            handle_edit(bot,
                        chat,
                        job_queue,
                        message,
                        start=edit_command_matched.end(),
                        with_photo=with_photo)
        elif command == 'tag':
            handle_add_tag(bot, chat, job_queue, message)
    else:
        rpg_message = RpgMessage(message, start=1)
        handle_say(bot,
                   chat,
                   job_queue,
                   message,
                   name,
                   rpg_message,
                   with_photo=with_photo)