Пример #1
0
async def test_running_behaviour_with_class_state(context, sample_parameters):
    sa, skwa = sample_parameters

    class State:
        pass

    ms = MiddlewareState(State)

    assert ms.state == State

    async def next(*args, ctx, **kwargs):
        assert ctx == context and list(args) == sa and kwargs == skwa
        assert isinstance(MiddlewareState.get_state(ctx, State), State)
        return 42

    assert await ms.run(*sa, ctx=context, next=next, **skwa) == 42
    assert isinstance(ms.state, State)

    prev_state = ms.state

    async def next(*args, ctx, **kwargs):
        assert ctx == context and list(args) == sa and kwargs == skwa
        assert MiddlewareState.get_state(ctx, State) == prev_state
        return 42

    assert await ms.run(*sa, ctx=context, next=next, **skwa) == 42
Пример #2
0
    def __init__(self) -> None:
        super().__init__()

        self._state = State()
        self._extension_middleware = [
            chain_of(
                [
                    Connect(),
                    MiddlewareState(self._state),
                    EventTypeFilter(EventType.CONNECT),
                    EventNormalization(),
                ]
            ),
            chain_of(
                [
                    Message(),
                    MiddlewareState(self._state),
                    Command("status"),
                    BotFilter(authored_by_bot=False),
                    EventTypeFilter(EventType.MESSAGE),
                    EventNormalization(),
                ]
            ),
        ]
Пример #3
0
async def test_running_behaviour_on_defaults(context, sample_parameters):
    sa, skwa = sample_parameters

    class State:
        pass

    state = State()
    ms = MiddlewareState(state)

    async def next(*args, ctx, **kwargs):
        assert ctx == context and list(args) == sa and kwargs == skwa
        assert hasattr(ctx, "states")
        assert ctx.states.get(State) == state
        assert MiddlewareState.get_state(ctx, State) == state
        return 42

    assert await ms.run(*sa, ctx=context, next=next, **skwa) == 42
Пример #4
0
    def __init__(self) -> None:
        super().__init__()

        self._state = State()
        self._client_middleware = [MiddlewareState(self._state)]
        self._extension_middleware = [
            chain_of([
                Join(),
                collection_of(
                    OneOfAll,
                    [Command("join"), Command("connect")]),
                ChannelTypeFilter(guild=True),
                BotFilter(authored_by_bot=False),
                EventTypeFilter(EventType.MESSAGE),
                EventNormalization(),
            ]),
            chain_of([
                Leave(),
                collection_of(
                    OneOfAll,
                    [Command("leave"), Command("disconnect")]),
                ChannelTypeFilter(guild=True),
                BotFilter(authored_by_bot=False),
                EventTypeFilter(EventType.MESSAGE),
                EventNormalization(),
            ]),
            chain_of([
                Volume(),
                Command("volume", rest_pattern="(?P<volume>.+)?"),
                Command("master"),
                ChannelTypeFilter(guild=True),
                BotFilter(authored_by_bot=False),
                EventTypeFilter(EventType.MESSAGE),
                EventNormalization(),
            ]),
        ]
Пример #5
0
    def __init__(self) -> None:
        super().__init__()

        self._state = State()
        self._extension_middleware = [
            chain_of([
                Play(),
                MiddlewareState(self._state),
                Command("play", rest_pattern="(?P<url>.+)?"),
                Command("player"),
                ChannelTypeFilter(guild=True),
                BotFilter(authored_by_bot=False),
                EventTypeFilter(EventType.MESSAGE),
                EventNormalization(),
            ]),
            chain_of([
                Pause(),
                MiddlewareState(self._state),
                Command("pause"),
                Command("player"),
                ChannelTypeFilter(guild=True),
                BotFilter(authored_by_bot=False),
                EventTypeFilter(EventType.MESSAGE),
                EventNormalization(),
            ]),
            chain_of([
                Resume(),
                MiddlewareState(self._state),
                Command("resume"),
                Command("player"),
                ChannelTypeFilter(guild=True),
                BotFilter(authored_by_bot=False),
                EventTypeFilter(EventType.MESSAGE),
                EventNormalization(),
            ]),
            chain_of([
                Stop(),
                MiddlewareState(self._state),
                Command("stop"),
                Command("player"),
                ChannelTypeFilter(guild=True),
                BotFilter(authored_by_bot=False),
                EventTypeFilter(EventType.MESSAGE),
                EventNormalization(),
            ]),
            chain_of([
                Skip(),
                MiddlewareState(self._state),
                Command("skip"),
                Command("player"),
                ChannelTypeFilter(guild=True),
                BotFilter(authored_by_bot=False),
                EventTypeFilter(EventType.MESSAGE),
                EventNormalization(),
            ]),
            chain_of([
                Volume(),
                MiddlewareState(self._state),
                Command("volume", rest_pattern="(?P<volume>.+)?"),
                Command("player"),
                ChannelTypeFilter(guild=True),
                BotFilter(authored_by_bot=False),
                EventTypeFilter(EventType.MESSAGE),
                EventNormalization(),
            ]),
        ]
Пример #6
0
 def __init__(self):
     self._client_middleware = [MiddlewareState(FirstState), first_mw]
     self._extension_middleware = [
         MiddlewareState(SecondState),
         second_mw,
     ]