|
1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
|
|
2
|
|
|
# MIT License |
|
3
|
|
|
# |
|
4
|
|
|
# Copyright (c) 2021 Pyscord |
|
5
|
|
|
# |
|
6
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
# of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
# in the Software without restriction, including without limitation the rights |
|
9
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
# copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
# furnished to do so, subject to the following conditions: |
|
12
|
|
|
# |
|
13
|
|
|
# The above copyright notice and this permission notice shall be included in all |
|
14
|
|
|
# copies or substantial portions of the Software. |
|
15
|
|
|
# |
|
16
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
22
|
|
|
# SOFTWARE. |
|
23
|
|
|
|
|
24
|
|
|
from __future__ import annotations |
|
25
|
|
|
|
|
26
|
|
|
from json import dumps, loads |
|
27
|
|
|
from typing import Optional, Union, Dict, Any |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
class GatewayDispatch: |
|
31
|
|
|
""" |
|
32
|
|
|
Represents a websocket message. |
|
33
|
|
|
""" |
|
34
|
|
|
|
|
35
|
|
|
def __init__(self, op: int, data: Optional[Union[int, Dict[str, Any]]], |
|
36
|
|
|
seq: Optional[int] = None, name: Optional[str] = None): |
|
37
|
|
|
""" |
|
38
|
|
|
Instantiate a new GatewayDispatch object. |
|
39
|
|
|
|
|
40
|
|
|
:param op: The discord opcode which represents what the message |
|
41
|
|
|
means. |
|
42
|
|
|
:param data: The event data that has been sent/received. |
|
43
|
|
|
:param seq: The sequence number of a message, which can be used |
|
44
|
|
|
for resuming sessions and heartbeats. |
|
45
|
|
|
:param name: The event name for the payload. |
|
46
|
|
|
""" |
|
47
|
|
|
self.op: int = op |
|
|
|
|
|
|
48
|
|
|
self.data: Optional[Union[int, Dict[str, Any]]] = data |
|
49
|
|
|
self.seq: Optional[int] = seq |
|
50
|
|
|
self.event_name: Optional[str] = name |
|
51
|
|
|
|
|
52
|
|
|
def __str__(self) -> str: |
|
53
|
|
|
""" |
|
54
|
|
|
:return The string representation of the GatewayDispatch object. |
|
55
|
|
|
This object can be used to send a websocket message to the gateway. |
|
56
|
|
|
""" |
|
57
|
|
|
return dumps(dict( |
|
58
|
|
|
op=self.op, |
|
59
|
|
|
d=self.data, |
|
60
|
|
|
s=self.seq, |
|
61
|
|
|
t=self.event_name |
|
62
|
|
|
)) |
|
63
|
|
|
|
|
64
|
|
|
@classmethod |
|
65
|
|
|
def from_string(cls, payload: str) -> GatewayDispatch: |
|
66
|
|
|
""" |
|
67
|
|
|
Parses a given payload from a string format and returns a |
|
68
|
|
|
GatewayDispatch. |
|
69
|
|
|
|
|
70
|
|
|
:param payload: The payload to parse. |
|
71
|
|
|
:return: A proper GatewayDispatch object. |
|
72
|
|
|
""" |
|
73
|
|
|
payload: Dict[str, Union[int, str, Dict[str, Any]]] = loads(payload) |
|
74
|
|
|
return cls( |
|
75
|
|
|
payload.get("op"), |
|
76
|
|
|
payload.get("d"), |
|
77
|
|
|
payload.get("s"), |
|
78
|
|
|
payload.get("t") |
|
79
|
|
|
) |
|
80
|
|
|
|