Skip to content

Commit 1de54e9

Browse files
ItsRqtlRyan
andauthored
feat: Implement send method for User class. (#1109)
Co-authored-by: Ryan <[email protected]>
1 parent c41832b commit 1de54e9

File tree

1 file changed

+96
-2
lines changed

1 file changed

+96
-2
lines changed

interactions/api/models/user.py

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
from typing import TYPE_CHECKING, Optional, Union
1+
from typing import TYPE_CHECKING, List, Optional, Union
22

33
from ...utils.attrs_utils import ClientSerializerMixin, define, field
4+
from ...utils.missing import MISSING
5+
from ..error import LibraryException
46
from .flags import UserFlags
5-
from .misc import IDMixin, Snowflake
7+
from .misc import AllowedMentions, File, IDMixin, Snowflake
68

79
if TYPE_CHECKING:
10+
from ...client.models.component import ActionRow, Button, SelectMenu
811
from .gw import Presence
12+
from .message import Attachment, Embed, Message
913

1014
__all__ = ("User",)
1115

@@ -111,3 +115,93 @@ def presence(self) -> Optional["Presence"]:
111115
from .gw import Presence
112116

113117
return self._client.cache[Presence].get(self.id)
118+
119+
async def send(
120+
self,
121+
content: Optional[str] = MISSING,
122+
*,
123+
components: Optional[
124+
Union[
125+
"ActionRow",
126+
"Button",
127+
"SelectMenu",
128+
List["ActionRow"],
129+
List["Button"],
130+
List["SelectMenu"],
131+
]
132+
] = MISSING,
133+
tts: Optional[bool] = MISSING,
134+
attachments: Optional[List["Attachment"]] = MISSING,
135+
files: Optional[Union[File, List[File]]] = MISSING,
136+
embeds: Optional[Union["Embed", List["Embed"]]] = MISSING,
137+
allowed_mentions: Optional[Union[AllowedMentions, dict]] = MISSING,
138+
) -> "Message":
139+
"""
140+
Sends a DM to the user.
141+
142+
:param content?: The contents of the message as a string or string-converted value.
143+
:type content?: Optional[str]
144+
:param components?: A component, or list of components for the message.
145+
:type components?: Optional[Union[ActionRow, Button, SelectMenu, List[ActionRow], List[Button], List[SelectMenu]]]
146+
:param tts?: Whether the message utilizes the text-to-speech Discord programme or not.
147+
:type tts?: Optional[bool]
148+
:param attachments?: The attachments to attach to the message. Needs to be uploaded to the CDN first
149+
:type attachments?: Optional[List[Attachment]]
150+
:param files?: A file or list of files to be attached to the message.
151+
:type files?: Optional[Union[File, List[File]]]
152+
:param embeds?: An embed, or list of embeds for the message.
153+
:type embeds?: Optional[Union[Embed, List[Embed]]]
154+
:param allowed_mentions?: The allowed mentions for the message.
155+
:type allowed_mentions?: Optional[Union[AllowedMentions, dict]]
156+
:return: The sent message as an object.
157+
:rtype: Message
158+
"""
159+
if not self._client:
160+
raise LibraryException(code=13)
161+
from ...client.models.component import _build_components
162+
from .message import Message
163+
164+
_content: str = "" if content is MISSING else content
165+
_tts: bool = False if tts is MISSING else tts
166+
_attachments = [] if attachments is MISSING else [a._json for a in attachments]
167+
_embeds: list = (
168+
[]
169+
if not embeds or embeds is MISSING
170+
else ([embed._json for embed in embeds] if isinstance(embeds, list) else [embeds._json])
171+
)
172+
_allowed_mentions: dict = (
173+
{}
174+
if allowed_mentions is MISSING
175+
else allowed_mentions._json
176+
if isinstance(allowed_mentions, AllowedMentions)
177+
else allowed_mentions
178+
)
179+
if not components or components is MISSING:
180+
_components = []
181+
else:
182+
_components = _build_components(components=components)
183+
184+
if not files or files is MISSING:
185+
_files = []
186+
elif isinstance(files, list):
187+
_files = [file._json_payload(id) for id, file in enumerate(files)]
188+
else:
189+
_files = [files._json_payload(0)]
190+
files = [files]
191+
192+
_files.extend(_attachments)
193+
194+
payload = dict(
195+
content=_content,
196+
tts=_tts,
197+
attachments=_files,
198+
embeds=_embeds,
199+
components=_components,
200+
allowed_mentions=_allowed_mentions,
201+
)
202+
channel = await self._client.create_dm(recipient_id=int(self.id))
203+
res = await self._client.create_message(
204+
channel_id=int(channel["id"]), payload=payload, files=files
205+
)
206+
207+
return Message(**res, _client=self._client)

0 commit comments

Comments
 (0)