Skip to content

Commit

Permalink
✨ Soundboard gateway events
Browse files Browse the repository at this point in the history
  • Loading branch information
Paillat-dev committed Nov 7, 2024
1 parent 8238d7c commit a600965
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
36 changes: 36 additions & 0 deletions discord/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
from .types.guild import Guild as GuildPayload
from .types.message import Message as MessagePayload
from .types.poll import Poll as PollPayload
from .types.soundboard import SounboardSound as SoundboardSoundPayload
from .types.sticker import GuildSticker as GuildStickerPayload
from .types.user import User as UserPayload
from .voice_client import VoiceClient
Expand Down Expand Up @@ -2017,6 +2018,11 @@ def parse_voice_channel_effect_send(self, data) -> None:
def _get_sound(self, sound_id: int) -> SoundboardSound | None:
return self._sounds.get(sound_id)

def _update_sound(self, sound: SoundboardSound) -> SoundboardSound | None:
before = self._sounds.get(sound.id)
self._sounds[sound.id] = sound
return before

def parse_soundboard_sounds(self, data) -> None:
guild_id = int(data["guild_id"])
for sound_data in data["soundboard_sounds"]:
Expand All @@ -2026,6 +2032,36 @@ def parse_soundboard_sounds(self, data) -> None:
)
)

def parse_guild_soundboard_sounds_update(self, data):
before_sounds = []
after_sounds = []
for sound_data in data["soundboard_sounds"]:
after = SoundboardSound(state=self, http=self.http, data=sound_data)
if before := self._update_sound(after):
before_sounds.append(before)
after_sounds.append(after)
if len(before_sounds) == len(after_sounds):
self.dispatch("soundboard_sounds_update", before_sounds, after_sounds)
self.dispatch("raw_soundboard_sounds_update", after_sounds)

def parse_guild_soundboard_sound_update(self, data):
after = SoundboardSound(state=self, http=self.http, data=data)
if before := self._update_sound(after):
self.dispatch("soundboard_sound_update", before, after)
self.dispatch("raw_soundboard_sound_update", after)

def parse_guild_soundboard_sound_create(self, data):
sound = SoundboardSound(state=self, http=self.http, data=data)
self._add_sound(sound)
self.dispatch("soundboard_sound_create", sound)

def parse_guild_soundboard_sound_delete(self, data):
sound_id = int(data["sound_id"])
sound = self._get_sound(sound_id)
if sound is not None:
self._remove_sound(sound)
self.dispatch("soundboard_sound_delete", sound)

async def _add_default_sounds(self):
default_sounds = await self.http.get_default_sounds()
for default_sound in default_sounds:
Expand Down
62 changes: 62 additions & 0 deletions docs/api/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1388,3 +1388,65 @@ Voice Channel Effects

:param event: The voice channel effect event.
:type event: :class:`VoiceChannelEffectSendEvent`

Soundboard Sound
----------------
.. function:: on_soundboard_sounds_update(before, after)

Called when multiple guild soundboard sounds are updated at once and they were all already in the cache.
This is called for example when a guild looses a boost level and some sounds become unavailable.

.. versionadded:: 2.7

:param before: The soundboard sounds prior to being updated.
:type before: List[:class:`SoundboardSound`]
:param after: The soundboard sounds after being updated.
:type after: List[:class:`SoundboardSound`]

.. function:: on_raw_soundboard_sounds_update(after)

Called when multiple guild soundboard sounds are updated at once.
This is called for example when a guild looses a boost level and some sounds become unavailable.

.. versionadded:: 2.7

:param after: The soundboard sounds after being updated.
:type after: List[:class:`SoundboardSound`]

.. function:: on_soundboard_sound_update(before, after)

Called when a soundboard sound is updated and it was already in the cache.

.. versionadded:: 2.7

:param before: The soundboard sound prior to being updated.
:type before: :class:`Soundboard
:param after: The soundboard sound after being updated.
:type after: :class:`Soundboard
.. function:: on_raw_soundboard_sound_update(after)
Called when a soundboard sound is updated.
.. versionadded:: 2.7
:param after: The soundboard sound after being updated.
:type after: :class:`SoundboardSound`

.. function:: on_soundboard_sound_delete(sound)

Called when a soundboard sound is deleted.

.. versionadded:: 2.7

:param sound: The soundboard sound that was deleted.
:type sound: :class:`SoundboardSound`

.. function:: on_soundboard_sound_create(sound)

Called when a soundboard sound is created.

.. versionadded:: 2.7

:param sound: The soundboard sound that was created.
:type sound: :class:`SoundboardSound`

0 comments on commit a600965

Please sign in to comment.