Skip to content

Add support for soundboard_sounds field on message #10102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion discord/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
from .threads import Thread
from .channel import PartialMessageable
from .poll import Poll
from .soundboard import SoundboardSound, SoundboardDefaultSound

if TYPE_CHECKING:
from typing_extensions import Self
Expand All @@ -81,6 +82,7 @@
CallMessage as CallMessagePayload,
PurchaseNotificationResponse as PurchaseNotificationResponsePayload,
GuildProductPurchase as GuildProductPurchasePayload,
SoundboardSound as SoundboardSoundPayload,
)

from .types.interactions import MessageInteraction as MessageInteractionPayload
Expand Down Expand Up @@ -865,7 +867,7 @@

self.original_response_message_id: Optional[int] = None
try:
self.original_response_message_id = int(data['original_response_message_id']) # type: ignore # EAFP

Check warning on line 870 in discord/message.py

View workflow job for this annotation

GitHub Actions / check 3.x

Unnecessary "# type: ignore" comment
except KeyError:
pass

Expand Down Expand Up @@ -2118,6 +2120,10 @@
message_snapshots: List[:class:`MessageSnapshot`]
The message snapshots attached to this message.

.. versionadded:: 2.5
soundboard_sounds: List[Union[:class:`SoundboardSound`, :class:`SoundboardDefaultSound`]]
The soundboard sounds mentioned in this message.

.. versionadded:: 2.5
"""

Expand Down Expand Up @@ -2158,6 +2164,7 @@
'call',
'purchase_notification',
'message_snapshots',
'soundboard_sounds',
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -2197,6 +2204,7 @@
self.application_id: Optional[int] = utils._get_as_snowflake(data, 'application_id')
self.stickers: List[StickerItem] = [StickerItem(data=d, state=state) for d in data.get('sticker_items', [])]
self.message_snapshots: List[MessageSnapshot] = MessageSnapshot._from_value(state, data.get('message_snapshots'))
self.soundboard_sounds: List[Union[SoundboardSound, SoundboardDefaultSound]] = []

self.poll: Optional[Poll] = None
try:
Expand Down Expand Up @@ -2299,7 +2307,7 @@
else:
self.purchase_notification = PurchaseNotification(purchase_notification)

for handler in ('author', 'member', 'mentions', 'mention_roles', 'components', 'call'):
for handler in ('author', 'member', 'mentions', 'mention_roles', 'components', 'call', 'soundboard_sounds'):
try:
getattr(self, f'_handle_{handler}')(data[handler])
except KeyError:
Expand Down Expand Up @@ -2492,6 +2500,21 @@
else:
self.call = None

def _handle_soundboard_sounds(self, data: List[SoundboardSoundPayload]):
for sound in data:
guild_id = utils._get_as_snowflake(sound, 'guild_id')
try:
if guild_id:
self.soundboard_sounds.append(
SoundboardSound(
state=self._state, data=sound, guild=self._state._get_or_create_unavailable_guild(guild_id)
)
)
else:
self.soundboard_sounds.append(SoundboardDefaultSound(state=self._state, data=sound)) # type: ignore # EAFP
except KeyError:
pass

def _rebind_cached_references(
self,
new_guild: Guild,
Expand Down
2 changes: 2 additions & 0 deletions discord/types/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from .sticker import StickerItem
from .threads import Thread
from .poll import Poll
from soundboard import SoundboardSound, SoundboardDefaultSound


class PartialMessage(TypedDict):
Expand Down Expand Up @@ -227,6 +228,7 @@ class Message(PartialMessage):
thread: NotRequired[Thread]
call: NotRequired[CallMessage]
purchase_notification: NotRequired[PurchaseNotificationResponse]
soundboard_sounds: NotRequired[List[Union[SoundboardSound, SoundboardDefaultSound]]]


AllowedMentionType = Literal['roles', 'users', 'everyone']
Expand Down
Loading