aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--discord/member.py12
-rw-r--r--discord/state.py5
-rw-r--r--discord/user.py37
3 files changed, 52 insertions, 2 deletions
diff --git a/discord/member.py b/discord/member.py
index eb8d18d4..3d9928dc 100644
--- a/discord/member.py
+++ b/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']
diff --git a/discord/state.py b/discord/state.py
index fa8c12b9..31f29545 100644
--- a/discord/state.py
+++ b/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):
diff --git a/discord/user.py b/discord/user.py
index 77f257fe..387c20b0 100644
--- a/discord/user.py
+++ b/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