From 7690455b21d3406cbea55b75a7ebb10423b8d14c Mon Sep 17 00:00:00 2001 From: Rapptz Date: Tue, 3 Jan 2017 09:16:15 -0500 Subject: Make User and Member messageable. --- discord/user.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'discord/user.py') 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 ''.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 -- cgit v1.2.3