|
1
|
|
|
# Copyright Pincer 2021-Present |
|
|
|
|
|
|
2
|
|
|
# Full MIT License can be found in `LICENSE` at the project root. |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
from __future__ import annotations |
|
6
|
|
|
|
|
7
|
|
|
from json import dumps, loads |
|
8
|
|
|
from typing import TYPE_CHECKING |
|
9
|
|
|
|
|
10
|
|
|
if TYPE_CHECKING: |
|
11
|
|
|
from typing import Any, Dict, Optional, Union |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
class GatewayDispatch: |
|
15
|
|
|
"""Represents a websocket message. |
|
16
|
|
|
|
|
17
|
|
|
Attributes |
|
18
|
|
|
---------- |
|
19
|
|
|
op: :class:`int` |
|
20
|
|
|
The discord opcode which represents what the message means. |
|
21
|
|
|
data: Optional[Union[:class:`int`, Dict[:class:`str`, Any]]] |
|
22
|
|
|
The event data that has been sent/received. |
|
23
|
|
|
seq: Optional[:class:`int`] |
|
24
|
|
|
The sequence number of a message, which can be used |
|
25
|
|
|
for resuming sessions and heartbeats. |
|
26
|
|
|
event_name: Optional[:class:`str`] |
|
27
|
|
|
The event name for the payload. |
|
28
|
|
|
""" |
|
29
|
|
|
|
|
30
|
|
|
def __init__( |
|
31
|
|
|
self, |
|
|
|
|
|
|
32
|
|
|
op: int, |
|
|
|
|
|
|
33
|
|
|
data: Optional[Union[int, bool, Dict[str, Any]]] = None, |
|
|
|
|
|
|
34
|
|
|
seq: Optional[int] = None, |
|
|
|
|
|
|
35
|
|
|
name: Optional[str] = None, |
|
|
|
|
|
|
36
|
|
|
): |
|
37
|
|
|
self.op: int = op |
|
38
|
|
|
self.data: Optional[Union[int, bool, Dict[str, Any]]] = data |
|
39
|
|
|
self.seq: Optional[int] = seq |
|
40
|
|
|
self.event_name: Optional[str] = name |
|
41
|
|
|
|
|
42
|
|
|
def __str__(self) -> str: |
|
43
|
|
|
return dumps( |
|
44
|
|
|
dict(op=self.op, d=self.data, s=self.seq, t=self.event_name) |
|
45
|
|
|
) |
|
46
|
|
|
|
|
47
|
|
|
@classmethod |
|
48
|
|
|
def from_string(cls, payload: str) -> GatewayDispatch: |
|
49
|
|
|
"""Parses a given payload from a string format |
|
50
|
|
|
and returns a GatewayDispatch. |
|
51
|
|
|
|
|
52
|
|
|
Parameters |
|
53
|
|
|
---------- |
|
54
|
|
|
payload : :class:`str` |
|
55
|
|
|
The payload to parse. |
|
56
|
|
|
|
|
57
|
|
|
Returns |
|
58
|
|
|
------- |
|
59
|
|
|
:class:`~pincer.core.dispatch.GatewayDispatch` |
|
60
|
|
|
The new class. |
|
61
|
|
|
""" |
|
62
|
|
|
payload: Dict[str, Union[int, str, Dict[str, Any]]] = loads(payload) |
|
63
|
|
|
return cls( |
|
64
|
|
|
payload.get("op"), |
|
65
|
|
|
payload.get("d"), |
|
66
|
|
|
payload.get("s"), |
|
67
|
|
|
payload.get("t"), |
|
68
|
|
|
) |
|
69
|
|
|
|