mirror of
https://github.com/MelisaDev/melisa.git
synced 2024-09-22 19:22:01 +03:00
to_dict method to the APIModelBase
This commit is contained in:
parent
a5ebb9a972
commit
59d5b51b0c
6 changed files with 52 additions and 33 deletions
|
@ -12,11 +12,11 @@ import aiohttp
|
|||
from ..exceptions import GatewayError, PrivilegedIntentsRequired, LoginFailure
|
||||
from ..listeners import listeners
|
||||
from ..models.user import BotActivity
|
||||
from ..utils import APIObjectBase
|
||||
from ..utils import APIModelBase
|
||||
|
||||
|
||||
@dataclass
|
||||
class GatewayBotInfo(APIObjectBase):
|
||||
class GatewayBotInfo(APIModelBase):
|
||||
"""Gateway info from the `gateway/bot` endpoint"""
|
||||
url: str
|
||||
shards: int
|
||||
|
@ -210,15 +210,7 @@ class Gateway:
|
|||
}
|
||||
|
||||
if activity is not None:
|
||||
activity_to_set = {
|
||||
"name": activity.name,
|
||||
"type": int(activity.type)
|
||||
}
|
||||
|
||||
if int(activity.type) == 1 and activity.url:
|
||||
activity_to_set["url"] = activity.url
|
||||
|
||||
data["activities"] = [activity_to_set]
|
||||
data["activities"] = activity.to_dict()
|
||||
|
||||
if status is not None:
|
||||
data["status"] = str(status)
|
||||
|
|
|
@ -73,13 +73,6 @@ class Shard:
|
|||
|
||||
return self
|
||||
|
||||
async def disconnect(self) -> None:
|
||||
"""
|
||||
|coro|
|
||||
|
||||
Disconnect current shard"""
|
||||
await self._try_close()
|
||||
|
||||
async def reconnect(self, wait_time: int = 3) -> None:
|
||||
"""|coro|
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ from enum import IntEnum, Enum, Flag
|
|||
from typing import Optional, Tuple, List, Literal
|
||||
|
||||
from ...utils import Snowflake
|
||||
from ...utils import APIObjectBase
|
||||
from ...utils import APIModelBase
|
||||
from ...utils.types import APINullable
|
||||
|
||||
|
||||
|
@ -46,7 +46,7 @@ class ActivityType(IntEnum):
|
|||
|
||||
|
||||
@dataclass(repr=False)
|
||||
class ActivityTimestamp(BasePresence, APIObjectBase):
|
||||
class ActivityTimestamp(BasePresence, APIModelBase):
|
||||
"""Represents the timestamp of an activity.
|
||||
|
||||
Attributes
|
||||
|
@ -61,7 +61,7 @@ class ActivityTimestamp(BasePresence, APIObjectBase):
|
|||
|
||||
|
||||
@dataclass(repr=False)
|
||||
class ActivityEmoji(BasePresence, APIObjectBase):
|
||||
class ActivityEmoji(BasePresence, APIModelBase):
|
||||
"""Represents an emoji in an activity.
|
||||
|
||||
Attributes
|
||||
|
@ -79,7 +79,7 @@ class ActivityEmoji(BasePresence, APIObjectBase):
|
|||
|
||||
|
||||
@dataclass(repr=False)
|
||||
class ActivityParty(BasePresence, APIObjectBase):
|
||||
class ActivityParty(BasePresence, APIModelBase):
|
||||
"""Represents a party in an activity.
|
||||
|
||||
Attributes
|
||||
|
@ -94,7 +94,7 @@ class ActivityParty(BasePresence, APIObjectBase):
|
|||
|
||||
|
||||
@dataclass(repr=False)
|
||||
class ActivityAssets(BasePresence, APIObjectBase):
|
||||
class ActivityAssets(BasePresence, APIModelBase):
|
||||
"""Represents an asset of an activity.
|
||||
|
||||
Attributes
|
||||
|
@ -115,7 +115,7 @@ class ActivityAssets(BasePresence, APIObjectBase):
|
|||
|
||||
|
||||
@dataclass(repr=False)
|
||||
class ActivitySecrets(BasePresence, APIObjectBase):
|
||||
class ActivitySecrets(BasePresence, APIModelBase):
|
||||
"""Represents a secret of an activity.
|
||||
|
||||
Attributes
|
||||
|
@ -132,7 +132,7 @@ class ActivitySecrets(BasePresence, APIObjectBase):
|
|||
match_: APINullable[str] = None
|
||||
|
||||
|
||||
class ActivityFlags(BasePresence, APIObjectBase):
|
||||
class ActivityFlags(BasePresence, APIModelBase):
|
||||
"""
|
||||
Just Activity Flags (From Discord API).
|
||||
|
||||
|
@ -152,7 +152,7 @@ class ActivityFlags(BasePresence, APIObjectBase):
|
|||
|
||||
|
||||
@dataclass(repr=False)
|
||||
class ActivityButton(BasePresence, APIObjectBase):
|
||||
class ActivityButton(BasePresence, APIModelBase):
|
||||
"""When received over the gateway, the buttons field is an array of strings, which are the button labels. Bots
|
||||
cannot access a user's activity button URLs. When sending, the buttons field must be an array of the below
|
||||
object:
|
||||
|
@ -168,7 +168,7 @@ class ActivityButton(BasePresence, APIObjectBase):
|
|||
|
||||
|
||||
@dataclass(repr=False)
|
||||
class Activity(BasePresence, APIObjectBase):
|
||||
class Activity(BasePresence, APIModelBase):
|
||||
"""Bots are only able to send ``name``, ``type``, and optionally ``url``.
|
||||
|
||||
Attributes
|
||||
|
@ -224,7 +224,7 @@ class Activity(BasePresence, APIObjectBase):
|
|||
|
||||
|
||||
@dataclass(repr=False)
|
||||
class BotActivity(BasePresence, APIObjectBase):
|
||||
class BotActivity(BasePresence, APIModelBase):
|
||||
"""
|
||||
|
||||
Attributes
|
||||
|
|
|
@ -4,7 +4,7 @@ from enum import IntEnum
|
|||
from dataclasses import dataclass
|
||||
from typing import Optional
|
||||
|
||||
from ...utils.api_object import APIObjectBase
|
||||
from ...utils.api_model import APIModelBase
|
||||
from ...utils.types import APINullable
|
||||
from ...utils.snowflake import Snowflake
|
||||
|
||||
|
@ -106,7 +106,7 @@ class VisibilityTypes(IntEnum):
|
|||
|
||||
|
||||
@dataclass(repr=False)
|
||||
class User(APIObjectBase):
|
||||
class User(APIModelBase):
|
||||
"""User Structure
|
||||
|
||||
Attributes
|
||||
|
|
|
@ -5,10 +5,10 @@ from .types import (
|
|||
from .snowflake import Snowflake
|
||||
|
||||
|
||||
from .api_object import APIObjectBase
|
||||
from .api_model import APIModelBase
|
||||
|
||||
__all__ = (
|
||||
"Coro",
|
||||
"Snowflake",
|
||||
"APIObjectBase"
|
||||
"APIModelBase"
|
||||
)
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
from dataclasses import _is_dataclass_instance, fields
|
||||
from enum import Enum
|
||||
from inspect import getfullargspec
|
||||
from typing import (
|
||||
|
@ -13,7 +15,36 @@ from typing import (
|
|||
T = TypeVar("T")
|
||||
|
||||
|
||||
class APIObjectBase:
|
||||
def _to_dict_without_none(model):
|
||||
if _is_dataclass_instance(model):
|
||||
result = []
|
||||
|
||||
for field in fields(model):
|
||||
value = _to_dict_without_none(getattr(model, field.name))
|
||||
|
||||
if isinstance(value, Enum):
|
||||
result.append((field.name, value.value))
|
||||
elif value is not None and not field.name.startswith("_"):
|
||||
result.append((field.name, value))
|
||||
|
||||
return dict(result)
|
||||
|
||||
elif isinstance(model, tuple) and hasattr(model, "_fields"):
|
||||
return type(model)(*[_to_dict_without_none(v) for v in model])
|
||||
|
||||
elif isinstance(model, (list, tuple)):
|
||||
return type(model)(_to_dict_without_none(v) for v in model)
|
||||
|
||||
elif isinstance(model, dict):
|
||||
return type(model)(
|
||||
(_to_dict_without_none(k), _to_dict_without_none(v))
|
||||
for k, v in model.items()
|
||||
)
|
||||
else:
|
||||
return copy.deepcopy(model)
|
||||
|
||||
|
||||
class APIModelBase:
|
||||
"""
|
||||
Represents an object which has been fetched from the Discord API.
|
||||
"""
|
||||
|
@ -55,3 +86,6 @@ class APIObjectBase:
|
|||
)
|
||||
)
|
||||
)
|
||||
|
||||
def to_dict(self) -> Dict:
|
||||
return _to_dict_without_none(self)
|
Loading…
Reference in a new issue