Skip to content

Developer - Implementation of SoundboardSound #87

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 23 commits into
base: developer
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 discord/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
from .sticker import Sticker, GuildSticker, StickerPack
from .scheduled_event import GuildScheduledEvent
from .monetization import *
from .soundboard import *


MISSING = utils.MISSING
Expand Down
2 changes: 1 addition & 1 deletion discord/application_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1937,7 +1937,7 @@ def __init__(
if 32 < len(name) < 1:
raise ValueError('The name of the Message-Command has to be 1-32 characters long, got %s.' % len(name))
super().__init__(3, name=name, name_localizations=name_localizations,
default_member_permissions=default_member_permissions, allow_dm=allow_dm, integration_types=integration_types,
default_member_permissions=default_member_permissions, allow_dm=allow_dm, integration_types=inntegration_types,
contexts=contexts, **kwargs
)

Expand Down
41 changes: 41 additions & 0 deletions discord/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
from .iterators import GuildIterator, EntitlementIterator
from .appinfo import AppInfo
from .application_commands import *
from .soundboard import SoundboardSound

if TYPE_CHECKING:
import datetime
Expand Down Expand Up @@ -467,12 +468,14 @@ async def _run_event(self, coro: Coro, event_name: str, *args, **kwargs):

def _schedule_event(self, coro: Coro, event_name: str, *args, **kwargs) -> _ClientEventTask:
wrapped = self._run_event(coro, event_name, *args, **kwargs)
#print(coro, event_name, *args, **kwargs)
# Schedules the task
return _ClientEventTask(original_coro=coro, event_name=event_name, coro=wrapped, loop=self.loop)

def dispatch(self, event: str, *args, **kwargs) -> None:
log.debug('Dispatching event %s', event)
method = 'on_' + event
#print(method)

listeners = self._listeners.get(event)
if listeners:
Expand Down Expand Up @@ -744,6 +747,44 @@ async def request_offline_members(self, *guilds):
for guild in guilds:
await self._connection.chunk_guild(guild)

async def fetch_soundboard_sounds(self, guild_id):
"""|coro|

Requests all soundboard sounds for the given guilds.

This method retrieves the list of soundboard sounds from the Discord API for each guild ID provided.

.. note::

You must have the :attr:`~Permissions.manage_guild_expressions` permission
in each guild to retrieve its soundboard sounds.

Parameters
----------
guild_ids: List[:class:`int`]
A list of guild IDs to fetch soundboard sounds from.

Raises
-------
HTTPException
Retrieving soundboard sounds failed.
NotFound
One of the provided guilds does not exist or is inaccessible.
Forbidden
Missing permissions to view soundboard sounds in one or more guilds.

Returns
-------
Dict[:class:`int`, List[:class:`SoundboardSound`]]
A dictionary mapping each guild ID to a list of its soundboard sounds.
"""
guild = self.get_guild(guild_id)

data = await self.http.all_soundboard_sounds(guild_id)
data = data["items"]
return SoundboardSound._from_list(guild=guild, state=self._connection, data_list=data)
#await self.ws.request_soundboard_sounds(guild_ids)

# hooks

async def _call_before_identify_hook(self, shard_id, *, initial=False):
Expand Down
41 changes: 28 additions & 13 deletions discord/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,25 +268,28 @@ class DiscordWebSocket:
a connection issue.
GUILD_SYNC
Send only. Requests a guild sync.
REQUEST_SOUNDBOARD_SOUNDs
Send only. Used to request soundboard sounds for a list of guilds.
gateway
The gateway we are currently connected to.
token
The authentication token for discord.
"""

DISPATCH = 0
HEARTBEAT = 1
IDENTIFY = 2
PRESENCE = 3
VOICE_STATE = 4
VOICE_PING = 5
RESUME = 6
RECONNECT = 7
REQUEST_MEMBERS = 8
INVALIDATE_SESSION = 9
HELLO = 10
HEARTBEAT_ACK = 11
GUILD_SYNC = 12
DISPATCH = 0
HEARTBEAT = 1
IDENTIFY = 2
PRESENCE = 3
VOICE_STATE = 4
VOICE_PING = 5
RESUME = 6
RECONNECT = 7
REQUEST_MEMBERS = 8
INVALIDATE_SESSION = 9
HELLO = 10
HEARTBEAT_ACK = 11
GUILD_SYNC = 12
REQUEST_SOUNDBOARD_SOUNDs = 31

def __init__(self, socket, *, loop):
self.socket = socket
Expand Down Expand Up @@ -718,6 +721,18 @@ async def voice_state(self, guild_id, channel_id, self_mute=False, self_deaf=Fal
log.debug('Updating our voice state to %s.', payload)
await self.send_as_json(payload)

async def request_soundboard_sounds(self, guild_ids):
if not isinstance(guild_ids, list):
raise TypeError("guild_ids has to be a list.")

payload = {
'op': self.REQUEST_SOUNDBOARD_SOUNDs,
'd': {
'guild_ids': guild_ids
}
}
await self.send_as_json(payload)

async def close(self, code=4000):
if self._keep_alive:
self._keep_alive.stop()
Expand Down
Loading