Skip to content

alicebot.matcher

事件匹配器。

事件匹配器是 Bot.get() 方法的底层实现。

class EventMatcher

Bases: object

事件匹配器。

  • Attributes

    • func (collections.abc.Callable[[typing.Any], bool | collections.abc.Awaitable[bool]] | None)

    • bot (Bot)

    • event_type (type['Event[Any]'] | None)

    • adapter_type (type['Adapter[Any, Any]'] | None)

    • max_try_times (int | None)

    • timeout (int | float | None)

    • to_thread (bool)

    • event (anyio.Event)

    • try_times (int)

    • start_time (float)

    • result (AnyEvent | None)

    • exception (BaseException | None)

method __init__(self, func, *, bot, event_type, adapter_type, max_try_times, timeout, to_thread)

实例化一个 EventMatcher 对象。

  • Arguments

    • func (collections.abc.Callable[[typing.Any], bool | collections.abc.Awaitable[bool]] | None) - 原始函数。

    • bot (Bot) - 当前 Bot 对象。

    • event_type (type['Event[Any]'] | None) - 当指定时,只接受指定类型的事件,先于 func 条件生效。默认为 None

    • adapter_type (type['Adapter[Any, Any]'] | None) - 当指定时,只接受指定适配器产生的事件,先于 func 条件生效。默认为 None

    • max_try_times (int | None) - 最大事件数。

    • timeout (float | None) - 超时时间。

    • to_thread (bool) - 是否在独立的线程中运行同步函数。仅当 func 为同步函数时生效。

  • Returns

    Type: None

async method match(self, event)

检查当前事件是否被匹配。

  • Arguments

    • event (Event[Any]) - 事件。
  • Returns

    Type: bool

    是否被匹配。

  • Raises

    • GetEventTimeout - 超过最大事件数或超时。

async method run(self, event)

运行 get() 函数,检查当前 get() 是否成功。

  • Arguments

    • event (Event[Any]) - 当前被处理的事件。
  • Returns

    Type: bool | None

    返回 True 说明 Event 被 EventMatcher 成功处理了。 返回 False 说明 Event 未被 EventMatcher 成功处理。 返回 None 说明当前 EventMatcher 已经失效 (超时或异常)。

async method wait(self)

等待当前事件匹配器直到满足条件或者超时。

  • Returns

    Type: Event[Any]

    匹配到的事件。

  • Raises

    • GetEventTimeout - 事件匹配器超时。

    • RuntimeError - 内部错误。

Released under the MIT License.