1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
2
|
|
|
# MIT License |
3
|
|
|
# |
4
|
|
|
# Copyright (c) 2021 Pincer |
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
|
|
|
"""Represents a websocket message.""" |
32
|
|
|
|
33
|
|
|
def __init__(self, op: int, data: Optional[Union[int, Dict[str, Any]]], |
34
|
|
|
seq: Optional[int] = None, name: Optional[str] = None): |
35
|
|
|
""" |
36
|
|
|
Instantiate a new GatewayDispatch object. |
37
|
|
|
|
38
|
|
|
:param op: The discord opcode which represents what the message |
39
|
|
|
means. |
40
|
|
|
:param data: The event data that has been sent/received. |
41
|
|
|
:param seq: The sequence number of a message, which can be used |
42
|
|
|
for resuming sessions and heartbeats. |
43
|
|
|
:param name: The event name for the payload. |
44
|
|
|
""" |
45
|
|
|
self.op: int = op |
|
|
|
|
46
|
|
|
self.data: Optional[Union[int, Dict[str, Any]]] = data |
47
|
|
|
self.seq: Optional[int] = seq |
48
|
|
|
self.event_name: Optional[str] = name |
49
|
|
|
|
50
|
|
|
def __str__(self) -> str: |
51
|
|
|
""" |
52
|
|
|
:return The string representation of the GatewayDispatch object. |
53
|
|
|
This object can be used to send a websocket message to the gateway. |
54
|
|
|
""" |
55
|
|
|
return dumps(dict( |
56
|
|
|
op=self.op, |
57
|
|
|
d=self.data, |
58
|
|
|
s=self.seq, |
59
|
|
|
t=self.event_name |
60
|
|
|
)) |
61
|
|
|
|
62
|
|
|
@classmethod |
63
|
|
|
def from_string(cls, payload: str) -> GatewayDispatch: |
64
|
|
|
""" |
65
|
|
|
Parses a given payload from a string format and returns a |
66
|
|
|
GatewayDispatch. |
67
|
|
|
|
68
|
|
|
:param payload: The payload to parse. |
69
|
|
|
:return: A proper GatewayDispatch object. |
70
|
|
|
""" |
71
|
|
|
payload: Dict[str, Union[int, str, Dict[str, Any]]] = loads(payload) |
72
|
|
|
return cls( |
73
|
|
|
payload.get("op"), |
74
|
|
|
payload.get("d"), |
75
|
|
|
payload.get("s"), |
76
|
|
|
payload.get("t") |
77
|
|
|
) |
78
|
|
|
|