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(member): implement new voice states endpoints #1217

Merged
merged 18 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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/1216.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add :meth:`Member.fetch_voice_state` to fetch the :class:`VoiceState` of a member.
8 changes: 8 additions & 0 deletions disnake/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,14 @@ def change_nickname(
}
return self.request(r, json=payload, reason=reason)

def get_voice_state(
self, guild_id: Snowflake, user_id: Snowflake
) -> Response[voice.GuildVoiceState]:
r = Route(
"GET", "/guilds/{guild_id}/voice-states/{user_id}", guild_id=guild_id, user_id=user_id
)
return self.request(r)

def edit_my_voice_state(self, guild_id: Snowflake, payload: Dict[str, Any]) -> Response[None]:
r = Route("PATCH", "/guilds/{guild_id}/voice-states/@me", guild_id=guild_id)
return self.request(r, json=payload)
Expand Down
39 changes: 39 additions & 0 deletions disnake/member.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,45 @@ async def edit(
data = await http.edit_member(guild_id, self.id, reason=reason, **payload)
return Member(data=data, guild=self.guild, state=self._state)

async def fetch_voice_state(self, *, cache: bool = False) -> VoiceState:
"""|coro|

Fetches the :class:`VoiceState` of the member.

.. versionadded:: 2.10

.. warning::

If the :attr:`disnake.Intents.voice_states` is disabled caching this could lead to a memory leak.
This could happen because with the intent disabled you don't receive voice state updates, hence
the cached voice state could possibly never be removed from the cache.

Parameters
----------
cache: :class:`bool`
Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved
Whether to cache the fetched voice state. Defaults to ``False``.

Raises
------
NotFound
The member for which you tried to fetch a voice state is not
connected to a channel in this guild.

Snipy7374 marked this conversation as resolved.
Show resolved Hide resolved
Returns
-------
:class:`VoiceState`
The voice state of the member.
"""
data = await self._state.http.get_voice_state(self.guild.id, self.id)
channel_id = utils._get_as_snowflake(data, "channel_id")

if cache:
_, _, after = self.guild._update_voice_state(data, channel_id)
return after

channel: Optional[VocalGuildChannel] = self.guild.get_channel(channel_id) # type: ignore
return VoiceState(data=data, channel=channel)

async def request_to_speak(self) -> None:
"""|coro|

Expand Down
Loading