1 | # Copyright Pincer 2021-Present |
||
2 | # Full MIT License can be found in `LICENSE` at the project root. |
||
3 | |||
4 | """ |
||
5 | non-subscription event sent immediately after connecting, |
||
6 | contains server information |
||
7 | """ |
||
8 | from __future__ import annotations |
||
9 | |||
10 | from typing import TYPE_CHECKING |
||
11 | |||
12 | from ..commands import ChatCommandHandler |
||
13 | from ..exceptions import InvalidPayload |
||
14 | from ..objects.user.user import User |
||
15 | |||
16 | if TYPE_CHECKING: |
||
17 | from typing import Tuple |
||
18 | from ..utils.types import Coro |
||
19 | from ..client import Client |
||
20 | from ..core.gateway import Gateway |
||
21 | from ..core.gateway import GatewayDispatch |
||
22 | |||
23 | |||
24 | async def on_ready_middleware( |
||
25 | self: Client, gateway: Gateway, payload: GatewayDispatch |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
26 | ) -> Tuple[str]: |
||
27 | """|coro| |
||
28 | |||
29 | Middleware for the ``on_ready`` event. |
||
30 | |||
31 | Parameters |
||
32 | ---------- |
||
33 | payload : :class:`~pincer.core.gateway.GatewayDispatch` |
||
34 | The data received from the stage instance create event |
||
35 | gateway : :class:`~pincer.core.gateway.Gateway` |
||
36 | The gateway for the current shard. |
||
37 | |||
38 | Returns |
||
39 | ------- |
||
40 | Tuple[:class:`str`] |
||
41 | ``on_ready`` |
||
42 | """ |
||
43 | |||
44 | gateway.set_session_id(payload.data.get("session_id")) |
||
45 | |||
46 | user = payload.data.get("user") |
||
47 | guilds = payload.data.get("guilds") |
||
48 | |||
49 | if not user or guilds is None: |
||
50 | raise InvalidPayload( |
||
51 | "A `user` and `guilds` key/value pair is expected on the " |
||
52 | "`ready` payload event." |
||
53 | ) |
||
54 | |||
55 | self.bot = User.from_dict(user) |
||
56 | self.guilds = dict(map(lambda i: (i["id"], None), guilds)) |
||
57 | |||
58 | await ChatCommandHandler(self).initialize() |
||
59 | return ("on_ready",) |
||
60 | |||
61 | |||
62 | def export() -> Coro: |
||
0 ignored issues
–
show
|
|||
63 | return on_ready_middleware |
||
64 |