Skip to content

Commit 9faa308

Browse files
committed
refactor(types): make VoiceRegion a dataclass
1 parent 0a47380 commit 9faa308

3 files changed

Lines changed: 31 additions & 14 deletions

File tree

discord/guild.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
from .stage_instance import StageInstance
9292
from .sticker import GuildSticker
9393
from .threads import Thread, ThreadMember
94+
from .types.voice import VoiceRegion as VoiceRegionData
9495
from .user import User
9596
from .utils import _D, _FETCHABLE
9697
from .welcome_screen import WelcomeScreen, WelcomeScreenChannel
@@ -121,7 +122,6 @@
121122
from .types.guild import ModifyIncidents as ModifyIncidentsPayload
122123
from .types.member import Member as MemberPayload
123124
from .types.threads import Thread as ThreadPayload
124-
from .types.voice import VoiceRegion as VoiceRegionPayload
125125
from .types.voice import VoiceState as GuildVoiceState
126126
from .voice import VoiceClient
127127
from .webhook import Webhook
@@ -3797,7 +3797,7 @@ async def vanity_invite(self) -> Invite | None:
37973797
payload["uses"] = payload.get("uses", 0)
37983798
return Invite(state=self._state, data=payload, guild=self, channel=channel)
37993799

3800-
async def fetch_voice_regions(self) -> list[VoiceRegionPayload]:
3800+
async def fetch_voice_regions(self) -> list[VoiceRegionData]:
38013801
"""|coro|
38023802
38033803
Retrieves the voice regions that the guild has access to.
@@ -3806,19 +3806,18 @@ async def fetch_voice_regions(self) -> list[VoiceRegionPayload]:
38063806
recommended way to get the currently available regions.
38073807
.. versionadded:: 2.9
38083808
3809-
Each payload is a :class:`~discord.types.voice.VoiceRegion` TypedDict
3810-
with the following keys:
3809+
Each :class:`~discord.types.voice.VoiceRegion` has the following attributes:
38113810
3812-
``id``
3811+
:attr:`~discord.types.voice.VoiceRegion.id`
38133812
The region ID, e.g. ``"us-west"``. Use this as the
38143813
:attr:`~discord.VoiceChannel.rtc_region` of a voice channel.
3815-
``name``
3814+
:attr:`~discord.types.voice.VoiceRegion.name`
38163815
The region's display name, e.g. ``"US West"``.
3817-
``optimal``
3816+
:attr:`~discord.types.voice.VoiceRegion.optimal`
38183817
Whether the region is optimal for the guild's members.
3819-
``deprecated``
3818+
:attr:`~discord.types.voice.VoiceRegion.deprecated`
38203819
Whether the region is deprecated.
3821-
``custom``
3820+
:attr:`~discord.types.voice.VoiceRegion.custom`
38223821
Whether the region is a custom region.
38233822
38243823
Returns
@@ -3831,7 +3830,8 @@ async def fetch_voice_regions(self) -> list[VoiceRegionPayload]:
38313830
HTTPException
38323831
Retrieving the voice regions failed.
38333832
"""
3834-
return await self._state.http.get_guild_voice_regions(self.id)
3833+
regions = await self._state.http.get_guild_voice_regions(self.id)
3834+
return [VoiceRegionData(**region) for region in regions]
38353835

38363836
# TODO: use MISSING when async iterators get refactored
38373837
def audit_logs(

discord/http.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,13 +1045,14 @@ def guild_voice_state(
10451045
return self.request(r, json=payload, reason=reason)
10461046

10471047
def get_guild_voice_regions(
1048-
self, guild_id: Snowflake
1049-
) -> Response[list[voice.VoiceRegion]]:
1048+
self,
1049+
guild_id: Snowflake,
1050+
) -> Response[list[voice.VoiceRegionPayload]]:
10501051
return self.request(
10511052
Route("GET", "/guilds/{guild_id}/regions", guild_id=guild_id)
10521053
)
10531054

1054-
def get_voice_regions(self) -> Response[list[voice.VoiceRegion]]:
1055+
def get_voice_regions(self) -> Response[list[voice.VoiceRegionPayload]]:
10551056
return self.request(Route("GET", "/voice/regions"))
10561057

10571058
def edit_profile(self, payload: dict[str, Any]) -> Response[user.User]:

discord/types/voice.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
from __future__ import annotations
2727

28+
from dataclasses import dataclass
2829
from typing import Literal
2930

3031
from typing_extensions import NotRequired, TypedDict
@@ -59,7 +60,22 @@ class VoiceState(TypedDict):
5960
GuildVoiceState = VoiceState
6061

6162

62-
class VoiceRegion(TypedDict):
63+
class VoiceRegionPayload(TypedDict):
64+
id: str
65+
name: str
66+
vip: bool
67+
optimal: bool
68+
deprecated: bool
69+
custom: bool
70+
71+
72+
@dataclass(frozen=True, slots=True)
73+
class VoiceRegion:
74+
"""Represents a voice region a guild can use for voice channels.
75+
76+
This is returned by :meth:`Guild.fetch_voice_regions`.
77+
"""
78+
6379
id: str
6480
name: str
6581
vip: bool

0 commit comments

Comments
 (0)