Skip to content
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

feat: implement soundmojis #1281

Open
wants to merge 1 commit 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
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 @@
The poll contained in this message.

.. versionadded:: 2.10

soundboard_sounds: List[:class:`GuildSoundboardSound`]
A list of soundboard sounds in the message.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
A list of soundboard sounds in the message.
A list of soundboard sounds in the message,
if the message contains soundmojis.


.. versionadded:: 2.11
"""

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

self.soundboard_sounds: List[GuildSoundboardSound] = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also handle this for message updates.

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this won't necessarily be the same guild (or even from any guild at all), soundmojis can be from other guilds or default sounds.

Example message data snippets:

guild sound

    "soundboard_sounds": [
        {
            "name": "test",
            "sound_id": "123123",
            "volume": 0.699999988079071,
            "emoji_id": "456456",
            "emoji_name": null,
            "guild_id": "789789",
            "available": true
        }
    ],

default sound

    "soundboard_sounds": [
        {
            "name": "airhorn",
            "sound_id": "2",
            "volume": 1.0,
            "emoji_id": null,
            "emoji_name": "🔊",
            "available": true
        }
    ],

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 @@
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 @@
"stickers",
"components",
"guild_id",
"soundboard_sounds",
)

def __init__(
Expand Down Expand Up @@ -2956,6 +2972,11 @@
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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mention should be implemented on SoundboardSound and GuildSoundboardSound, but there isn't a use for it on partial sounds, as the guild data is just missing.

""":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
Loading