aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRapptz <[email protected]>2017-01-08 01:51:10 -0500
committerRapptz <[email protected]>2017-01-08 01:51:10 -0500
commit4bc6625739ba5166d26e927489f2c8317c8e9016 (patch)
tree403063a7c4226e7a1f60eec456e24fe73e891c49
parentChange the way shards are launched in AutoShardedClient. (diff)
downloaddiscord.py-4bc6625739ba5166d26e927489f2c8317c8e9016.tar.xz
discord.py-4bc6625739ba5166d26e927489f2c8317c8e9016.zip
Add AutoShardedClient.change_presence.
-rw-r--r--discord/client.py11
-rw-r--r--discord/gateway.py17
-rw-r--r--discord/shard.py60
3 files changed, 72 insertions, 16 deletions
diff --git a/discord/client.py b/discord/client.py
index f8f45870..7f18effd 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 8180f4ec..3d922660 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 df0973b3..53e90785 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