Browse Source

Make User and Member messageable.

pull/447/head
Rapptz 8 years ago
parent
commit
7690455b21
  1. 12
      discord/member.py
  2. 5
      discord/state.py
  3. 37
      discord/user.py

12
discord/member.py

@ -26,6 +26,8 @@ DEALINGS IN THE SOFTWARE.
import asyncio
import discord.abc
from . import utils
from .user import User
from .game import Game
@ -100,7 +102,7 @@ def flatten_user(cls):
return cls
@flatten_user
class Member:
class Member(discord.abc.Messageable):
"""Represents a Discord member to a :class:`Guild`.
This implements a lot of the functionality of :class:`User`.
@ -167,6 +169,14 @@ class Member:
def __hash__(self):
return hash(self._user.id)
@asyncio.coroutine
def _get_channel(self):
ch = yield from self.create_dm()
return ch
def _get_guild_id(self):
return None
def _update(self, data, user=None):
if user:
self._user.name = user['username']

5
discord/state.py

@ -169,6 +169,11 @@ class ConnectionState:
if isinstance(channel, DMChannel):
self._private_channels_by_user[channel.recipient.id] = channel
def add_dm_channel(self, data):
channel = DMChannel(me=self.user, state=self, data=data)
self._add_private_channel(channel)
return channel
def _remove_private_channel(self, channel):
self._private_channels.pop(channel.id, None)
if isinstance(channel, DMChannel):

37
discord/user.py

@ -27,7 +27,10 @@ DEALINGS IN THE SOFTWARE.
from .utils import snowflake_time
from .enums import DefaultAvatar
class User:
import discord.abc
import asyncio
class User(discord.abc.Messageable):
"""Represents a Discord user.
Supported Operations:
@ -83,6 +86,38 @@ class User:
def __repr__(self):
return '<User id={0.id} name={0.name!r} discriminator={0.discriminator!r} bot={0.bot}>'.format(self)
@asyncio.coroutine
def _get_channel(self):
ch = yield from self.create_dm()
return ch
def _get_guild_id(self):
return None
@property
def dm_channel(self):
"""Returns the :class:`DMChannel` associated with this user if it exists.
If this returns ``None``, you can create a DM channel by calling the
:meth:`create_dm` coroutine function.
"""
return self._state._get_private_channel_by_user(self.id)
@asyncio.coroutine
def create_dm(self):
"""Creates a :class:`DMChannel` with this user.
This should be rarely called, as this is done transparently for most
people.
"""
found = self.dm_channel
if found is not None:
return found
state = self._state
data = yield from state.http.start_private_message(self.id)
return state.add_dm_channel(data)
@property
def avatar_url(self):
"""Returns a friendly URL version of the avatar variable the user has. An empty string if

Loading…
Cancel
Save