Skip to content

Commit 24d2db0

Browse files
style(pre-commit): auto fixes from pre-commit.com hooks
1 parent f231d72 commit 24d2db0

File tree

6 files changed

+205
-121
lines changed

6 files changed

+205
-121
lines changed

discord/opus.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
if TYPE_CHECKING:
4545
T = TypeVar("T")
46-
APPLICATION_CTL = Literal['audio', 'voip', 'lowdelay']
46+
APPLICATION_CTL = Literal["audio", "voip", "lowdelay"]
4747
BAND_CTL = Literal["narrow", "medium", "wide", "superwide", "full"]
4848
SIGNAL_CTL = Literal["auto", "voice", "music"]
4949

@@ -81,7 +81,7 @@ class ApplicationCtl(TypedDict):
8181
c_int_ptr = ctypes.POINTER(ctypes.c_int)
8282
c_int16_ptr = ctypes.POINTER(ctypes.c_int16)
8383
c_float_ptr = ctypes.POINTER(ctypes.c_float)
84-
OPUS_SILENCE = b'\xf8\xff\xfe'
84+
OPUS_SILENCE = b"\xf8\xff\xfe"
8585

8686
_lib = None
8787

@@ -105,9 +105,9 @@ class DecoderStruct(ctypes.Structure):
105105

106106
# Encoder CTLs
107107
application_ctl: ApplicationCtl = {
108-
'audio': 2049,
109-
'lowdelay': 2051,
110-
'voip': 2048,
108+
"audio": 2049,
109+
"lowdelay": 2051,
110+
"voip": 2048,
111111
}
112112

113113
CTL_SET_BITRATE = 4002
@@ -378,22 +378,20 @@ class Encoder(_OpusStruct):
378378
def __init__(
379379
self,
380380
*,
381-
application: APPLICATION_CTL = 'audio',
381+
application: APPLICATION_CTL = "audio",
382382
bitrate: int = 128,
383383
fec: bool = True,
384384
expected_packet_loss: float = 0.15,
385-
bandwidth: BAND_CTL = 'full',
386-
signal_type: SIGNAL_TL = 'auto',
385+
bandwidth: BAND_CTL = "full",
386+
signal_type: SIGNAL_TL = "auto",
387387
) -> None:
388388
if application not in application_ctl:
389-
raise ValueError(
390-
'invalid application ctl type provided'
391-
)
389+
raise ValueError("invalid application ctl type provided")
392390
if not 16 <= bitrate <= 512:
393-
raise ValueError('bitrate must be between 16 and 512, both included')
391+
raise ValueError("bitrate must be between 16 and 512, both included")
394392
if not 0 < expected_packet_loss <= 1:
395393
raise ValueError(
396-
'expected_packet_loss must be between 0 and 1, including 1',
394+
"expected_packet_loss must be between 0 and 1, including 1",
397395
)
398396

399397
_OpusStruct.get_opus_version()

discord/sinks/core.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ def __init__(self, data: bytes, client: VoiceClient):
128128
self.header: bytes = data[:cutoff]
129129
self.data = self.data[cutoff:]
130130

131-
self.decrypted_data: bytes = getattr(self.client, f"_decrypt_{self.client.mode}")(
132-
self.header, self.data
133-
)
131+
self.decrypted_data: bytes = getattr(
132+
self.client, f"_decrypt_{self.client.mode}"
133+
)(self.header, self.data)
134134
self.decoded_data: bytes | None = None
135135

136136
self.user_id: int | None = None

discord/voice/client.py

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,47 +26,48 @@
2626
from __future__ import annotations
2727

2828
import asyncio
29-
from collections.abc import Callable, Coroutine
3029
import struct
30+
from collections.abc import Callable, Coroutine
3131
from typing import TYPE_CHECKING, Any, Literal, overload
3232

3333
from discord import opus
3434
from discord.errors import ClientException
35+
from discord.sinks import RawData, Sink
3536
from discord.sinks.errors import RecordingException
3637
from discord.utils import MISSING
37-
from discord.sinks import RawData, Sink
3838

3939
from ._types import VoiceProtocol
40-
from .state import VoiceConnectionState
40+
from .player import AudioPlayer
4141
from .recorder import Recorder
4242
from .source import AudioSource
43-
from .player import AudioPlayer
43+
from .state import VoiceConnectionState
4444

4545
if TYPE_CHECKING:
4646
from typing_extensions import ParamSpec
4747

4848
from discord import abc
4949
from discord.client import Client
5050
from discord.guild import Guild, VocalGuildChannel
51-
from discord.state import ConnectionState
52-
from discord.user import ClientUser
51+
from discord.opus import APPLICATION_CTL, BAND_CTL, SIGNAL_CTL, Decoder, Encoder
5352
from discord.raw_models import (
54-
RawVoiceStateUpdateEvent,
5553
RawVoiceServerUpdateEvent,
54+
RawVoiceStateUpdateEvent,
5655
)
56+
from discord.state import ConnectionState
5757
from discord.types.voice import SupportedModes
58-
from discord.opus import Encoder, APPLICATION_CTL, BAND_CTL, SIGNAL_CTL, Decoder
58+
from discord.user import ClientUser
5959

6060
from .gateway import VoiceWebSocket
6161

6262
AfterCallback = Callable[[Exception | None], Any]
63-
P = ParamSpec('P')
63+
P = ParamSpec("P")
6464

6565
has_nacl: bool
6666

6767
try:
6868
import nacl.secret
6969
import nacl.utils
70+
7071
has_nacl = True
7172
except ImportError:
7273
has_nacl = False
@@ -102,9 +103,9 @@ class VoiceClient(VoiceProtocol):
102103
def __init__(self, client: Client, channel: abc.Connectable) -> None:
103104
if not has_nacl:
104105
raise RuntimeError(
105-
'PyNaCl library is needed in order to use voice related features, '
106+
"PyNaCl library is needed in order to use voice related features, "
106107
'you can run "pip install py-cord[voice]" to install all voice-related '
107-
'dependencies.'
108+
"dependencies."
108109
)
109110

110111
super().__init__(client, channel)
@@ -130,10 +131,10 @@ def __init__(self, client: Client, channel: abc.Connectable) -> None:
130131

131132
warn_nacl: bool = not has_nacl
132133
supported_modes: tuple[SupportedModes, ...] = (
133-
'aead_xchacha20_poly1305_rtpsize',
134-
'xsalsa20_poly1305_lite',
135-
'xsalsa20_poly1305_suffix',
136-
'xsalsa20_poly1305',
134+
"aead_xchacha20_poly1305_rtpsize",
135+
"xsalsa20_poly1305_lite",
136+
"xsalsa20_poly1305_suffix",
137+
"xsalsa20_poly1305",
137138
)
138139

139140
@property
@@ -224,7 +225,7 @@ def latency(self) -> float:
224225
.. versionadded:: 1.4
225226
"""
226227
ws = self.ws
227-
return float('inf') if not ws else ws.latency
228+
return float("inf") if not ws else ws.latency
228229

229230
@property
230231
def average_latency(self) -> float:
@@ -233,7 +234,7 @@ def average_latency(self) -> float:
233234
.. versionadded:: 1.4
234235
"""
235236
ws = self.ws
236-
return float('inf') if not ws else ws.average_latency
237+
return float("inf") if not ws else ws.average_latency
237238

238239
async def disconnect(self, *, force: bool = False) -> None:
239240
"""|coro|
@@ -245,7 +246,9 @@ async def disconnect(self, *, force: bool = False) -> None:
245246
await self._connection.disconnect(force=force, wait=True)
246247
self.cleanup()
247248

248-
async def move_to(self, channel: abc.Snowflake | None, *, timeout: float | None = 30.0) -> None:
249+
async def move_to(
250+
self, channel: abc.Snowflake | None, *, timeout: float | None = 30.0
251+
) -> None:
249252
"""|coro|
250253
251254
moves you to a different voice channel.
@@ -285,11 +288,11 @@ def _get_voice_packet(self, data: Any) -> bytes:
285288
# formulate rtp header
286289
header[0] = 0x80
287290
header[1] = 0x78
288-
struct.pack_into('>H', header, 2, self.sequence)
289-
struct.pack_into('>I', header, 4, self.timestamp)
290-
struct.pack_into('>I', header, 8, self.ssrc)
291+
struct.pack_into(">H", header, 2, self.sequence)
292+
struct.pack_into(">I", header, 4, self.timestamp)
293+
struct.pack_into(">I", header, 8, self.ssrc)
291294

292-
encrypt_packet = getattr(self, f'_encrypt_{self.mode}')
295+
encrypt_packet = getattr(self, f"_encrypt_{self.mode}")
293296
return encrypt_packet(header, data)
294297

295298
def _encrypt_xsalsa20_poly1305(self, header: bytes, data: Any) -> bytes:
@@ -309,17 +312,23 @@ def _encrypt_xsalsa20_poly1305_lite(self, header: bytes, data: Any) -> bytes:
309312
# deprecated
310313
box = nacl.secret.SecretBox(bytes(self.secret_key))
311314
nonce = bytearray(24)
312-
nonce[:4] = struct.pack('>I', self._incr_nonce)
313-
self.checked_add('_incr_nonce', 1, 4294967295)
315+
nonce[:4] = struct.pack(">I", self._incr_nonce)
316+
self.checked_add("_incr_nonce", 1, 4294967295)
314317

315318
return header + box.encrypt(bytes(data), bytes(nonce)).ciphertext + nonce[:4]
316319

317-
def _encrypt_aead_xcacha20_poly1305_rtpsize(self, header: bytes, data: Any) -> bytes:
320+
def _encrypt_aead_xcacha20_poly1305_rtpsize(
321+
self, header: bytes, data: Any
322+
) -> bytes:
318323
box = nacl.secret.Aead(bytes(self.secret_key))
319324
nonce = bytearray(24)
320-
nonce[:4] = struct.pack('>I', self._incr_nonce)
321-
self.checked_add('_incr_nonce', 1, 4294967295)
322-
return header + box.encrypt(bytes(data), bytes(header), bytes(nonce)).ciphertext + nonce[:4]
325+
nonce[:4] = struct.pack(">I", self._incr_nonce)
326+
self.checked_add("_incr_nonce", 1, 4294967295)
327+
return (
328+
header
329+
+ box.encrypt(bytes(data), bytes(header), bytes(nonce)).ciphertext
330+
+ nonce[:4]
331+
)
323332

324333
def _decrypt_xsalsa20_poly1305(self, header: bytes, data: Any) -> bytes:
325334
# deprecated
@@ -349,7 +358,9 @@ def _decrypt_xsalsa20_poly1305_lite(self, header: bytes, data: Any) -> bytes:
349358

350359
return self.strip_header_ext(box.decrypt(bytes(data), bytes(nonce)))
351360

352-
def _decrypt_aead_xchacha20_poly1305_rtpsize(self, header: bytes, data: Any) -> bytes:
361+
def _decrypt_aead_xchacha20_poly1305_rtpsize(
362+
self, header: bytes, data: Any
363+
) -> bytes:
353364
box = nacl.secret.Aead(bytes(self.secret_key))
354365

355366
nonce = bytearray(24)
@@ -363,7 +374,7 @@ def _decrypt_aead_xchacha20_poly1305_rtpsize(self, header: bytes, data: Any) ->
363374
@staticmethod
364375
def strip_header_ext(data: bytes) -> bytes:
365376
if len(data) > 4 and data[0] == 0xBE and data[1] == 0xDE:
366-
_, length = struct.unpack_from('>HH', data)
377+
_, length = struct.unpack_from(">HH", data)
367378
offset = 4 + length * 4
368379
data = data[offset:]
369380
return data
@@ -403,12 +414,12 @@ def play(
403414
source: AudioSource,
404415
*,
405416
after: AfterCallback | None = None,
406-
application: APPLICATION_CTL = 'audio',
417+
application: APPLICATION_CTL = "audio",
407418
bitrate: int = 128,
408419
fec: bool = True,
409420
expected_packet_loss: float = 0.15,
410-
bandwidth: BAND_CTL = 'full',
411-
signal_type: SIGNAL_CTL = 'auto',
421+
bandwidth: BAND_CTL = "full",
422+
signal_type: SIGNAL_CTL = "auto",
412423
wait_finish: bool = False,
413424
) -> None | asyncio.Future[None]:
414425
"""Plays an :class:`AudioSource`.
@@ -465,12 +476,12 @@ def play(
465476
"""
466477

467478
if not self.is_connected():
468-
raise ClientException('Not connected to voice')
479+
raise ClientException("Not connected to voice")
469480
if self.is_playing():
470-
raise ClientException('Already playing audio')
481+
raise ClientException("Already playing audio")
471482
if not isinstance(source, AudioSource):
472483
raise TypeError(
473-
f'Source must be an AudioSource, not {source.__class__.__name__}',
484+
f"Source must be an AudioSource, not {source.__class__.__name__}",
474485
)
475486
if not self.encoder and not source.is_opus():
476487
self.encoder = opus.Encoder(
@@ -579,10 +590,12 @@ def start_recording(
579590
"""
580591

581592
if not self.is_connected():
582-
raise RecordingException('Not connected to a voice channel')
593+
raise RecordingException("Not connected to a voice channel")
583594
if self.recording:
584-
raise RecordingException('You are already recording')
595+
raise RecordingException("You are already recording")
585596
if not isinstance(sink, Sink):
586-
raise RecordingException(f'Expected a Sink object, got {sink.__class__.__name__}')
597+
raise RecordingException(
598+
f"Expected a Sink object, got {sink.__class__.__name__}"
599+
)
587600

588601
self._recording_handler.empty()

discord/voice/gateway.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ async def received_message(self, msg: Any, /):
162162
"successfully RESUMED.",
163163
)
164164
elif op == OpCodes.session_description:
165-
self.state.mode = data['mode']
165+
self.state.mode = data["mode"]
166166
elif op == OpCodes.hello:
167167
interval = data["heartbeat_interval"] / 1000.0
168168
self._keep_alive = KeepAliveHandler(
@@ -268,10 +268,10 @@ async def close(self, code: int = 1000) -> None:
268268
async def speak(self, state: SpeakingState = SpeakingState.voice) -> None:
269269
await self.send_as_json(
270270
{
271-
'op': int(OpCodes.speaking),
272-
'd': {
273-
'speaking': int(state),
274-
'delay': 0,
271+
"op": int(OpCodes.speaking),
272+
"d": {
273+
"speaking": int(state),
274+
"delay": 0,
275275
},
276276
},
277277
)
@@ -285,7 +285,7 @@ async def from_state(
285285
hook: Callable[..., Coroutine[Any, Any, Any]] | None = None,
286286
seq_ack: int = -1,
287287
) -> Self:
288-
gateway = f'wss://{state.endpoint}/?v=8'
288+
gateway = f"wss://{state.endpoint}/?v=8"
289289
client = state.client
290290
http = client._state.http
291291
socket = await http.ws_connect(gateway, compress=15)

discord/voice/recorder.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2323
DEALINGS IN THE SOFTWARE.
2424
"""
25+
2526
from __future__ import annotations
2627

2728
# TODO: finish this

0 commit comments

Comments
 (0)