From 3da683ebde87be043423b36e0aaeaf7fe545a714 Mon Sep 17 00:00:00 2001 From: grey-cat-1908 Date: Fri, 18 Mar 2022 12:13:46 +0300 Subject: [PATCH] create_dm_channel method --- melisa/client.py | 4 +++- melisa/models/user/user.py | 7 +++++++ melisa/utils/api_model.py | 6 +++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/melisa/client.py b/melisa/client.py index a29e2d4..1fba0b6 100644 --- a/melisa/client.py +++ b/melisa/client.py @@ -1,6 +1,6 @@ from .models import User from .models.app import Shard -from .utils import Snowflake +from .utils import Snowflake, APIModelBase from .utils.types import Coro from .core.http import HTTPClient @@ -55,6 +55,8 @@ class Client: self._activity = activity self._status = status + APIModelBase.set_client(self) + async def _get_gateway(self): """Get Gateway information""" return GatewayBotInfo.from_dict(await self.http.get("gateway/bot")) diff --git a/melisa/models/user/user.py b/melisa/models/user/user.py index c72795b..b0db4d6 100644 --- a/melisa/models/user/user.py +++ b/melisa/models/user/user.py @@ -194,3 +194,10 @@ class User(APIModelBase): def avatar_url(self) -> str: """Avatar url (from the Discord CDN server)""" return "https://cdn.discordapp.com/avatars/{}/{}.png?size=1024".format(self.id, self.avatar) + + async def create_dm_channel(self): + # ToDo: Add docstrings + # ToDo: Add checking this channel in cache + return await self._http.post( + "/users/@me/channels", data={"recipient_id": self.id} + ) diff --git a/melisa/utils/api_model.py b/melisa/utils/api_model.py index 7a72785..e3074b5 100644 --- a/melisa/utils/api_model.py +++ b/melisa/utils/api_model.py @@ -54,10 +54,14 @@ class APIModelBase: @property def _http(self): if not self._client: - raise AttributeError("Object is not yet linked to a main client") + return None return self._client.http + @classmethod + def set_client(cls, client): + cls._client = client + @classmethod def from_dict( cls: Generic[T], data: Dict[str, Union[str, bool, int, Any]]