Split Game object to separate Activity subtypes for Rich Presences.

This is a massive breaking change.

* All references to "game" have been renamed to "activity"
* Activity objects contain a majority of the rich presence information
* Game and Streaming are subtypes for memory optimisation purposes for
  the more common cases.
* Introduce a more specialised read-only type, Spotify, for the
  official Spotify integration to make it easier to use.
This commit is contained in:
Rapptz
2018-03-05 11:01:46 -05:00
parent 2f562bd695
commit f8f8f418f3
12 changed files with 708 additions and 150 deletions

View File

@ -30,7 +30,7 @@ import websockets
import asyncio
from . import utils, compat
from .game import Game
from .activity import create_activity, _ActivityTag
from .errors import ConnectionClosed, InvalidArgument
import logging
import zlib, json
@ -283,10 +283,10 @@ class DiscordWebSocket(websockets.client.WebSocketClientProtocol):
payload['d']['shard'] = [self.shard_id, self.shard_count]
state = self._connection
if state._game is not None or state._status is not None:
if state._activity is not None or state._status is not None:
payload['d']['presence'] = {
'status': state._status,
'game': state._game,
'game': state._activity,
'since': 0,
'afk': False
}
@ -469,19 +469,19 @@ 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):
if game is not None and not isinstance(game, Game):
raise InvalidArgument('game must be of type Game or None')
def change_presence(self, *, activity=None, status=None, afk=False, since=0.0):
if activity is not None:
if not isinstance(activity, _ActivityTag):
raise InvalidArgument('activity must be one of Game, Streaming, or Activity.')
activity = activity.to_dict()
if status == 'idle':
since = int(time.time() * 1000)
sent_game = dict(game) if game else None
payload = {
'op': self.PRESENCE,
'd': {
'game': sent_game,
'game': activity,
'afk': afk,
'since': since,
'status': status