Browse Source

Add game data class, replace game_id.

pull/65/head
Khazhismel 9 years ago
parent
commit
0aa46e6def
  1. 1
      discord/__init__.py
  2. 25
      discord/client.py
  3. 65
      discord/game.py
  4. 11
      discord/member.py
  5. 5
      discord/server.py
  6. 4
      discord/state.py

1
discord/__init__.py

@ -19,6 +19,7 @@ __version__ = '0.10.0-alpha'
from .client import Client
from .user import User
from .game import Game
from .channel import Channel, PrivateChannel
from .server import Server
from .member import Member

25
discord/client.py

@ -27,6 +27,7 @@ DEALINGS IN THE SOFTWARE.
from . import __version__ as library_version
from . import endpoints
from .user import User
from .game import Game
from .channel import Channel, PrivateChannel
from .server import Server
from .message import Message
@ -1391,15 +1392,13 @@ class Client:
self._update_cache(self.email, password)
@asyncio.coroutine
def change_status(self, game_id=None, idle=False):
def change_status(self, game=None, idle=False):
"""|coro|
Changes the client's status.
The game_id parameter is a numeric ID (not a string) that represents
a game being played currently. The list of game_id to actual games changes
constantly and would thus be out of date pretty quickly. An old version of
the game_id database can be seen `here <game_list>`_ to help you get started.
The game parameter is a Game object (not a string) that represents
a game being played currently.
The idle parameter is a boolean parameter that indicates whether the
client should go idle or not.
@ -1408,27 +1407,27 @@ class Client:
Parameters
----------
game_id : Optional[int]
The game ID being played. None if no game is being played.
game : Optional[:class:`Game`]
The game being played. None if no game is being played.
idle : bool
Indicates if the client should go idle.
Raises
------
InvalidArgument
If the ``game_id`` parameter is convertible integer or None.
If the ``game`` parameter is not :class:`Game` or None.
"""
if game is not None and not isinstance(game, Game):
raise InvalidArgument('game must be of Game or None')
idle_since = None if idle == False else int(time.time() * 1000)
try:
game_id = None if game_id is None else int(game_id)
except:
raise InvalidArgument('game_id must be convertible to an integer or None')
game = game and {'name': game.name}
payload = {
'op': 3,
'd': {
'game_id': game_id,
'game': game,
'idle_since': idle_since
}
}

65
discord/game.py

@ -0,0 +1,65 @@
# -*- coding: utf-8 -*-
"""
The MIT License (MIT)
Copyright (c) 2015 Rapptz
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
class Game:
"""Represents a Discord game.
Supported Operations:
+-----------+------------------------------------+
| Operation | Description |
+===========+====================================+
| x == y | Checks if two games are equal. |
+-----------+------------------------------------+
| x != y | Checks if two games are not equal. |
+-----------+------------------------------------+
| hash(x) | Return the games's hash. |
+-----------+------------------------------------+
| str(x) | Returns the games's name. |
+-----------+------------------------------------+
Attributes
-----------
name : str
The game's name.
"""
__slots__ = ['name']
def __init__(self, **kwargs):
self.name = kwargs.get('name')
def __str__(self):
return self.name
def __eq__(self, other):
return isinstance(other, Game) and other.name == self.name
def __ne__(self, other):
return not self.__eq__(other)
def __hash__(self):
return hash(self.name)

11
discord/member.py

@ -25,6 +25,7 @@ DEALINGS IN THE SOFTWARE.
"""
from .user import User
from .game import Game
from .utils import parse_time
from .enums import Status
@ -58,14 +59,14 @@ class Member(User):
status : :class:`Status`
The member's status. There is a chance that the status will be a ``str``
if it is a value that is not recognised by the enumerator.
game_id : int
The game ID that the user is currently playing. Could be None if no game is being played.
game : :class:`Game`
The game that the user is currently playing. Could be None if no game is being played.
server : :class:`Server`
The server that the member belongs to.
"""
__slots__ = [ 'deaf', 'mute', 'self_mute', 'self_deaf', 'is_afk',
'voice_channel', 'roles', 'joined_at', 'status', 'game_id',
'voice_channel', 'roles', 'joined_at', 'status', 'game',
'server' ]
def __init__(self, deaf, joined_at, user, roles, mute, **kwargs):
@ -75,7 +76,8 @@ class Member(User):
self.joined_at = parse_time(joined_at)
self.roles = roles
self.status = Status.offline
self.game_id = kwargs.get('game_id', None)
game = kwargs.get('game')
self.game = game and Game(**game)
self.server = kwargs.get('server', None)
self.update_voice_state(mute=mute, deaf=deaf)
@ -101,4 +103,3 @@ class Member(User):
# we switched channels
if self.voice_channel is not None:
self.voice_channel.voice_members.append(self)

5
discord/server.py

@ -27,6 +27,7 @@ DEALINGS IN THE SOFTWARE.
from . import utils
from .role import Role
from .member import Member
from .game import Game
from .channel import Channel
from .enums import ServerRegion, Status
from .mixins import Hashable
@ -143,7 +144,8 @@ class Server(Hashable):
member.status = Status(member.status)
except:
pass
member.game_id = presence['game_id']
game = presence.get('game')
member.game = game and Game(**game)
if 'channels' in guild:
channels = guild['channels']
@ -171,4 +173,3 @@ class Server(Hashable):
if self.icon is None:
return ''
return 'https://cdn.discordapp.com/icons/{0.id}/{0.icon}.jpg'.format(self)

4
discord/state.py

@ -26,6 +26,7 @@ DEALINGS IN THE SOFTWARE.
from .server import Server
from .user import User
from .game import Game
from .message import Message
from .channel import Channel, PrivateChannel
from .member import Member
@ -121,7 +122,8 @@ class ConnectionState:
except:
pass
member.game_id = data.get('game_id')
game = data.get('game')
member.game = game and Game(**game)
member.name = user.get('username', member.name)
member.avatar = user.get('avatar', member.avatar)

Loading…
Cancel
Save