跳转至

send

本模块包含许多用于 Ariadne.SendMessage 的 action 函数

Bypass 🔗

Bases: SendMessageAction

透传错误的 SendMessage action (有 Exception 时直接返回它而不抛出)

注意, 请小心 traceback 重生.

Source code in src/graia/ariadne/util/send.py
13
14
15
16
17
18
19
20
21
22
class Bypass(SendMessageAction):
    """
    透传错误的 SendMessage action (有 Exception 时直接返回它而不抛出)

    注意, 请小心 traceback 重生.
    """

    @staticmethod
    async def exception(item: Exc_T) -> Exc_T:
        return item

Ignore 🔗

Bases: SendMessageAction

忽略错误的 SendMessage action (发生 Exception 时 返回 None)

Source code in src/graia/ariadne/util/send.py
33
34
35
36
37
38
class Ignore(SendMessageAction):
    """忽略错误的 SendMessage action (发生 Exception 时 返回 None)"""

    @staticmethod
    async def exception(_) -> None:
        return

Safe 🔗

Bases: SendMessageAction

安全发送的 SendMessage action

行为: 在第一次尝试失败后先移除 quote, 之后每次失败时按顺序替换元素为其 str 形式: AtAll, At, Poke, Forward, MultimediaElement

仅会对以下错误进行重试:

  • UnknownTarget (指向对象不存在,可能由移除成员等行为引发竞争)
  • RemoteException (网络问题)
  • UnknownError (其他由 RPC 引发的未知错误)
Source code in src/graia/ariadne/util/send.py
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
class Safe(SendMessageAction):
    """
    安全发送的 SendMessage action

    行为:
    在第一次尝试失败后先移除 quote,
    之后每次失败时按顺序替换元素为其 str 形式: AtAll, At, Poke, Forward, MultimediaElement

    仅会对以下错误进行重试:

    - `UnknownTarget` (指向对象不存在,可能由移除成员等行为引发竞争)
    - `RemoteException` (网络问题)
    - `UnknownError` (其他由 RPC 引发的未知错误)
    """

    def __init__(self, ignore: bool = False) -> None:
        self.ignore: bool = ignore

    @overload
    @staticmethod
    async def exception(item) -> ActiveMessage:
        ...

    @overload
    async def exception(self, item) -> ActiveMessage:
        ...

    @staticmethod
    async def _handle(item: SendMessageException, ignore: bool):
        from ..message.chain import MessageChain
        from ..message.element import At, AtAll, Forward, MultimediaElement, Plain, Poke

        chain: MessageChain = item.send_data["message"]
        ariadne = Ariadne.current()

        def convert(msg_chain: MessageChain, type) -> None:
            for ind, elem in enumerate(msg_chain.__root__[:]):
                if isinstance(elem, type):
                    msg_chain.__root__[ind] = Plain(elem.display)

        for type in [AtAll, At, Poke, Forward, MultimediaElement]:
            convert(chain, type)
            try:
                val = await ariadne.send_message(**item.send_data, action=Strict)  # type: ignore
            except (UnknownTarget, RemoteException, UnknownError):  # 这些错误可以继续重试
                continue
            except SendMessageException:  # 其他错误
                break
            return val

        if not ignore:
            raise item

    @overload
    @staticmethod
    async def exception(s, i):  # sourcery skip # pyright: ignore
        ...

    @overload
    async def exception(s, i):  # sourcery skip # pyright: ignore
        ...

    async def exception(s: Union["Safe", Exc_T], i: Optional[Exc_T] = None):  # type: ignore
        # sourcery skip: instance-method-first-arg-name
        if not isinstance(s, Safe):
            return await Safe._handle(s, True)
        if i:
            return await Safe._handle(i, s.ignore)

Strict 🔗

Bases: SendMessageAction

严格的 SendMessage action (有错误时 raise)

Source code in src/graia/ariadne/util/send.py
26
27
28
29
class Strict(SendMessageAction):
    """严格的 SendMessage action (有错误时 raise)"""

    ...