Add AutoShardedClient.change_presence.

This commit is contained in:
Rapptz 2017-01-08 01:51:10 -05:00
parent b5bed9ef33
commit 4bc6625739
3 changed files with 72 additions and 16 deletions

View File

@ -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):

View File

@ -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 = {

View File

@ -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