Passed
Push — main ( 3c43d8...60b649 )
by
unknown
02:22
created

pyscord.core.dispatch   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 25
dl 0
loc 79
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A GatewayDispatch.__init__() 0 16 1
A GatewayDispatch.from_string() 0 15 1
A GatewayDispatch.__str__() 0 10 1
1
# -*- coding: utf-8 -*-
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
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
1 ignored issue
show
Coding Style Naming introduced by
Attribute name "op" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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