From 3436792614d6478e25e43a717c4f393a16dd43bb Mon Sep 17 00:00:00 2001 From: Rapptz Date: Sat, 20 May 2017 15:19:47 -0400 Subject: [PATCH] Allow setting a presence upon logging in. --- discord/client.py | 4 ++++ discord/gateway.py | 9 +++++++++ discord/state.py | 17 ++++++++++++++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/discord/client.py b/discord/client.py index 6c3422f4c..1309fa8bb 100644 --- a/discord/client.py +++ b/discord/client.py @@ -92,6 +92,10 @@ class Client: members from the guilds the bot belongs to. If this is ``False``\, then no offline members are received and :meth:`request_offline_members` must be used to fetch the offline members of the guild. + game: Optional[:class:`Game`] + A game to start your presence with upon logging on to Discord. + status: Optional[:class:`Status`] + A status to start your presence with upon logging on to Discord. Attributes ----------- diff --git a/discord/gateway.py b/discord/gateway.py index 56d26c026..8cc13f044 100644 --- a/discord/gateway.py +++ b/discord/gateway.py @@ -275,6 +275,15 @@ class DiscordWebSocket(websockets.client.WebSocketClientProtocol): if self.shard_id is not None and self.shard_count is not None: payload['d']['shard'] = [self.shard_id, self.shard_count] + state = self._connection + if state._game is not None or state._status is not None: + payload['d']['presence'] = { + 'status': state._status, + 'game': state._game, + 'since': 0, + 'afk': False + } + yield from self.send_as_json(payload) log.info('Shard ID %s has sent the IDENTIFY payload.', self.shard_id) diff --git a/discord/state.py b/discord/state.py index 0f41769b0..1871055fd 100644 --- a/discord/state.py +++ b/discord/state.py @@ -32,7 +32,7 @@ from .relationship import Relationship from .channel import * from .member import Member from .role import Role -from .enums import ChannelType, try_enum +from .enums import ChannelType, try_enum, Status from .calls import GroupCall from . import utils, compat @@ -64,6 +64,21 @@ class ConnectionState: self._ready_task = None self._fetch_offline = options.get('fetch_offline_members', True) self._listeners = [] + + game = options.get('game', None) + if game: + game = dict(game) + + status = options.get('status', None) + if status: + if status is Status.offline: + status = 'invisible' + else: + status = str(status) + + self._game = game + self._status = status + self.clear() def clear(self):