Skip to content

Commit 762db8b

Browse files
committed
🐛 Fixes
1 parent 5ced85d commit 762db8b

File tree

9 files changed

+68
-74
lines changed

9 files changed

+68
-74
lines changed

discord/app/cache.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from discord import utils
2929
from discord.member import Member
3030
from discord.message import Message
31+
from discord.soundboard import SoundboardSound
3132

3233
from ..channel import DMChannel
3334
from ..emoji import AppEmoji, GuildEmoji
@@ -166,6 +167,13 @@ async def get_all_members(self) -> list[Member]: ...
166167

167168
async def clear(self, views: bool = True) -> None: ...
168169

170+
async def store_sound(self, sound: SoundboardSound) -> None: ...
171+
172+
async def get_sound(self, sound_id: int) -> SoundboardSound | None: ...
173+
174+
async def get_all_sounds(self) -> list[SoundboardSound]: ...
175+
176+
async def delete_sound(self, sound_id: int) -> None: ...
169177

170178
class MemoryCache(Cache):
171179
def __init__(self, max_messages: int | None = None) -> None:
@@ -177,6 +185,7 @@ def __init__(self, max_messages: int | None = None) -> None:
177185
self._stickers: dict[int, list[GuildSticker]] = {}
178186
self._views: dict[str, View] = {}
179187
self._modals: dict[str, Modal] = {}
188+
self._sounds: dict[int, SoundboardSound] = {}
180189
self._messages: Deque[Message] = deque(maxlen=self.max_messages)
181190

182191
self._emojis: dict[int, list[GuildEmoji | AppEmoji]] = {}
@@ -186,6 +195,8 @@ def __init__(self, max_messages: int | None = None) -> None:
186195

187196
self._guild_members: dict[int, dict[int, Member]] = defaultdict(dict)
188197

198+
199+
189200
def _flatten(self, matrix: list[list[T]]) -> list[T]:
190201
return [item for row in matrix for item in row]
191202

@@ -396,3 +407,15 @@ async def get_guild_members(self, guild_id: int) -> list[Member]:
396407

397408
async def get_all_members(self) -> list[Member]:
398409
return self._flatten([list(members.values()) for members in self._guild_members.values()])
410+
411+
async def store_sound(self, sound: SoundboardSound) -> None:
412+
self._sounds[sound.id] = sound
413+
414+
async def get_sound(self, sound_id: int) -> SoundboardSound | None:
415+
return self._sounds.get(sound_id)
416+
417+
async def get_all_sounds(self) -> list[SoundboardSound]:
418+
return list(self._sounds.values())
419+
420+
async def delete_sound(self, sound_id: int) -> None:
421+
self._sounds.pop(sound_id, None)

discord/app/state.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
cast,
4545
)
4646

47+
from discord.soundboard import SoundboardSound
48+
4749
from .. import utils
4850
from ..activity import BaseActivity
4951
from ..automod import AutoModRule
@@ -237,7 +239,7 @@ def __init__(
237239
self._voice_clients: dict[int, VoiceClient] = {}
238240

239241
if not intents.members or cache_flags._empty:
240-
self.store_user = self.create_user # type: ignore
242+
self.store_user = self.create_user_async # type: ignore
241243
self.deref_user = self.deref_user_no_intents # type: ignore
242244

243245
self.cache_app_emojis: bool = options.get("cache_app_emojis", False)
@@ -320,6 +322,9 @@ async def deref_user(self, user_id: int) -> None:
320322
def create_user(self, data: UserPayload) -> User:
321323
return User(state=self, data=data)
322324

325+
async def create_user_async(self, data: UserPayload) -> User:
326+
return User(state=self, data=data)
327+
323328
def deref_user_no_intents(self, user_id: int) -> None:
324329
return
325330

@@ -373,6 +378,21 @@ async def _remove_guild(self, guild: Guild) -> None:
373378

374379
del guild
375380

381+
async def _add_default_sounds(self) -> None:
382+
default_sounds = await self.http.get_default_sounds()
383+
for default_sound in default_sounds:
384+
sound = SoundboardSound(state=self, http=self.http, data=default_sound)
385+
await self._add_sound(sound)
386+
387+
async def _add_sound(self, sound: SoundboardSound) -> None:
388+
await self.cache.store_sound(sound)
389+
390+
async def _remove_sound(self, sound: SoundboardSound) -> None:
391+
await self.cache.delete_sound(sound.id)
392+
393+
async def get_sounds(self) -> list[SoundboardSound]:
394+
return list(await self.cache.get_all_sounds())
395+
376396
async def get_emojis(self) -> list[GuildEmoji | AppEmoji]:
377397
return await self.cache.get_all_emojis()
378398

@@ -487,15 +507,15 @@ async def query_members(
487507
)
488508
raise
489509

490-
def _get_create_guild(self, data):
510+
async def _get_create_guild(self, data):
491511
if data.get("unavailable") is False:
492512
# GUILD_CREATE with unavailable in the response
493513
# usually means that the guild has become available
494514
# and is therefore in the cache
495-
guild = self._get_guild(int(data["id"]))
515+
guild = await self._get_guild(int(data["id"]))
496516
if guild is not None:
497517
guild.unavailable = False
498-
guild._from_data(data)
518+
await guild._from_data(data, self)
499519
return guild
500520

501521
return self._add_guild_from_data(data)
@@ -660,6 +680,7 @@ async def _delay_ready(self) -> None:
660680
future = asyncio.ensure_future(self.chunk_guild(guild))
661681
current_bucket.append(future)
662682
else:
683+
await self._add_default_sounds()
663684
future = self.loop.create_future()
664685
future.set_result([])
665686

discord/events/channel.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def __init__(self) -> None: ...
7373

7474
@classmethod
7575
@override
76-
async def __load__(cls, data: tuple[PrivateChannel | None, PrivateChannel], _: ConnectionState) -> Self | None:
76+
async def __load__(cls, data: tuple[PrivateChannel | None, PrivateChannel], state: ConnectionState) -> Self | None:
7777
self = cls()
7878
self.old = data[0]
7979
self.__dict__.update(data[1].__dict__)
@@ -89,7 +89,7 @@ def __init__(self) -> None: ...
8989

9090
@classmethod
9191
@override
92-
async def __load__(cls, data: tuple[GuildChannel | None, GuildChannel], _: ConnectionState) -> Self | None:
92+
async def __load__(cls, data: tuple[GuildChannel | None, GuildChannel], state: ConnectionState) -> Self | None:
9393
self = cls()
9494
self.old = data[0]
9595
self.__dict__.update(data[1].__dict__)

discord/events/gateway.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ async def __load__(cls, data: GuildPayload, state: ConnectionState) -> Self:
131131
guild = await Guild._from_data(data, state)
132132
await state._add_guild(guild)
133133
self.guild = guild
134-
self.__dict__.update(self.guild.__dict__)
134+
# self.__dict__.update(self.guild.__dict__) # TODO: Find another way to do this
135135
if state._guild_needs_chunking(guild):
136136
await state.chunk_guild(guild)
137137
if guild.unavailable:
@@ -153,10 +153,10 @@ def __init__(self) -> None:
153153

154154
@classmethod
155155
@override
156-
async def __load__(cls, data: Guild, _: ConnectionState) -> Self:
156+
async def __load__(cls, data: Guild, state: ConnectionState) -> Self:
157157
self = cls()
158158
self.guild = data
159-
self.__dict__.update(self.guild.__dict__)
159+
# self.__dict__.update(self.guild.__dict__) # TODO: Find another way to do this
160160
return self
161161

162162

@@ -172,10 +172,10 @@ def __init__(self) -> None:
172172

173173
@classmethod
174174
@override
175-
async def __load__(cls, data: Guild, _: ConnectionState) -> Self:
175+
async def __load__(cls, data: Guild, state: ConnectionState) -> Self:
176176
self = cls()
177177
self.guild = data
178-
self.__dict__.update(self.guild.__dict__)
178+
# self.__dict__.update(self.guild.__dict__) # TODO: Find another way to do this
179179
return self
180180

181181

discord/events/guild.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,9 @@ def __init__(self) -> None: ...
255255

256256
@classmethod
257257
@override
258-
async def __load__(cls, data: Guild, _: ConnectionState) -> Self:
258+
async def __load__(cls, data: Guild, state: ConnectionState) -> Self:
259259
self = cls()
260-
self.__dict__.update(data.__dict__)
260+
# self.__dict__.update(data.__dict__) # TODO: Find another way to do this
261261
return self
262262

263263

@@ -268,7 +268,7 @@ def __init__(self) -> None: ...
268268

269269
@classmethod
270270
@override
271-
async def __load__(cls, data: Guild, _: ConnectionState) -> Self:
271+
async def __load__(cls, data: Guild, state: ConnectionState) -> Self:
272272
self = cls()
273273
self.__dict__.update(data.__dict__)
274274
return self
@@ -281,9 +281,9 @@ def __init__(self) -> None: ...
281281

282282
@classmethod
283283
@override
284-
async def __load__(cls, data: Guild, _: ConnectionState) -> Self:
284+
async def __load__(cls, data: Guild, state: ConnectionState) -> Self:
285285
self = cls()
286-
self.__dict__.update(data.__dict__)
286+
# self.__dict__.update(data.__dict__) # TODO: Find another way to do this
287287
return self
288288

289289

@@ -323,7 +323,7 @@ async def __load__(cls, data: Any, state: ConnectionState) -> Self | None:
323323
await state.emitter.emit("GUILD_JOIN", guild)
324324

325325
self = cls()
326-
self.__dict__.update(data.__dict__)
326+
# self.__dict__.update(data.__dict__) # TODO: Find another way to do this
327327
return self
328328

329329

discord/events/thread.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def __init__(self) -> None: ...
4646

4747
@classmethod
4848
@override
49-
async def __load__(cls, data: ThreadMember, _: ConnectionState) -> Self:
49+
async def __load__(cls, data: ThreadMember, state: ConnectionState) -> Self:
5050
self = cls()
5151
self.__dict__.update(data.__dict__)
5252
return self
@@ -59,7 +59,7 @@ def __init__(self) -> None: ...
5959

6060
@classmethod
6161
@override
62-
async def __load__(cls, data: Thread, _: ConnectionState) -> Self:
62+
async def __load__(cls, data: Thread, state: ConnectionState) -> Self:
6363
self = cls()
6464
self.__dict__.update(data.__dict__)
6565
return self
@@ -72,7 +72,7 @@ def __init__(self) -> None: ...
7272

7373
@classmethod
7474
@override
75-
async def __load__(cls, data: ThreadMember, _: ConnectionState) -> Self:
75+
async def __load__(cls, data: ThreadMember, state: ConnectionState) -> Self:
7676
self = cls()
7777
self.__dict__.update(data.__dict__)
7878
return self
@@ -85,7 +85,7 @@ def __init__(self) -> None: ...
8585

8686
@classmethod
8787
@override
88-
async def __load__(cls, data: Thread, _: ConnectionState) -> Self:
88+
async def __load__(cls, data: Thread, state: ConnectionState) -> Self:
8989
self = cls()
9090
self.__dict__.update(data.__dict__)
9191
return self

discord/gateway.py

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -367,31 +367,6 @@ async def from_client(
367367
await ws.resume()
368368
return ws
369369

370-
def wait_for(self, event, predicate, result=None):
371-
"""Waits for a DISPATCH'd event that meets the predicate.
372-
373-
Parameters
374-
----------
375-
event: :class:`str`
376-
The event name in all upper case to wait for.
377-
predicate
378-
A function that takes a data parameter to check for event
379-
properties. The data parameter is the 'd' key in the JSON message.
380-
result
381-
A function that takes the same data parameter and executes to send
382-
the result to the future. If ``None``, returns the data.
383-
384-
Returns
385-
-------
386-
asyncio.Future
387-
A future to wait for.
388-
"""
389-
390-
future = self.loop.create_future()
391-
entry = EventListener(event=event, predicate=predicate, result=result, future=future)
392-
self._dispatch_listeners.append(entry)
393-
return future
394-
395370
async def identify(self):
396371
"""Sends the IDENTIFY packet."""
397372
payload = {
@@ -538,31 +513,6 @@ async def received_message(self, msg, /):
538513

539514
await self._emitter.emit(event, data)
540515

541-
# remove the dispatched listeners
542-
removed = []
543-
for index, entry in enumerate(self._dispatch_listeners):
544-
if entry.event != event:
545-
continue
546-
547-
future = entry.future
548-
if future.cancelled():
549-
removed.append(index)
550-
continue
551-
552-
try:
553-
valid = entry.predicate(data)
554-
except Exception as exc:
555-
future.set_exception(exc)
556-
removed.append(index)
557-
else:
558-
if valid:
559-
ret = data if entry.result is None else entry.result(data)
560-
future.set_result(ret)
561-
removed.append(index)
562-
563-
for index in reversed(removed):
564-
del self._dispatch_listeners[index]
565-
566516
@property
567517
def latency(self) -> float:
568518
"""Measures latency between a HEARTBEAT and a HEARTBEAT_ACK in seconds. If no heartbeat

discord/gears/gear.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def listen(
9393
@hybridmethod
9494
def listen(
9595
cls: type[_T],
96-
event: type[E], # noqa: N805
96+
event: type[E],
9797
) -> Callable[[Callable[[Any, E], Awaitable[None]]], InstanceEventCallback[E]]:
9898
def decorator(func: Callable[[Any, E], Awaitable[None]]) -> InstanceEventCallback[E]:
9999
func.__is_instance_method__ = True

discord/guild.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ def _voice_state_for(self, user_id: int, /) -> VoiceState | None:
324324
return self._voice_states.get(user_id)
325325

326326
async def _add_member(self, member: Member, /) -> None:
327-
await cast(ConnectionState, self._state).cache.store_member(member)
327+
await cast("ConnectionState", self._state).cache.store_member(member)
328328

329329
async def _get_and_update_member(self, payload: MemberPayload, user_id: int, cache_flag: bool, /) -> Member:
330330
members = await cast(ConnectionState, self._state).cache.get_guild_members(self.id)
@@ -549,7 +549,7 @@ async def _from_data(cls, guild: GuildPayload, state: ConnectionState) -> Self:
549549

550550
def _add_sound(self, sound: SoundboardSound) -> None:
551551
self._sounds[sound.id] = sound
552-
self._state._add_sound(sound)
552+
await self._state._add_sound(sound)
553553

554554
def _remove_sound(self, sound_id: int) -> None:
555555
self._sounds.pop(sound_id, None)

0 commit comments

Comments
 (0)