2626from __future__ import annotations
2727
2828import asyncio
29- from collections .abc import Callable , Coroutine
3029import struct
30+ from collections .abc import Callable , Coroutine
3131from typing import TYPE_CHECKING , Any , Literal , overload
3232
3333from discord import opus
3434from discord .errors import ClientException
35+ from discord .sinks import RawData , Sink
3536from discord .sinks .errors import RecordingException
3637from discord .utils import MISSING
37- from discord .sinks import RawData , Sink
3838
3939from ._types import VoiceProtocol
40- from .state import VoiceConnectionState
40+ from .player import AudioPlayer
4141from .recorder import Recorder
4242from .source import AudioSource
43- from .player import AudioPlayer
43+ from .state import VoiceConnectionState
4444
4545if 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
6565has_nacl : bool
6666
6767try :
6868 import nacl .secret
6969 import nacl .utils
70+
7071 has_nacl = True
7172except 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 ()
0 commit comments