Browse Source

Implement User.profile coroutine to get a user's profile.

pull/468/head
Rapptz 8 years ago
parent
commit
02f30f21c4
  1. 2
      discord/__init__.py
  2. 3
      discord/http.py
  3. 38
      discord/user.py
  4. 29
      docs/api.rst

2
discord/__init__.py

@ -18,7 +18,7 @@ __copyright__ = 'Copyright 2015-2017 Rapptz'
__version__ = '1.0.0a0'
from .client import Client, AppInfo, ChannelPermissions
from .user import User, ClientUser
from .user import User, ClientUser, Profile
from .game import Game
from .emoji import Emoji, PartialEmoji
from .channel import *

3
discord/http.py

@ -666,3 +666,6 @@ class HTTPClient:
def get_user_info(self, user_id):
return self.request(Route('GET', '/users/{user_id}', user_id=user_id))
def get_user_profile(self, user_id):
return self.request(Route('GET', '/users/{user_id}/profile', user_id=user_id))

38
discord/user.py

@ -24,13 +24,17 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from .utils import snowflake_time, _bytes_to_base64_data
from .utils import snowflake_time, _bytes_to_base64_data, parse_time
from .enums import DefaultAvatar, RelationshipType
from .errors import ClientException
from collections import namedtuple
import discord.abc
import asyncio
Profile = namedtuple('Profile', 'premium user mutual_guilds connected_accounts premium_since')
class BaseUser:
__slots__ = ('name', 'id', 'discriminator', 'avatar', 'bot', '_state')
@ -424,3 +428,35 @@ class User(BaseUser, discord.abc.Messageable):
Sending the friend request failed.
"""
yield from self._state.http.send_friend_request(username=self.name, discriminator=self.discriminator)
@asyncio.coroutine
def profile(self):
"""|coro|
Gets the user's profile. This can only be used by non-bot accounts.
Raises
-------
Forbidden
Not allowed to fetch profiles.
HTTPException
Fetching the profile failed.
Returns
--------
:class:`Profile`
The profile of the user.
"""
state = self._state
data = yield from state.http.get_user_profile(self.id)
def transform(d):
return state._get_guild(int(d['id']))
mutual_guilds = list(filter(None, map(transform, data.get('mutual_guilds', []))))
return Profile(premium=data['premium'],
premium_since=parse_time(data.get('premium_since')),
mutual_guilds=mutual_guilds,
user=self,
connected_accounts=data['connected_accounts'])

29
docs/api.rst

@ -481,6 +481,35 @@ Application Info
The owner of the application. This is a :class:`User` instance
with the owner's information at the time of the call.
Profile
---------
.. class:: Profile
A namedtuple representing a user's Discord public profile.
.. attribute:: user
The :class:`User` the profile belongs to.
.. attribute:: premium
A boolean indicating if the user has premium (i.e. Discord Nitro).
.. attribute:: premium_since
A naive UTC datetime indicating how long the user has been premium since.
This could be ``None`` if not applicable.
.. attribute:: mutual_guilds
A list of :class:`Guild` that the :class:`ClientUser` shares with this
user.
.. attribute:: connected_accounts
A list of dict objects indicating the accounts the user has connected.
An example entry can be seen below: ::
{type: "twitch", id: "92473777", name: "discordapp"}
.. _discord-api-enums:
Enumerations

Loading…
Cancel
Save