示例#1
0
def test_context(client, sample_parameters):
    sa, skwa = sample_parameters
    context = Context(client, EventType.UNKNOWN, *sa, **skwa)

    assert context.client == client
    assert context.event == EventType.UNKNOWN
    assert context.args == sa
    assert context.kwargs == skwa
示例#2
0
async def test_ignoring(client):
    event = EventType.MESSAGE
    channel = Mock(spec=discord.DMChannel)
    context = Context(client,
                      event,
                      message=make_discord_object(0, channel=channel))

    ctf = ChannelTypeFilter(guild=True)
    assert not isr(await ctf.run(ctx=context, next=empty_next_callable))
示例#3
0
async def test_ignoring(client):
    event = EventType.MESSAGE
    pattern = r"some text"
    content = "A message without text to match"
    context = Context(client,
                      event,
                      message=make_discord_object(0, content=content))

    pf = PatternFilter(pattern)
    assert not isr(await pf.run(ctx=context, next=empty_next_callable))
示例#4
0
async def test_unknown(client):
    sa, skwa = [1, "2", [1, "2"]], {"other": [1, "2"]}

    en = EventNormalization()
    event = EventType.UNKNOWN

    async def check(*args, ctx, **kwargs):
        assert len(ctx.args) == len(sa)
        assert len(ctx.kwargs) == len(skwa)

    await en.run(ctx=Context(client, event, *sa, **skwa), next=check)
示例#5
0
async def test_pattern_with_named_groups_ignores_unnamed(client):
    event = EventType.MESSAGE
    pattern = r"find (\w+) and (?P<first>\w+)"
    content = "It should find 42 and firework but match only firework as first"
    context = Context(client,
                      event,
                      message=make_discord_object(0, content=content))

    @m(PatternFilter(pattern))
    async def mw(*args, ctx, next, first, **kwargs):
        assert first == "firework"

    assert isr(await mw.run(ctx=context, next=empty_next_callable))
示例#6
0
    async def run(self, *args, ctx: Context, next: Callable,
                  **kwargs) -> Union[MiddlewareResult, Any]:  # noqa: D102
        if ctx.event not in self.EVENT_FIELDS:
            return await next(*args, ctx=ctx, **kwargs)

        state = self._get_state(ctx)
        if state.is_processed:
            return await next(*args, ctx=ctx, **kwargs)

        for i, parameter in enumerate(self.EVENT_FIELDS[ctx.event]):
            ctx.kwargs[parameter] = ctx.args[i]

        state.is_processed = True
        return await next(*args, ctx=ctx, **kwargs)
示例#7
0
async def test_pattern_with_named_groups(client):
    event = EventType.MESSAGE
    pattern = r"find (?P<first>\w+) and (?P<second>\w+)"
    content = "It should find 42 and firework as first and second parameters"
    context = Context(client,
                      event,
                      message=make_discord_object(0, content=content))

    @m(PatternFilter(pattern))
    async def mw(*args, ctx, next, first, second, **kwargs):
        assert first == "42"
        assert second == "firework"

    assert isr(await mw.run(ctx=context, next=empty_next_callable))
示例#8
0
async def test(client, authored_by_bot, bot):
    event = EventType.MESSAGE
    context = Context(
        client,
        event,
        message=make_discord_object(0, author=make_discord_object(1, bot=bot)),
    )

    bf = BotFilter(authored_by_bot=authored_by_bot)

    if not authored_by_bot ^ bot:
        assert isr(await bf.run(ctx=context, next=empty_next_callable))
    else:
        assert not isr(await bf.run(ctx=context, next=empty_next_callable))
示例#9
0
async def test_with_positional(client):
    sa, skwa = [1, "2", [1, "2"]], {"other": [1, "2"]}

    en = EventNormalization()
    event = EventType.MESSAGE_EDIT
    context = Context(client, event, *sa, **skwa)
    event_fields = EventNormalization.EVENT_FIELDS[event]

    async def check(*args, ctx, **kwargs):
        assert len(ctx.args) == len(sa)
        assert len(ctx.kwargs) - len(event_fields) == len(skwa)

        for i, k in enumerate(event_fields):
            assert ctx.kwargs[k] == sa[i]

    await en.run(ctx=context, next=check)
    # Test one more time, it should process as well
    await en.run(ctx=context, next=check)
示例#10
0
    def dispatch(self, event: str, *args, **kwargs):  # noqa: D401
        """Wrapper around default event dispatcher for a client."""
        super().dispatch(event, *args, **kwargs)

        try:
            event_type = EventType(event)
        except ValueError:
            event_type = EventType.UNKNOWN
        #
        ctx = Context(self, event_type, *args, **kwargs)
        log.debug(f"Dispatching event `{event_type}`")

        self.loop.create_task(
            self._run_event(
                self.extension_manager.root_middleware.run,
                event,
                ctx=ctx,
                next=empty_next_callable,
            ))
示例#11
0
def context(client):
    """Return sample context."""
    return Context(client, EventType.UNKNOWN)
示例#12
0
async def test_passing(client):
    event = EventType.MESSAGE
    context = Context(client, event)

    etf = EventTypeFilter(event)
    assert isr(await etf.run(ctx=context, next=empty_next_callable))
示例#13
0
async def test_ignoring(client):
    event = EventType.READY
    context = Context(client, event)

    etf = EventTypeFilter(EventType.MESSAGE)
    assert not isr(await etf.run(ctx=context, next=empty_next_callable))
示例#14
0
 def _ensure_context(ctx: Context) -> None:
     """Checks is the context has states storage, and creates it if
     necessary."""
     if getattr(ctx, "states", None) is None:
         ctx.states = dict()
示例#15
0
 def set_state(ctx: Context, state: Any) -> None:
     """Sets the state to the context."""
     MiddlewareState._ensure_context(ctx)
     ctx.states[type(state)] = state