Skip to content

Commit 1ded164

Browse files
committed
feat: Add a method to Client to retrieve all voice regions from the API
Signed-off-by: Mathieu C <McCuber04@outlook.de> Signed-off-by: Mathieu Corsham <McCuber04@outlook.de>
1 parent ab37c70 commit 1ded164

File tree

7 files changed

+156
-4
lines changed

7 files changed

+156
-4
lines changed

discord/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
from .player import *
5858
from .webhook import *
5959
from .welcome_screen import *
60-
from .voice_client import VoiceClient, VoiceProtocol
60+
from .voice_client import *
6161
from .sink import *
6262
from .audit_logs import AuditLogChanges, AuditLogEntry, AuditLogDiff
6363
from .raw_models import *
@@ -67,6 +67,7 @@
6767
from .automod import *
6868

6969
MISSING = utils.MISSING
70+
Mentionable = abc.Mentionable
7071

7172
VersionInfo = namedtuple('VersionInfo', 'major minor micro releaselevel serial')
7273

discord/application_commands.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
try_enum
6060
)
6161
from .permissions import Permissions
62+
from .abc import GuildChannel
6263

6364
if TYPE_CHECKING:
6465
from datetime import datetime

discord/asset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
"""
2626
from __future__ import annotations
2727

28+
import io
29+
2830
from typing import (
2931
BinaryIO,
3032
Optional,
@@ -53,8 +55,6 @@
5355
HAS_HTTP_CONNECTION = Union[ConnectionState, OAuth2Client]
5456

5557

56-
import io
57-
5858
from .errors import DiscordException, InvalidArgument
5959
from . import utils
6060

discord/client.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
from .enums import Status, VoiceRegion
6767
from .gateway import *
6868
from .activity import BaseActivity, create_activity
69-
from .voice_client import VoiceClient
69+
from .voice_client import VoiceRegionInfo, VoiceClient
7070
from .http import HTTPClient
7171
from .state import ConnectionState
7272
from . import utils
@@ -2805,3 +2805,22 @@ async def fetch_all_nitro_stickers(self) -> List[StickerPack]:
28052805
data = await self.http.get_all_nitro_stickers()
28062806
packs = [StickerPack(state=self._connection, data=d) for d in data['sticker_packs']]
28072807
return packs
2808+
2809+
async def fetch_voice_regions(self) -> List[VoiceRegionInfo]:
2810+
"""|coro|
2811+
2812+
Returns a list of :class:`.VoiceRegionInfo` that can be used when creating or editing a
2813+
:attr:`VoiceChannel` or :attr:`StageChannel`\'s region.
2814+
2815+
.. note::
2816+
2817+
This method is an API call.
2818+
For general usage, consider using the :class:`VoiceRegion` enum instead.
2819+
2820+
Returns
2821+
--------
2822+
List[:class:`.VoiceRegionInfo`]
2823+
The voice regions that can be used.
2824+
"""
2825+
data = await self.http.get_voice_regions()
2826+
return [VoiceRegionInfo(data=d) for d in data]

discord/types/voice.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2021-present mccoderpy
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a
7+
copy of this software and associated documentation files (the "Software"),
8+
to deal in the Software without restriction, including without limitation
9+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
10+
and/or sell copies of the Software, and to permit persons to whom the
11+
Software is furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22+
DEALINGS IN THE SOFTWARE.
23+
"""
24+
25+
from __future__ import annotations
26+
27+
from typing_extensions import TypedDict
28+
29+
30+
class VoiceRegion(TypedDict):
31+
id: str
32+
name: str
33+
vip: bool
34+
optimal: bool
35+
deprecated: bool
36+
custom: bool

discord/voice_client.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2626
DEALINGS IN THE SOFTWARE.
2727
"""
28+
from __future__ import annotations
2829

2930
"""Some documentation to refer to:
3031
@@ -58,6 +59,11 @@
5859
Tuple
5960
)
6061

62+
if TYPE_CHECKING:
63+
from .types.voice import (
64+
VoiceRegion as VoiceRegionData,
65+
)
66+
6167
from . import opus, utils
6268
from .backoff import ExponentialBackoff
6369
from .gateway import *
@@ -73,6 +79,84 @@
7379

7480
log = logging.getLogger(__name__)
7581

82+
__all__ = (
83+
'VoiceRegionInfo',
84+
'VoiceClient',
85+
'VoiceProtocol',
86+
)
87+
88+
89+
class VoiceRegionInfo:
90+
"""A class containing info about a specific voice region.
91+
92+
These can be retrieved via :meth:`~discord.Client.fetch_voice_regions`.
93+
94+
.. versionadded:: 2.0
95+
96+
Attributes
97+
------------
98+
id: :class:`str`
99+
The unique ID of the region.
100+
name: :class:`str`
101+
The name of the region.
102+
vip: :class:`bool`
103+
Indicates if this is a VIP-only server.
104+
optimal: :class:`bool`
105+
``True`` for a single server that is closest to the current user's client
106+
deprecated: :class:`bool`
107+
Indicates if this is a deprecated voice region (avoid switching to these).
108+
custom: :class:`bool`
109+
Indicates if this is a custom voice region (used for events/etc).
110+
111+
.. container:: operations
112+
113+
.. describe:: x == y
114+
Whether two voice regions are equal.
115+
.. describe:: x != y
116+
Whether two voice regions are not equal.
117+
.. describe:: x > y
118+
Whether a voice region is optimal over another.
119+
.. describe:: x < y
120+
Whether a voice region is not optimal over another.
121+
.. describe:: str(x)
122+
Returns the id of the region.
123+
.. describe:: repr(x)
124+
Returns a representation of the region.
125+
..
126+
"""
127+
def __init__(self, *, data: VoiceRegionData) -> None:
128+
self.id: str = data['id']
129+
self.name: str = data['name']
130+
self.vip: bool = data['vip']
131+
self.optimal: bool = data['optimal']
132+
self.deprecated: bool = data['deprecated']
133+
self.custom: bool = data['custom']
134+
135+
def __repr__(self) -> str:
136+
return f'<VoiceRegion id={self.id} name={self.name!r} vip={self.vip} optimal={self.optimal}' \
137+
f' deprecated={self.deprecated} custom={self.custom}>'
138+
139+
def __eq__(self, other: Any) -> bool:
140+
return isinstance(other, VoiceRegionInfo) and other.id == self.id
141+
142+
def __ne__(self, other: Any) -> bool:
143+
return not self.__eq__(other)
144+
145+
def __gt__(self, other: Any) -> bool:
146+
return isinstance(other, VoiceRegionInfo) and self.optimal and not other.optimal
147+
148+
def __ge__(self, other: Any) -> bool:
149+
return isinstance(other, VoiceRegionInfo) and self.optimal and not other.optimal
150+
151+
def __lt__(self, other: Any) -> bool:
152+
return isinstance(other, VoiceRegionInfo) and not self.optimal and other.optimal
153+
154+
def __le__(self, other: Any) -> bool:
155+
return isinstance(other, VoiceRegionInfo) and not self.optimal and other.optimal
156+
157+
def __str__(self) -> str:
158+
return self.id
159+
76160

77161
class VoiceProtocol:
78162
"""A class that represents the Discord voice protocol.

docs/api.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,9 @@ of :class:`enum.Enum`.
14621462

14631463
Specifies the region a voice server belongs to.
14641464

1465+
.. note::
1466+
If you wan\'t to fetch all currently available voice regions, consider using :meth:`Client.fetch_voice_regions`.
1467+
14651468
.. attribute:: amsterdam
14661469

14671470
The Amsterdam region.
@@ -3683,6 +3686,14 @@ Sticker
36833686
:members:
36843687
:exclude-members: pack, pack_id, sort_value
36853688

3689+
VoiceRegionInfo
3690+
~~~~~~~~~~~~~~~~
3691+
3692+
.. attributetable:: VoiceRegionInfo
3693+
3694+
.. autoclass:: VoiceRegionInfo()
3695+
:members:
3696+
36863697
RawMessageDeleteEvent
36873698
~~~~~~~~~~~~~~~~~~~~~~~
36883699

0 commit comments

Comments
 (0)