From 4bc6625739ba5166d26e927489f2c8317c8e9016 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Sun, 8 Jan 2017 01:51:10 -0500 Subject: [PATCH] Add AutoShardedClient.change_presence. --- discord/client.py | 11 +++++++++ discord/gateway.py | 17 +------------ discord/shard.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 16 deletions(-) diff --git a/discord/client.py b/discord/client.py index f8f458708..7f18effd2 100644 --- a/discord/client.py +++ b/discord/client.py @@ -1006,13 +1006,24 @@ class Client: if status is None: status = 'online' + status_enum = Status.online elif status is Status.offline: status = 'invisible' + status_enum = Status.offline else: + status_enum = status status = str(status) yield from self.ws.change_presence(game=game, status=status, afk=afk) + for guild in self.connection.guilds: + me = guild.me + if me is None: + continue + + me.game = game + me.status = status_enum + # Invite management def _fill_invite_data(self, data): diff --git a/discord/gateway.py b/discord/gateway.py index 8180f4ec3..3d9226605 100644 --- a/discord/gateway.py +++ b/discord/gateway.py @@ -412,13 +412,10 @@ class DiscordWebSocket(websockets.client.WebSocketClientProtocol): raise ConnectionClosed(e, shard_id=self.shard_id) from e @asyncio.coroutine - def change_presence(self, *, game=None, status=None, afk=False, since=0.0, idle=None): + def change_presence(self, *, game=None, status=None, afk=False, since=0.0): if game is not None and not isinstance(game, Game): raise InvalidArgument('game must be of type Game or None') - if idle: - status = 'idle' - if status == 'idle': since = int(time.time() * 1000) @@ -438,18 +435,6 @@ class DiscordWebSocket(websockets.client.WebSocketClientProtocol): log.debug('Sending "{}" to change status'.format(sent)) yield from self.send(sent) - status_enum = try_enum(Status, status) - if status_enum is Status.invisible: - status_enum = Status.offline - - for guild in self._connection.guilds: - me = guild.me - if me is None: - continue - - me.game = game - me.status = status_enum - @asyncio.coroutine def request_sync(self, guild_ids): payload = { diff --git a/discord/shard.py b/discord/shard.py index df0973b34..53e907851 100644 --- a/discord/shard.py +++ b/discord/shard.py @@ -29,6 +29,7 @@ from .client import Client from .gateway import * from .errors import ConnectionClosed from . import compat +from .enums import Status import asyncio import logging @@ -224,3 +225,62 @@ class AutoShardedClient(Client): yield from self.http.close() self._closed.set() self._is_ready.clear() + + @asyncio.coroutine + def change_presence(self, *, game=None, status=None, afk=False, shard_id=None): + """|coro| + + Changes the client's presence. + + The game parameter is a Game object (not a string) that represents + a game being played currently. + + Parameters + ---------- + game: Optional[:class:`Game`] + The game being played. None if no game is being played. + status: Optional[:class:`Status`] + Indicates what status to change to. If None, then + :attr:`Status.online` is used. + afk: bool + Indicates if you are going AFK. This allows the discord + client to know how to handle push notifications better + for you in case you are actually idle and not lying. + shard_id: Optional[int] + The shard_id to change the presence to. If not specified + or ``None``, then it will change the presence of every + shard the bot can see. + + Raises + ------ + InvalidArgument + If the ``game`` parameter is not :class:`Game` or None. + """ + + if status is None: + status = 'online' + status_enum = Status.online + elif status is Status.offline: + status = 'invisible' + status_enum = Status.offline + else: + status_enum = status + status = str(status) + + if shard_id is None: + for shard in self.shards.values(): + yield from shard.ws.change_presence(game=game, status=status, afk=afk) + + guilds = self.connection.guilds + else: + shard = self.shards[shard_id] + yield from shard.ws.change_presence(game=game, status=status, afk=afk) + guilds = [g for g in self.connection.guilds if g.shard_id == shard_id] + + for guild in guilds: + me = guild.me + if me is None: + continue + + me.game = game + me.status = status_enum