Skip to content

Commit

Permalink
feat: implement soundmojis
Browse files Browse the repository at this point in the history
  • Loading branch information
Snipy7374 committed Jan 29, 2025
1 parent ce88413 commit 091b54f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/1280.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add :attr:`PartialSoundboardSound.mention`, :attr:`SoundboardSound.mention`, :attr:`GuildSoundboardSound.mention`, :attr:`Message.soundboard_sounds` and :attr:`ForwardedMessage.soundboard_sounds`.
23 changes: 22 additions & 1 deletion disnake/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from .errors import HTTPException
from .file import File
from .flags import AttachmentFlags, MessageFlags
from .guild import Guild
from .guild import Guild, GuildSoundboardSound
from .member import Member
from .mixins import Hashable
from .partial_emoji import PartialEmoji
Expand Down Expand Up @@ -1097,6 +1097,11 @@ class Message(Hashable):
The poll contained in this message.
.. versionadded:: 2.10
soundboard_sounds: List[:class:`GuildSoundboardSound`]
A list of soundboard sounds in the message.
.. versionadded:: 2.11
"""

__slots__ = (
Expand Down Expand Up @@ -1135,6 +1140,7 @@ class Message(Hashable):
"components",
"guild",
"poll",
"soundboard_sounds",
"_edited_timestamp",
"_role_subscription_data",
)
Expand Down Expand Up @@ -1200,6 +1206,11 @@ def __init__(
except AttributeError:
self.guild = state._get_guild(utils._get_as_snowflake(data, "guild_id"))

self.soundboard_sounds: List[GuildSoundboardSound] = [
GuildSoundboardSound(data=d, state=state, guild_id=self.guild.id)

Check failure on line 1210 in disnake/message.py

View workflow job for this annotation

GitHub Actions / pyright (3.8, false)

"id" is not a known member of "None" (reportOptionalMemberAccess)
for d in data.get("soundboard_sounds", [])
]

self._interaction: Optional[InteractionReference] = (
InteractionReference(state=state, guild=self.guild, data=interaction)
if (interaction := data.get("interaction"))
Expand Down Expand Up @@ -2885,6 +2896,10 @@ class ForwardedMessage:
A list of components in the message.
guild_id: Optional[:class:`int`]
The guild ID where the message was forwarded from, if applicable.
soundboard_sounds: List[:class:`GuildSoundboardSound`]
A list of soundboard sounds in the message.
.. versionadded:: 2.11
"""

__slots__ = (
Expand All @@ -2902,6 +2917,7 @@ class ForwardedMessage:
"stickers",
"components",
"guild_id",
"soundboard_sounds",
)

def __init__(
Expand Down Expand Up @@ -2956,6 +2972,11 @@ def __init__(
if role is not None:
self.role_mentions.append(role)

self.soundboard_sounds: List[GuildSoundboardSound] = [
GuildSoundboardSound(data=d, state=state, guild_id=guild_id or 0)
for d in data.get("soundboard_sounds", [])
]

def __repr__(self) -> str:
return f"<{self.__class__.__name__}>"

Expand Down
18 changes: 18 additions & 0 deletions disnake/soundboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ def created_at(self) -> Optional[datetime.datetime]:
return None
return snowflake_time(self.id)

@property
def mention(self) -> str:
""":class:`str`: Returns a string that allows you to mention the given soundboard sound.
.. versionadded:: 2.11
"""
return f"<sound:0:{self.id}>"

@property
def url(self) -> str:
""":class:`str`: The url for the sound file."""
Expand Down Expand Up @@ -230,6 +238,16 @@ def guild(self) -> Guild:
# this will most likely never return None
return self._state._get_guild(self.guild_id) # type: ignore

@property
def mention(self) -> str:
""":class:`str`: Returns a string that allows you to mention the given soundboard sound.
.. versionadded:: 2.11
"""
if self.is_default():
return super().mention
return f"<sound:{self.guild_id}:{self.id}>"

async def edit(
self,
*,
Expand Down
3 changes: 3 additions & 0 deletions disnake/types/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .member import Member, UserWithMember
from .poll import Poll
from .snowflake import Snowflake, SnowflakeList
from .soundboard import GuildSoundboardSound
from .sticker import StickerItem
from .threads import Thread
from .user import User
Expand Down Expand Up @@ -90,6 +91,7 @@ class ForwardedMessage(TypedDict):
mention_roles: NotRequired[SnowflakeList]
sticker_items: NotRequired[List[StickerItem]]
components: NotRequired[List[Component]]
soundboard_sounds: NotRequired[List[GuildSoundboardSound]]


class MessageSnapshot(TypedDict):
Expand Down Expand Up @@ -149,6 +151,7 @@ class Message(TypedDict):
# specific to MESSAGE_CREATE/MESSAGE_UPDATE events
guild_id: NotRequired[Snowflake]
member: NotRequired[Member]
soundboard_sounds: NotRequired[List[GuildSoundboardSound]]


AllowedMentionType = Literal["roles", "users", "everyone"]
Expand Down

0 comments on commit 091b54f

Please sign in to comment.