mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-05-15 10:19:59 +00:00
Make User and Member messageable.
This commit is contained in:
parent
7431a127cf
commit
7690455b21
@ -26,6 +26,8 @@ DEALINGS IN THE SOFTWARE.
|
|||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
|
import discord.abc
|
||||||
|
|
||||||
from . import utils
|
from . import utils
|
||||||
from .user import User
|
from .user import User
|
||||||
from .game import Game
|
from .game import Game
|
||||||
@ -100,7 +102,7 @@ def flatten_user(cls):
|
|||||||
return cls
|
return cls
|
||||||
|
|
||||||
@flatten_user
|
@flatten_user
|
||||||
class Member:
|
class Member(discord.abc.Messageable):
|
||||||
"""Represents a Discord member to a :class:`Guild`.
|
"""Represents a Discord member to a :class:`Guild`.
|
||||||
|
|
||||||
This implements a lot of the functionality of :class:`User`.
|
This implements a lot of the functionality of :class:`User`.
|
||||||
@ -167,6 +169,14 @@ class Member:
|
|||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return hash(self._user.id)
|
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):
|
def _update(self, data, user=None):
|
||||||
if user:
|
if user:
|
||||||
self._user.name = user['username']
|
self._user.name = user['username']
|
||||||
|
@ -169,6 +169,11 @@ class ConnectionState:
|
|||||||
if isinstance(channel, DMChannel):
|
if isinstance(channel, DMChannel):
|
||||||
self._private_channels_by_user[channel.recipient.id] = channel
|
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):
|
def _remove_private_channel(self, channel):
|
||||||
self._private_channels.pop(channel.id, None)
|
self._private_channels.pop(channel.id, None)
|
||||||
if isinstance(channel, DMChannel):
|
if isinstance(channel, DMChannel):
|
||||||
|
@ -27,7 +27,10 @@ DEALINGS IN THE SOFTWARE.
|
|||||||
from .utils import snowflake_time
|
from .utils import snowflake_time
|
||||||
from .enums import DefaultAvatar
|
from .enums import DefaultAvatar
|
||||||
|
|
||||||
class User:
|
import discord.abc
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
class User(discord.abc.Messageable):
|
||||||
"""Represents a Discord user.
|
"""Represents a Discord user.
|
||||||
|
|
||||||
Supported Operations:
|
Supported Operations:
|
||||||
@ -83,6 +86,38 @@ class User:
|
|||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<User id={0.id} name={0.name!r} discriminator={0.discriminator!r} bot={0.bot}>'.format(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
|
@property
|
||||||
def avatar_url(self):
|
def avatar_url(self):
|
||||||
"""Returns a friendly URL version of the avatar variable the user has. An empty string if
|
"""Returns a friendly URL version of the avatar variable the user has. An empty string if
|
||||||
|
Loading…
x
Reference in New Issue
Block a user