aboutsummaryrefslogtreecommitdiff
path: root/discord/client.py
diff options
context:
space:
mode:
Diffstat (limited to 'discord/client.py')
-rw-r--r--discord/client.py31
1 files changed, 22 insertions, 9 deletions
diff --git a/discord/client.py b/discord/client.py
index bfa54d53..0ffe73b8 100644
--- a/discord/client.py
+++ b/discord/client.py
@@ -72,6 +72,10 @@ class Client:
-----------
user : Optional[:class:`User`]
Represents the connected client. None if not logged in.
+ voice : Optional[:class:`VoiceClient`]
+ Represents the current voice connection. None if you are not connected
+ to a voice channel. To connect to voice use :meth:`join_voice_channel`.
+ To query the voice connection state use :meth:`is_voice_connected`.
servers : list of :class:`Server`
The servers that the connected client is a member of.
private_channels : list of :class:`PrivateChannel`
@@ -93,6 +97,7 @@ class Client:
self.ws = None
self.token = None
self.gateway = None
+ self.voice = None
self.loop = asyncio.get_event_loop() if loop is None else loop
self._listeners = []
@@ -112,10 +117,6 @@ class Client:
self._closed = False
self._is_logged_in = False
- # this is shared state between Client and VoiceClient
- # could this lead to issues? Not sure. I want to say no.
- self._is_voice_connected = asyncio.Event(loop=self.loop)
-
# These two events correspond to the two events necessary
# for a connection to be made
self._voice_data_found = asyncio.Event(loop=self.loop)
@@ -569,10 +570,15 @@ class Client:
if self._closed:
return
+ if self.is_voice_connected():
+ yield from self.voice.disconnect()
+ self.voice = None
+
yield from self.ws.close()
self.keep_alive.cancel()
self._closed = True
+
@asyncio.coroutine
def start(self, email, password):
"""|coro|
@@ -2010,6 +2016,9 @@ class Client:
Joins a voice channel and creates a :class:`VoiceClient` to
establish your connection to the voice server.
+ After this function is successfully called, :attr:`voice` is
+ set to the returned :class:`VoiceClient`.
+
Parameters
----------
channel : :class:`Channel`
@@ -2032,7 +2041,7 @@ class Client:
A voice client that is fully connected to the voice server.
"""
- if self._is_voice_connected.is_set():
+ if self.is_voice_connected():
raise ClientException('Already connected to a voice channel')
if getattr(channel, 'type', ChannelType.text) != ChannelType.voice:
@@ -2060,7 +2069,6 @@ class Client:
kwargs = {
'user': self.user,
- 'connected': self._is_voice_connected,
'channel': self.voice_channel,
'data': self._voice_data_found.data,
'loop': self.loop,
@@ -2068,6 +2076,11 @@ class Client:
'main_ws': self.ws
}
- result = VoiceClient(**kwargs)
- yield from result.connect()
- return result
+ self.voice = VoiceClient(**kwargs)
+ yield from self.voice.connect()
+ return self.voice
+
+
+ def is_voice_connected(self):
+ """bool : Indicates if we are currently connected to a voice channel."""
+ return self.voice is not None and self.voice.is_connected()