Skip to content

Commit 5ffd21e

Browse files
LulalabyDA-344pre-commit-ci[bot]Paillat-dev
authored
refactor(voice): Rewrite Voice Internals & DAVE Support (send) (#3143)
Signed-off-by: DA344 <[email protected]> Signed-off-by: Lala Sabathil <[email protected]> Co-authored-by: DA-344 <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Paillat <[email protected]>
1 parent 3212558 commit 5ffd21e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+5513
-1791
lines changed

.github/workflows/docs-checks.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
- name: Install dependencies
4848
run: |
4949
python -m pip install -U pip
50-
pip install ".[docs]"
50+
pip install ".[docs,voice]"
5151
- name: "Check Links"
5252
if: ${{ github.event_name == 'schedule' || inputs.with_linkcheck }}
5353
run: |

.github/workflows/docs-json-export.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
id: install-deps
2727
run: |
2828
python -m pip install -U pip
29-
pip install ".[docs]"
29+
pip install ".[docs,voice]"
3030
pip install beautifulsoup4
3131
- name: Build Sphinx HTML docs
3232
id: build-sphinx

.github/workflows/lib-checks.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ jobs:
157157
run: |
158158
python -m pip install --upgrade pip
159159
pip install . --group dev
160+
pip install .[voice]
160161
161162
- name: "Run tests"
162163
run: tox

.readthedocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ python:
1717
path: .
1818
extra_requirements:
1919
- docs
20+
- voice

discord/__init__.py

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

2626

2727
from . import abc, opus, sinks, ui, utils
28+
from ._voice_aliases import *
2829
from .activity import *
2930
from .appinfo import *
3031
from .application_role_connection import *
@@ -74,7 +75,6 @@
7475
from .template import *
7576
from .threads import *
7677
from .user import *
77-
from .voice_client import *
7878
from .webhook import *
7979
from .welcome_screen import *
8080
from .widget import *

discord/_voice_aliases.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2015-2021 Rapptz
5+
Copyright (c) 2021-present Pycord Development
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a
8+
copy of this software and associated documentation files (the "Software"),
9+
to deal in the Software without restriction, including without limitation
10+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
11+
and/or sell copies of the Software, and to permit persons to whom the
12+
Software is furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23+
DEALINGS IN THE SOFTWARE.
24+
"""
25+
26+
from typing import TYPE_CHECKING, Any
27+
28+
from typing_extensions import deprecated
29+
30+
if TYPE_CHECKING:
31+
from discord.voice import VoiceClient as VoiceClientC
32+
from discord.voice import VoiceProtocol as VoiceProtocolC
33+
34+
"""
35+
since discord.voice raises an error when importing it without having the
36+
required package (ie davey) installed, we can't import it in __init__ because
37+
that would break the whole library, that is why this file is here.
38+
39+
the error would still be raised, but at least here we have more freedom on how we are typing it
40+
"""
41+
42+
__all__ = ("VoiceProtocol", "VoiceClient")
43+
44+
45+
@deprecated(
46+
"discord.VoiceClient is deprecated in favour of discord.voice.VoiceClient since 2.7 and will be removed in 3.0",
47+
)
48+
def VoiceClient(*args: Any, **kwargs: Any) -> "VoiceClientC":
49+
from discord.voice import VoiceClient as VoiceClientC
50+
51+
return VoiceClientC(*args, **kwargs)
52+
53+
54+
@deprecated(
55+
"discord.VoiceProtocol is deprecated in favour of discord.voice.VoiceProtocol since 2.7 and will be removed in 3.0",
56+
)
57+
def VoiceProtocol(*args: Any, **kwargs: Any) -> "VoiceProtocolC":
58+
from discord.voice import VoiceProtocol as VoiceProtocolC
59+
60+
return VoiceProtocolC(*args, **kwargs)

discord/abc.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
from .scheduled_events import ScheduledEvent
5858
from .sticker import GuildSticker, StickerItem
5959
from .utils import warn_deprecated
60-
from .voice_client import VoiceClient, VoiceProtocol
6160

6261
__all__ = (
6362
"Snowflake",
@@ -69,8 +68,6 @@
6968
"Mentionable",
7069
)
7170

72-
T = TypeVar("T", bound=VoiceProtocol)
73-
7471
if TYPE_CHECKING:
7572
from datetime import datetime
7673

@@ -106,6 +103,10 @@
106103
MessageableChannel = Union[PartialMessageableChannel, GroupChannel]
107104
SnowflakeTime = Union["Snowflake", datetime]
108105

106+
from .voice import VoiceClient, VoiceProtocol
107+
108+
T = TypeVar("T", bound=VoiceProtocol)
109+
109110
MISSING = utils.MISSING
110111

111112

@@ -2003,6 +2004,7 @@ class Connectable(Protocol):
20032004

20042005
__slots__ = ()
20052006
_state: ConnectionState
2007+
id: int
20062008

20072009
def _get_voice_client_key(self) -> tuple[int, str]:
20082010
raise NotImplementedError
@@ -2015,7 +2017,7 @@ async def connect(
20152017
*,
20162018
timeout: float = 60.0,
20172019
reconnect: bool = True,
2018-
cls: Callable[[Client, Connectable], T] = VoiceClient,
2020+
cls: Callable[[Client, Connectable], T] = MISSING,
20192021
) -> T:
20202022
"""|coro|
20212023
@@ -2051,6 +2053,16 @@ async def connect(
20512053
The opus library has not been loaded.
20522054
"""
20532055

2056+
# import directly from _types so if the user does not have davey
2057+
# it won't error here
2058+
from .voice._types import VoiceProtocol
2059+
2060+
if cls is MISSING:
2061+
# if the user passes no cls, then actually import VoiceClient
2062+
from .voice import VoiceClient
2063+
2064+
cls = VoiceClient # pyright: ignore[reportAssignmentType]
2065+
20542066
key_id, _ = self._get_voice_client_key()
20552067
state = self._state
20562068

discord/channel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,7 @@ def _update(
16261626
self, guild: Guild, data: VoiceChannelPayload | StageChannelPayload
16271627
) -> None:
16281628
# This data will always exist
1629-
self.guild = guild
1629+
self.guild: Guild = guild
16301630
self.name: str = data["name"]
16311631
self.category_id: int | None = utils._get_as_snowflake(data, "parent_id")
16321632

discord/client.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
)
4343

4444
import aiohttp
45-
from typing_extensions import deprecated
45+
from typing_extensions import Self, deprecated
4646

4747
from . import utils
4848
from .activity import ActivityTypes, BaseActivity, create_activity
@@ -71,7 +71,7 @@
7171
from .ui.view import BaseView
7272
from .user import ClientUser, User
7373
from .utils import _D, _FETCHABLE, MISSING
74-
from .voice_client import VoiceClient
74+
from .voice.utils.dependencies import warn_if_voice_dependencies_missing
7575
from .webhook import Webhook
7676
from .widget import Widget
7777

@@ -87,7 +87,7 @@
8787
from .soundboard import SoundboardSound
8888
from .threads import Thread
8989
from .ui.item import ViewItem
90-
from .voice_client import VoiceProtocol
90+
from .voice import VoiceProtocol
9191

9292
__all__ = ("Client",)
9393

@@ -282,9 +282,7 @@ def __init__(
282282
self._connection._get_client = lambda: self
283283
self._event_handlers: dict[str, list[Coro]] = {}
284284

285-
if VoiceClient.warn_nacl:
286-
VoiceClient.warn_nacl = False
287-
_log.warning("PyNaCl is not installed, voice will NOT be supported")
285+
warn_if_voice_dependencies_missing()
288286

289287
# Used to hard-reference tasks so they don't get garbage collected (discarded with done_callbacks)
290288
self._tasks = set()
@@ -421,7 +419,7 @@ def private_channels(self) -> list[PrivateChannel]:
421419
return self._connection.private_channels
422420

423421
@property
424-
def voice_clients(self) -> list[VoiceProtocol]:
422+
def voice_clients(self) -> list[VoiceProtocol[Self]]:
425423
"""Represents a list of voice connections.
426424
427425
These are usually :class:`.VoiceClient` instances.

discord/commands/context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
from ..state import ConnectionState
5353
from ..ui import BaseView
5454
from ..user import User
55-
from ..voice_client import VoiceClient
55+
from ..voice import VoiceClient
5656
from ..webhook import WebhookMessage
5757
from .core import ApplicationCommand, Option
5858

0 commit comments

Comments
 (0)