26
26
from __future__ import annotations
27
27
28
28
import asyncio
29
- from collections .abc import Callable , Coroutine
30
29
import struct
30
+ from collections .abc import Callable , Coroutine
31
31
from typing import TYPE_CHECKING , Any , Literal , overload
32
32
33
33
from discord import opus
34
34
from discord .errors import ClientException
35
+ from discord .sinks import RawData , Sink
35
36
from discord .sinks .errors import RecordingException
36
37
from discord .utils import MISSING
37
- from discord .sinks import RawData , Sink
38
38
39
39
from ._types import VoiceProtocol
40
- from .state import VoiceConnectionState
40
+ from .player import AudioPlayer
41
41
from .recorder import Recorder
42
42
from .source import AudioSource
43
- from .player import AudioPlayer
43
+ from .state import VoiceConnectionState
44
44
45
45
if TYPE_CHECKING :
46
46
from typing_extensions import ParamSpec
47
47
48
48
from discord import abc
49
49
from discord .client import Client
50
50
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
53
52
from discord .raw_models import (
54
- RawVoiceStateUpdateEvent ,
55
53
RawVoiceServerUpdateEvent ,
54
+ RawVoiceStateUpdateEvent ,
56
55
)
56
+ from discord .state import ConnectionState
57
57
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
59
59
60
60
from .gateway import VoiceWebSocket
61
61
62
62
AfterCallback = Callable [[Exception | None ], Any ]
63
- P = ParamSpec ('P' )
63
+ P = ParamSpec ("P" )
64
64
65
65
has_nacl : bool
66
66
67
67
try :
68
68
import nacl .secret
69
69
import nacl .utils
70
+
70
71
has_nacl = True
71
72
except ImportError :
72
73
has_nacl = False
@@ -102,9 +103,9 @@ class VoiceClient(VoiceProtocol):
102
103
def __init__ (self , client : Client , channel : abc .Connectable ) -> None :
103
104
if not has_nacl :
104
105
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, "
106
107
'you can run "pip install py-cord[voice]" to install all voice-related '
107
- ' dependencies.'
108
+ " dependencies."
108
109
)
109
110
110
111
super ().__init__ (client , channel )
@@ -130,10 +131,10 @@ def __init__(self, client: Client, channel: abc.Connectable) -> None:
130
131
131
132
warn_nacl : bool = not has_nacl
132
133
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" ,
137
138
)
138
139
139
140
@property
@@ -224,7 +225,7 @@ def latency(self) -> float:
224
225
.. versionadded:: 1.4
225
226
"""
226
227
ws = self .ws
227
- return float (' inf' ) if not ws else ws .latency
228
+ return float (" inf" ) if not ws else ws .latency
228
229
229
230
@property
230
231
def average_latency (self ) -> float :
@@ -233,7 +234,7 @@ def average_latency(self) -> float:
233
234
.. versionadded:: 1.4
234
235
"""
235
236
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
237
238
238
239
async def disconnect (self , * , force : bool = False ) -> None :
239
240
"""|coro|
@@ -245,7 +246,9 @@ async def disconnect(self, *, force: bool = False) -> None:
245
246
await self ._connection .disconnect (force = force , wait = True )
246
247
self .cleanup ()
247
248
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 :
249
252
"""|coro|
250
253
251
254
moves you to a different voice channel.
@@ -285,11 +288,11 @@ def _get_voice_packet(self, data: Any) -> bytes:
285
288
# formulate rtp header
286
289
header [0 ] = 0x80
287
290
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 )
291
294
292
- encrypt_packet = getattr (self , f' _encrypt_{ self .mode } ' )
295
+ encrypt_packet = getattr (self , f" _encrypt_{ self .mode } " )
293
296
return encrypt_packet (header , data )
294
297
295
298
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:
309
312
# deprecated
310
313
box = nacl .secret .SecretBox (bytes (self .secret_key ))
311
314
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 )
314
317
315
318
return header + box .encrypt (bytes (data ), bytes (nonce )).ciphertext + nonce [:4 ]
316
319
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 :
318
323
box = nacl .secret .Aead (bytes (self .secret_key ))
319
324
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
+ )
323
332
324
333
def _decrypt_xsalsa20_poly1305 (self , header : bytes , data : Any ) -> bytes :
325
334
# deprecated
@@ -349,7 +358,9 @@ def _decrypt_xsalsa20_poly1305_lite(self, header: bytes, data: Any) -> bytes:
349
358
350
359
return self .strip_header_ext (box .decrypt (bytes (data ), bytes (nonce )))
351
360
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 :
353
364
box = nacl .secret .Aead (bytes (self .secret_key ))
354
365
355
366
nonce = bytearray (24 )
@@ -363,7 +374,7 @@ def _decrypt_aead_xchacha20_poly1305_rtpsize(self, header: bytes, data: Any) ->
363
374
@staticmethod
364
375
def strip_header_ext (data : bytes ) -> bytes :
365
376
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 )
367
378
offset = 4 + length * 4
368
379
data = data [offset :]
369
380
return data
@@ -403,12 +414,12 @@ def play(
403
414
source : AudioSource ,
404
415
* ,
405
416
after : AfterCallback | None = None ,
406
- application : APPLICATION_CTL = ' audio' ,
417
+ application : APPLICATION_CTL = " audio" ,
407
418
bitrate : int = 128 ,
408
419
fec : bool = True ,
409
420
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" ,
412
423
wait_finish : bool = False ,
413
424
) -> None | asyncio .Future [None ]:
414
425
"""Plays an :class:`AudioSource`.
@@ -465,12 +476,12 @@ def play(
465
476
"""
466
477
467
478
if not self .is_connected ():
468
- raise ClientException (' Not connected to voice' )
479
+ raise ClientException (" Not connected to voice" )
469
480
if self .is_playing ():
470
- raise ClientException (' Already playing audio' )
481
+ raise ClientException (" Already playing audio" )
471
482
if not isinstance (source , AudioSource ):
472
483
raise TypeError (
473
- f' Source must be an AudioSource, not { source .__class__ .__name__ } ' ,
484
+ f" Source must be an AudioSource, not { source .__class__ .__name__ } " ,
474
485
)
475
486
if not self .encoder and not source .is_opus ():
476
487
self .encoder = opus .Encoder (
@@ -579,10 +590,12 @@ def start_recording(
579
590
"""
580
591
581
592
if not self .is_connected ():
582
- raise RecordingException (' Not connected to a voice channel' )
593
+ raise RecordingException (" Not connected to a voice channel" )
583
594
if self .recording :
584
- raise RecordingException (' You are already recording' )
595
+ raise RecordingException (" You are already recording" )
585
596
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
+ )
587
600
588
601
self ._recording_handler .empty ()
0 commit comments