aboutsummaryrefslogtreecommitdiff
path: root/discord
diff options
context:
space:
mode:
authorRapptz <[email protected]>2015-08-24 00:00:42 -0400
committerRapptz <[email protected]>2015-08-24 00:00:42 -0400
commit9f601a24b158228c1fa5727121af26fef2a31905 (patch)
tree3b98ffbeeafe86588a754ce38622e5f6d1c9cf91 /discord
parentAdd support for channel creation events. (diff)
downloaddiscord.py-9f601a24b158228c1fa5727121af26fef2a31905.tar.xz
discord.py-9f601a24b158228c1fa5727121af26fef2a31905.zip
Add logout support and on_disconnect event.
Diffstat (limited to 'discord')
-rw-r--r--discord/client.py15
1 files changed, 13 insertions, 2 deletions
diff --git a/discord/client.py b/discord/client.py
index 714fd045..076c8c36 100644
--- a/discord/client.py
+++ b/discord/client.py
@@ -110,7 +110,6 @@ class Client(object):
self.ws.opened = self._opened
self.ws.closed = self._closed
self.ws.received_message = self._received_message
- self.ws.connect()
# the actual headers for the request...
# we only override 'authorization' since the rest could use the defaults.
@@ -236,6 +235,7 @@ class Client(object):
def _closed(self, code, reason=None):
print('Closed with {} ("{}") at {}'.format(code, reason, int(time.time())))
+ self._invoke_event('on_disconnect')
def run(self):
"""Runs the client and allows it to receive messages and events."""
@@ -369,7 +369,8 @@ class Client(object):
def login(self, email, password):
- """Logs in the user with the following credentials.
+ """Logs in the user with the following credentials and initialises
+ the connection to Discord.
After this function is called, :attr:`is_logged_in` returns True if no
errors occur.
@@ -378,6 +379,8 @@ class Client(object):
:param str password: The password used to login.
"""
+ self.ws.connect()
+
payload = {
'email': email,
'password': password
@@ -407,6 +410,14 @@ class Client(object):
self.ws.send(json.dumps(second_payload))
self._is_logged_in = True
+ def logout(self):
+ """Logs out of Discord and closes all connections."""
+ response = requests.post(endpoints.LOGOUT)
+ self.ws.close()
+ self._is_logged_in = False
+ self.keep_alive.cancel()
+
+
def logs_from(self, channel, limit=500):
"""A generator that obtains logs from a specified channel.