Browse Source

Fix VC playing, remove extraneous properties, fix typing issues

pull/10109/head
dolfies 3 years ago
parent
commit
77a8ad30d2
  1. 12
      discord/player.py
  2. 38
      discord/voice_client.py

12
discord/player.py

@ -44,7 +44,7 @@ from .oggparse import OggStream
from .utils import MISSING
if TYPE_CHECKING:
from .voice_client import VoiceClient
from .voice_client import Player
AT = TypeVar('AT', bound='AudioSource')
@ -151,7 +151,7 @@ class FFmpegAudio(AudioSource):
self._process: subprocess.Popen = self._spawn_process(args, **kwargs)
self._stdout: IO[bytes] = self._process.stdout # type: ignore
self._stdin: Optional[IO[Bytes]] = None
self._stdin: Optional[IO[bytes]] = None
self._pipe_thread: Optional[threading.Thread] = None
if piping:
@ -616,18 +616,18 @@ class PCMVolumeTransformer(AudioSource, Generic[AT]):
class AudioPlayer(threading.Thread):
DELAY: float = OpusEncoder.FRAME_LENGTH / 1000.0
def __init__(self, source: AudioSource, client: VoiceClient, *, after=None):
def __init__(self, source: AudioSource, client: Player, *, after=None):
threading.Thread.__init__(self)
self.daemon: bool = True
self.source: AudioSource = source
self.client: VoiceClient = client
self.client: Player = client
self.after: Optional[Callable[[Optional[Exception]], Any]] = after
self._end: threading.Event = threading.Event()
self._resumed: threading.Event = threading.Event()
self._resumed.set() # we are not paused
self._current_error: Optional[Exception] = None
self._connected: threading.Event = client._connected
self._connected: threading.Event = client.client._connected
self._lock: threading.Lock = threading.Lock()
if after is not None and not callable(after):
@ -638,7 +638,7 @@ class AudioPlayer(threading.Thread):
self._start = time.perf_counter()
# getattr lookup speed ups
play_audio = self.client.send_audio_packet
play_audio = self.client.send
self._speak(True)
while not self._end.is_set():

38
discord/voice_client.py

@ -249,18 +249,10 @@ class Player:
self._player._set_source(value)
@property
def playing(self) -> bool:
return self.is_playing()
def is_playing(self) -> bool:
"""Indicates if we're currently playing audio."""
return self._player and self._player.is_playing()
@property
def paused(self) -> bool:
return self.is_paused()
def is_paused(self) -> bool:
"""Indicates if we're playing audio, but if we're paused."""
return self._player and self._player.is_paused()
@ -307,7 +299,7 @@ class Player:
if not self.encoder and not source.is_opus():
self.encoder = opus.Encoder()
self._player = AudioPlayer(source, self.client, after=after)
self._player = AudioPlayer(source, self, after=after)
self._player.start()
def pause(self) -> None:
@ -324,7 +316,7 @@ class Player:
"""Stops playing audio."""
if self._player:
self._player.stop()
self._player = None
self._player = MISSING
class Listener:
@ -339,18 +331,10 @@ class Listener:
def ws(self) -> DiscordVoiceWebSocket:
return self.client.ws
@property
def listening(self) -> bool:
return self.is_playing()
def is_listening(self) -> bool:
"""Indicates if we're currently listening."""
return self._listener is not None and self._listener.is_listening()
@property
def paused(self) -> bool:
return self.is_paused()
def is_paused(self) -> bool:
"""Indicates if we're listening, but we're paused."""
return self._listener is not None and self._listener.is_paused()
@ -437,7 +421,7 @@ class VoiceClient(VoiceProtocol):
@property
def ssrc(self) -> int:
""":class:`str`: Our ssrc."""
return self.idrcs.get(self.user.id)
return self.idrcs.get(self.user.id) # type: ignore
@ssrc.setter
def ssrc(self, value):
@ -452,7 +436,7 @@ class VoiceClient(VoiceProtocol):
@property
def user(self) -> ClientUser:
""":class:`ClientUser`: The user connected to voice (i.e. ourselves)."""
return self._state.user
return self._state.user # type: ignore
# Connection related
@ -481,7 +465,8 @@ class VoiceClient(VoiceProtocol):
_log.info('Ignoring extraneous voice server update.')
return
self.token = data.get('token')
if 'token' in data:
self.token = data['token']
self.server_id = server_id = utils._get_as_snowflake(data, 'guild_id')
if server_id is None:
self.server_id = utils._get_as_snowflake(data, 'channel_id')
@ -509,13 +494,14 @@ class VoiceClient(VoiceProtocol):
self._voice_server_complete.set()
async def voice_connect(self) -> None:
channel = await self.channel._get_channel() if self.channel else None
if self.guild:
await self.guild.change_voice_state(channel=self.channel)
await self.guild.change_voice_state(channel=channel)
else:
await self._state.client.change_voice_state(channel=self.channel)
await self._state.client.change_voice_state(channel=channel)
async def voice_disconnect(self) -> None:
_log.info('The voice handshake is being terminated for channel ID %s (guild ID %s).', self.channel.id, getattr(self.guild, 'id', None))
_log.info('The voice handshake is being terminated for channel ID %s (guild ID %s).', (await self.channel._get_channel()).id, getattr(self.guild, 'id', None))
if self.guild:
await self.guild.change_voice_state(channel=None)
else:
@ -720,10 +706,6 @@ class VoiceClient(VoiceProtocol):
else:
await self._state.client.change_voice_state(channel=channel)
@property
def connected(self) -> bool:
return self.is_connected()
def is_connected(self) -> bool:
"""Indicates if the voice client is connected to voice."""
return self._connected.is_set()

Loading…
Cancel
Save