Skip to content

Commit 8b4546f

Browse files
feat: Implement AllowedMentions object (#1002)
* feat!: Implement ``AllowedMentions`` object * replace ``MessageInteraction``(what it do here lol) to ``AllowedMentions`` * ref: add to ``__all__`` * ci: pre-commit * typehints * Allow pass dict * ci: correct from checks. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent f5f01c8 commit 8b4546f

File tree

8 files changed

+129
-46
lines changed

8 files changed

+129
-46
lines changed

interactions/api/http/message.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
from ...api.cache import Cache
66
from ..models.attrs_utils import MISSING
7-
from ..models.message import Embed, Message, MessageInteraction, Sticker
8-
from ..models.misc import File, Snowflake
7+
from ..models.message import Embed, Message, Sticker
8+
from ..models.misc import AllowedMentions, File, Snowflake
99
from .request import _Request
1010
from .route import Route
1111

@@ -26,7 +26,7 @@ async def send_message(
2626
tts: bool = False,
2727
embeds: Optional[List[Embed]] = None,
2828
nonce: Union[int, str] = None,
29-
allowed_mentions: Optional[MessageInteraction] = None, # don't know type
29+
allowed_mentions: Optional[Union[AllowedMentions, dict]] = None,
3030
message_reference: Optional[Message] = None,
3131
stickers: Optional[List[Sticker]] = None,
3232
) -> dict:
@@ -49,7 +49,11 @@ async def send_message(
4949
payload["nonce"] = nonce
5050

5151
if allowed_mentions:
52-
payload["allowed_mentions"] = allowed_mentions
52+
payload["allowed_mentions"] = (
53+
allowed_mentions._json
54+
if isinstance(allowed_mentions, AllowedMentions)
55+
else allowed_mentions
56+
)
5357

5458
if message_reference:
5559
payload["message_reference"] = message_reference

interactions/api/models/channel.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
field,
1313
)
1414
from .flags import Permissions
15-
from .misc import File, IDMixin, Overwrite, Snowflake
15+
from .misc import AllowedMentions, File, IDMixin, Overwrite, Snowflake
1616
from .user import User
1717
from .webhook import Webhook
1818

1919
if TYPE_CHECKING:
2020
from ...client.models.component import ActionRow, Button, SelectMenu
2121
from .guild import Invite, InviteTargetType
2222
from .member import Member
23-
from .message import Attachment, Embed, Message, MessageInteraction, Sticker
23+
from .message import Attachment, Embed, Message, Sticker
2424

2525
__all__ = (
2626
"ChannelType",
@@ -198,7 +198,7 @@ async def send(
198198
attachments: Optional[List["Attachment"]] = MISSING,
199199
files: Optional[Union[File, List[File]]] = MISSING,
200200
embeds: Optional[Union["Embed", List["Embed"]]] = MISSING,
201-
allowed_mentions: Optional["MessageInteraction"] = MISSING,
201+
allowed_mentions: Optional[Union[AllowedMentions, dict]] = MISSING,
202202
stickers: Optional[List["Sticker"]] = MISSING,
203203
components: Optional[
204204
Union[
@@ -224,8 +224,8 @@ async def send(
224224
:type attachments?: Optional[List[Attachment]]
225225
:param embeds?: An embed, or list of embeds for the message.
226226
:type embeds?: Optional[Union[Embed, List[Embed]]]
227-
:param allowed_mentions?: The message interactions/mention limits that the message can refer to.
228-
:type allowed_mentions?: Optional[MessageInteraction]
227+
:param allowed_mentions?: The allowed mentions for the message.
228+
:type allowed_mentions?: Optional[Union[AllowedMentions, dict]]
229229
:param stickers?: A list of stickers to send with your message. You can send up to 3 stickers per message.
230230
:type stickers?: Optional[List[Sticker]]
231231
:param components?: A component, or list of components for the message.
@@ -241,7 +241,13 @@ async def send(
241241
_content: str = "" if content is MISSING else content
242242
_tts: bool = False if tts is MISSING else tts
243243
_attachments = [] if attachments is MISSING else [a._json for a in attachments]
244-
_allowed_mentions: dict = {} if allowed_mentions is MISSING else allowed_mentions
244+
_allowed_mentions: dict = (
245+
{}
246+
if allowed_mentions is MISSING
247+
else allowed_mentions._json
248+
if isinstance(allowed_mentions, AllowedMentions)
249+
else allowed_mentions
250+
)
245251
_sticker_ids: list = (
246252
[] if stickers is MISSING else [str(sticker.id) for sticker in stickers]
247253
)

interactions/api/models/gw.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
from .emoji import Emoji
1616
from .guild import EventMetadata
1717
from .member import Member
18-
from .message import Embed, Message, MessageInteraction, Sticker
18+
from .message import Embed, Message, Sticker
1919
from .misc import (
20+
AllowedMentions,
2021
AutoModAction,
2122
AutoModTriggerMetadata,
2223
AutoModTriggerType,
@@ -411,7 +412,7 @@ async def send(
411412
tts: Optional[bool] = MISSING,
412413
files: Optional[Union[File, List[File]]] = MISSING,
413414
embeds: Optional[Union[Embed, List[Embed]]] = MISSING,
414-
allowed_mentions: Optional[MessageInteraction] = MISSING,
415+
allowed_mentions: Optional[Union[AllowedMentions, dict]] = MISSING,
415416
) -> Message:
416417
"""
417418
Sends a DM to the member.
@@ -426,8 +427,8 @@ async def send(
426427
:type files?: Optional[Union[File, List[File]]]
427428
:param embeds?: An embed, or list of embeds for the message.
428429
:type embeds?: Optional[Union[Embed, List[Embed]]]
429-
:param allowed_mentions?: The message interactions/mention limits that the message can refer to.
430-
:type allowed_mentions?: Optional[MessageInteraction]
430+
:param allowed_mentions?: The allowed mentions for the message.
431+
:type allowed_mentions?: Optional[Union[AllowedMentions, dict]]
431432
:return: The sent message as an object.
432433
:rtype: Message
433434
"""
@@ -443,7 +444,13 @@ async def send(
443444
if not embeds or embeds is MISSING
444445
else ([embed._json for embed in embeds] if isinstance(embeds, list) else [embeds._json])
445446
)
446-
_allowed_mentions: dict = {} if allowed_mentions is MISSING else allowed_mentions
447+
_allowed_mentions: dict = (
448+
{}
449+
if allowed_mentions is MISSING
450+
else allowed_mentions._json
451+
if isinstance(allowed_mentions, AllowedMentions)
452+
else allowed_mentions
453+
)
447454
if not components or components is MISSING:
448455
_components = []
449456
else:

interactions/api/models/member.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
from .attrs_utils import MISSING, ClientSerializerMixin, convert_int, convert_list, define, field
66
from .channel import Channel
77
from .flags import Permissions
8-
from .misc import File, IDMixin, Snowflake
8+
from .misc import AllowedMentions, File, IDMixin, Snowflake
99
from .role import Role
1010
from .user import User
1111

1212
if TYPE_CHECKING:
1313
from ...client.models.component import ActionRow, Button, SelectMenu
1414
from .guild import Guild
15-
from .message import Attachment, Embed, Message, MessageInteraction
15+
from .message import Attachment, Embed, Message
1616

1717
__all__ = ("Member",)
1818

@@ -222,7 +222,7 @@ async def send(
222222
attachments: Optional[List["Attachment"]] = MISSING,
223223
files: Optional[Union[File, List[File]]] = MISSING,
224224
embeds: Optional[Union["Embed", List["Embed"]]] = MISSING,
225-
allowed_mentions: Optional["MessageInteraction"] = MISSING,
225+
allowed_mentions: Optional[Union[AllowedMentions, dict]] = MISSING,
226226
) -> "Message":
227227
"""
228228
Sends a DM to the member.
@@ -239,8 +239,8 @@ async def send(
239239
:type files?: Optional[Union[File, List[File]]]
240240
:param embeds?: An embed, or list of embeds for the message.
241241
:type embeds?: Optional[Union[Embed, List[Embed]]]
242-
:param allowed_mentions?: The message interactions/mention limits that the message can refer to.
243-
:type allowed_mentions?: Optional[MessageInteraction]
242+
:param allowed_mentions?: The allowed mentions for the message.
243+
:type allowed_mentions?: Optional[Union[AllowedMentions, dict]]
244244
:return: The sent message as an object.
245245
:rtype: Message
246246
"""
@@ -257,7 +257,13 @@ async def send(
257257
if not embeds or embeds is MISSING
258258
else ([embed._json for embed in embeds] if isinstance(embeds, list) else [embeds._json])
259259
)
260-
_allowed_mentions: dict = {} if allowed_mentions is MISSING else allowed_mentions
260+
_allowed_mentions: dict = (
261+
{}
262+
if allowed_mentions is MISSING
263+
else allowed_mentions._json
264+
if isinstance(allowed_mentions, AllowedMentions)
265+
else allowed_mentions
266+
)
261267
if not components or components is MISSING:
262268
_components = []
263269
else:

interactions/api/models/message.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from .channel import Channel
2020
from .emoji import Emoji
2121
from .member import Member
22-
from .misc import File, IDMixin, Snowflake
22+
from .misc import AllowedMentions, File, IDMixin, Snowflake
2323
from .team import Application
2424
from .user import User
2525

@@ -744,7 +744,6 @@ class Message(ClientSerializerMixin, IDMixin):
744744
:ivar Optional[MessageActivity] activity?: Message activity object that's sent by Rich Presence
745745
:ivar Optional[Application] application?: Application object that's sent by Rich Presence
746746
:ivar Optional[MessageReference] message_reference?: Data showing the source of a message (crosspost, channel follow, add, pin, or replied message)
747-
:ivar Optional[Any] allowed_mentions: The allowed mentions of roles attached in the message.
748747
:ivar int flags: Message flags
749748
:ivar Optional[MessageInteraction] interaction?: Message interaction object, if the message is sent by an interaction.
750749
:ivar Optional[Channel] thread?: The thread that started from this message, if any, with a thread member object embedded.
@@ -846,7 +845,7 @@ async def edit(
846845
files: Optional[Union[File, List[File]]] = MISSING,
847846
embeds: Optional[Union["Embed", List["Embed"]]] = MISSING,
848847
suppress_embeds: Optional[bool] = MISSING,
849-
allowed_mentions: Optional["MessageInteraction"] = MISSING,
848+
allowed_mentions: Optional[Union[AllowedMentions, dict]] = MISSING,
850849
message_reference: Optional[MessageReference] = MISSING,
851850
attachments: Optional[List["Attachment"]] = MISSING,
852851
components: Optional[
@@ -873,8 +872,8 @@ async def edit(
873872
:type embeds?: Optional[Union[Embed, List[Embed]]]
874873
:param suppress_embeds?: Whether to suppress embeds in the message.
875874
:type suppress_embeds?: Optional[bool]
876-
:param allowed_mentions?: The message interactions/mention limits that the message can refer to.
877-
:type allowed_mentions?: Optional[MessageInteraction]
875+
:param allowed_mentions?: The allowed mentions for the message.
876+
:type allowed_mentions?: Optional[Union[AllowedMentions, dict]]
878877
:param attachments?: The attachments to attach to the message. Needs to be uploaded to the CDN first
879878
:type attachments?: Optional[List[Attachment]]
880879
:param components?: A component, or list of components for the message. If `[]` the components will be removed
@@ -922,7 +921,13 @@ async def edit(
922921
else []
923922
)
924923

925-
_allowed_mentions: dict = {} if allowed_mentions is MISSING else allowed_mentions
924+
_allowed_mentions: dict = (
925+
{}
926+
if allowed_mentions is MISSING
927+
else allowed_mentions._json
928+
if isinstance(allowed_mentions, AllowedMentions)
929+
else allowed_mentions
930+
)
926931
_message_reference: dict = {} if message_reference is MISSING else message_reference._json
927932
if not components:
928933
_components = []
@@ -961,7 +966,7 @@ async def reply(
961966
embeds: Optional[Union["Embed", List["Embed"]]] = MISSING,
962967
files: Optional[Union[File, List[File]]] = MISSING,
963968
attachments: Optional[List["Attachment"]] = MISSING,
964-
allowed_mentions: Optional["MessageInteraction"] = MISSING,
969+
allowed_mentions: Optional[Union[AllowedMentions, dict]] = MISSING,
965970
stickers: Optional[List["Sticker"]] = MISSING,
966971
components: Optional[
967972
Union[
@@ -987,8 +992,8 @@ async def reply(
987992
:type files?: Optional[Union[File, List[File]]]
988993
:param embeds?: An embed, or list of embeds for the message.
989994
:type embeds?: Optional[Union[Embed, List[Embed]]]
990-
:param allowed_mentions?: The message interactions/mention limits that the message can refer to.
991-
:type allowed_mentions?: Optional[MessageInteraction]
995+
:param allowed_mentions?: The allowed mentions for the message.
996+
:type allowed_mentions?: Optional[Union[AllowedMentions, dict]]
992997
:param components?: A component, or list of components for the message.
993998
:type components?: Optional[Union[ActionRow, Button, SelectMenu, List[ActionRow], List[Button], List[SelectMenu]]]
994999
:param stickers?: A list of stickers to send with your message. You can send up to 3 stickers per message.
@@ -1009,7 +1014,13 @@ async def reply(
10091014
if not embeds or embeds is MISSING
10101015
else ([embed._json for embed in embeds] if isinstance(embeds, list) else [embeds._json])
10111016
)
1012-
_allowed_mentions: dict = {} if allowed_mentions is MISSING else allowed_mentions
1017+
_allowed_mentions: dict = (
1018+
{}
1019+
if allowed_mentions is MISSING
1020+
else allowed_mentions._json
1021+
if isinstance(allowed_mentions, AllowedMentions)
1022+
else allowed_mentions
1023+
)
10131024
_message_reference = MessageReference(message_id=int(self.id))._json
10141025
_attachments = [] if attachments is MISSING else [a._json for a in attachments]
10151026
if not components or components is MISSING:

interactions/api/models/misc.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import datetime
99
from base64 import b64encode
10-
from enum import IntEnum
10+
from enum import Enum, IntEnum
1111
from io import FileIO, IOBase
1212
from logging import Logger
1313
from math import floor
@@ -16,14 +16,16 @@
1616

1717
from ...base import get_logger
1818
from ..error import LibraryException
19-
from .attrs_utils import MISSING, DictSerializerMixin, define, field
19+
from .attrs_utils import MISSING, DictSerializerMixin, convert_list, define, field
2020

2121
__all__ = (
2222
"AutoModKeywordPresetTypes",
2323
"AutoModTriggerType",
2424
"AutoModMetaData",
2525
"AutoModAction",
2626
"AutoModTriggerMetadata",
27+
"AllowedMentionType",
28+
"AllowedMentions",
2729
"Snowflake",
2830
"Color",
2931
"ClientStatus",
@@ -370,3 +372,32 @@ def filename(self) -> str:
370372
Returns the name of the file.
371373
"""
372374
return self._name.split("/")[-1].split(".")[0]
375+
376+
377+
class AllowedMentionType(str, Enum):
378+
"""
379+
An enumerable object representing the allowed mention types
380+
"""
381+
382+
EVERYONE = "everyone"
383+
USERS = "users"
384+
ROLES = "roles"
385+
386+
387+
@define()
388+
class AllowedMentions(DictSerializerMixin):
389+
"""
390+
A class object representing the allowed mentions object
391+
392+
:ivar parse?: Optional[List[AllowedMentionType]]: An array of allowed mention types to parse from the content.
393+
:ivar users?: Optional[List[int]]: An array of user ids to mention.
394+
:ivar roles?: Optional[List[int]]: An array of role ids to mention.
395+
:ivar replied_user?: Optional[bool]: For replies, whether to mention the author of the message being replied to.
396+
"""
397+
398+
parse: Optional[List[AllowedMentionType]] = field(
399+
converter=convert_list(AllowedMentionType), default=None
400+
)
401+
users: Optional[List[int]] = field(default=None)
402+
roles: Optional[List[int]] = field(default=None)
403+
replied_user: Optional[bool] = field(default=None)

interactions/api/models/webhook.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from ..error import LibraryException
55
from .attrs_utils import MISSING, ClientSerializerMixin, define, field
6-
from .misc import File, IDMixin, Image, Snowflake
6+
from .misc import AllowedMentions, File, IDMixin, Image, Snowflake
77
from .user import User
88

99
if TYPE_CHECKING:
@@ -180,7 +180,7 @@ async def execute(
180180
avatar_url: Optional[str] = MISSING,
181181
tts: Optional[bool] = MISSING,
182182
embeds: Optional[Union["Embed", List["Embed"]]] = MISSING,
183-
allowed_mentions: Any = MISSING,
183+
allowed_mentions: Optional[Union[AllowedMentions, dict]] = MISSING,
184184
attachments: Optional[List["Attachment"]] = MISSING,
185185
components: Optional[
186186
Union[
@@ -213,8 +213,8 @@ async def execute(
213213
:type attachments?: Optional[List[Attachment]]
214214
:param embeds: embedded ``rich`` content
215215
:type embeds: Union[Embed, List[Embed]]
216-
:param allowed_mentions: allowed mentions for the message
217-
:type allowed_mentions: dict
216+
:param allowed_mentions?: The allowed mentions for the message.
217+
:type allowed_mentions?: Optional[Union[AllowedMentions, dict]]
218218
:param components: the components to include with the message
219219
:type components: Union[ActionRow, Button, SelectMenu, List[ActionRow], List[Button], List[SelectMenu]]
220220
:param files: The files to attach to the message
@@ -240,7 +240,13 @@ async def execute(
240240
if not embeds or embeds is MISSING
241241
else ([embed._json for embed in embeds] if isinstance(embeds, list) else [embeds._json])
242242
)
243-
_allowed_mentions: dict = {} if allowed_mentions is MISSING else allowed_mentions
243+
_allowed_mentions: dict = (
244+
{}
245+
if allowed_mentions is MISSING
246+
else allowed_mentions._json
247+
if isinstance(allowed_mentions, AllowedMentions)
248+
else allowed_mentions
249+
)
244250

245251
if not components or components is MISSING:
246252
_components = []

0 commit comments

Comments
 (0)