diff --git a/changelog/1216.feature.rst b/changelog/1216.feature.rst new file mode 100644 index 0000000000..b28073e2b4 --- /dev/null +++ b/changelog/1216.feature.rst @@ -0,0 +1 @@ +Add :meth:`Guild.fetch_voice_state` to fetch the :class:`VoiceState` of a member. diff --git a/disnake/guild.py b/disnake/guild.py index 05f47d9720..a0609063bf 100644 --- a/disnake/guild.py +++ b/disnake/guild.py @@ -4673,6 +4673,46 @@ async def fetch_voice_regions(self) -> List[VoiceRegion]: data = await self._state.http.get_guild_voice_regions(self.id) return [VoiceRegion(data=region) for region in data] + async def fetch_voice_state(self, member_id: int, /) -> VoiceState: + """|coro| + + Fetches the :class:`VoiceState` of a member. + + .. note:: + + This method is an API call. For general usage, consider :attr:`Member.voice` instead. + + .. versionadded:: 2.10 + + Parameters + ---------- + member_id: :class:`int` + The ID of the member. + + Raises + ------ + NotFound + The member for which you tried to fetch a voice state is not + connected to a channel in this guild. + Forbidden + You do not have permission to fetch the member's voice state. + HTTPException + Fetching the voice state failed. + + Returns + ------- + :class:`VoiceState` + The voice state of the member. + """ + if member_id == self.me.id: + data = await self._state.http.get_my_voice_state(self.id) + else: + data = await self._state.http.get_voice_state(self.id, member_id) + + channel_id = utils._get_as_snowflake(data, "channel_id") + channel: Optional[VocalGuildChannel] = self.get_channel(channel_id) # type: ignore + return VoiceState(data=data, channel=channel) + async def change_voice_state( self, *, channel: Optional[Snowflake], self_mute: bool = False, self_deaf: bool = False ) -> None: diff --git a/disnake/http.py b/disnake/http.py index 6cfad49a8e..89dfe30724 100644 --- a/disnake/http.py +++ b/disnake/http.py @@ -984,6 +984,17 @@ def change_nickname( } return self.request(r, json=payload, reason=reason) + def get_my_voice_state(self, guild_id: Snowflake) -> Response[voice.GuildVoiceState]: + return self.request(Route("GET", "/guilds/{guild_id}/voice-states/@me", guild_id=guild_id)) + + 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)