示例#1
0
# TODO: This is post-v4.
# TODO: Reorganise these models based on which big obj uses little obj
# TODO: Potentially rename some model references to enums, if applicable
# TODO: Reorganise mixins to its own thing, currently placed here because circular import sucks.
# also, it should be serialiser* but idk, fl0w'd say something if I left it like that. /shrug
import datetime
from logging import Logger
from math import floor
from typing import Union

from interactions.base import get_logger

log: Logger = get_logger("mixin")


class DictSerializerMixin(object):
    """
    The purpose of this mixin is to be subclassed.

    .. note::
        On subclass, it:
            -- From kwargs (received from the Discord API response), add it to the `_json` attribute
            such that it can be reused by other libraries/extensions
            -- Aids in attributing the kwargs to actual model attributes, i.e. `User.id`
            -- Dynamically sets attributes not given to kwargs but slotted to None, signifying that it doesn't exist.

    .. warning::
        This does NOT convert them to its own data types, i.e. timestamps, or User within Member. This is left by
        the object that's using the mixin.
    """
from typing import Any, List, Optional, Union

from orjson import dumps as ordumps
from orjson import loads

from interactions.api.models.gw import Presence
from interactions.base import get_logger
from interactions.enums import InteractionType, OptionType

from .dispatch import Listener
from .enums import OpCodeType
from .error import GatewayException
from .http import HTTPClient
from .models.flags import Intents

log: Logger = get_logger("gateway")

__all__ = ("Heartbeat", "WebSocket")


class Heartbeat(Thread):
    """
    A class representing a consistent heartbeat connection with the gateway.

    :ivar WebSocket ws: The WebSocket class to infer on.
    :ivar Union[int, float] interval: The heartbeat interval determined by the gateway.
    :ivar Event event: The multi-threading event.
    """

    __slots__ = ("ws", "interval", "event")
from asyncio import get_event_loop
from logging import Logger
from typing import Coroutine, Optional

from interactions.base import get_logger

log: Logger = get_logger("dispatch")


class Listener:
    """
    A class representing how events become dispatched and listened to.

    :ivar AbstractEventLoop loop: The coroutine event loop established on.
    :ivar dict events: A list of events being dispatched.
    """

    __slots__ = ("loop", "events")

    def __init__(self) -> None:
        self.loop = get_event_loop()
        self.events = {}

    def dispatch(self, name: str, *args, **kwargs) -> None:
        r"""
        Dispatches an event given out by the gateway.

        :param name: The name of the event to dispatch.
        :type name: str
        :param \*args: Multiple arguments of the coroutine.
        :type \*args: list[Any]