Move message creation to a factory method inside ConnectionState.

This commit is contained in:
Rapptz
2017-01-03 08:41:44 -05:00
parent 5e6bfecb07
commit 98b981848d
14 changed files with 94 additions and 104 deletions

View File

@ -27,8 +27,9 @@ DEALINGS IN THE SOFTWARE.
import asyncio
import re
import discord.utils
import discord.abc
from . import utils
from .user import User
from .reaction import Reaction
from .emoji import Emoji
@ -136,7 +137,7 @@ class Message:
def _add_reaction(self, data):
emoji = self._state.get_reaction_emoji(data['emoji'])
reaction = discord.utils.find(lambda r: r.emoji == emoji, self.reactions)
reaction = utils.find(lambda r: r.emoji == emoji, self.reactions)
is_me = data['me'] = int(data['user_id']) == self._state.self_id
if reaction is None:
@ -151,7 +152,7 @@ class Message:
def _remove_reaction(self, data):
emoji = self._state.get_reaction_emoji(data['emoji'])
reaction = discord.utils.find(lambda r: r.emoji == emoji, self.reactions)
reaction = utils.find(lambda r: r.emoji == emoji, self.reactions)
if reaction is None:
# already removed?
@ -177,7 +178,7 @@ class Message:
except KeyError:
continue
self._try_patch(data, 'edited_timestamp', discord.utils.parse_time)
self._try_patch(data, 'edited_timestamp', utils.parse_time)
self._try_patch(data, 'pinned', bool)
self._try_patch(data, 'mention_everyone', bool)
self._try_patch(data, 'tts', bool)
@ -218,7 +219,7 @@ class Message:
self.role_mentions = []
if self.guild is not None:
for role_id in role_mentions:
role = discord.utils.get(self.guild.roles, id=role_id)
role = utils.get(self.guild.roles, id=role_id)
if role is not None:
self.role_mentions.append(role)
@ -235,19 +236,19 @@ class Message:
if uid == self.author.id:
participants.append(self.author)
else:
user = discord.utils.find(lambda u: u.id == uid, self.mentions)
user = utils.find(lambda u: u.id == uid, self.mentions)
if user is not None:
participants.append(user)
call['participants'] = participants
self.call = CallMessage(message=self, **call)
@discord.utils.cached_slot_property('_cs_guild')
@utils.cached_slot_property('_cs_guild')
def guild(self):
"""Optional[:class:`Guild`]: The guild that the message belongs to, if applicable."""
return getattr(self.channel, 'guild', None)
@discord.utils.cached_slot_property('_cs_raw_mentions')
@utils.cached_slot_property('_cs_raw_mentions')
def raw_mentions(self):
"""A property that returns an array of user IDs matched with
the syntax of <@user_id> in the message content.
@ -257,28 +258,28 @@ class Message:
"""
return [int(x) for x in re.findall(r'<@!?([0-9]+)>', self.content)]
@discord.utils.cached_slot_property('_cs_raw_channel_mentions')
@utils.cached_slot_property('_cs_raw_channel_mentions')
def raw_channel_mentions(self):
"""A property that returns an array of channel IDs matched with
the syntax of <#channel_id> in the message content.
"""
return [int(x) for x in re.findall(r'<#([0-9]+)>', self.content)]
@discord.utils.cached_slot_property('_cs_raw_role_mentions')
@utils.cached_slot_property('_cs_raw_role_mentions')
def raw_role_mentions(self):
"""A property that returns an array of role IDs matched with
the syntax of <@&role_id> in the message content.
"""
return [int(x) for x in re.findall(r'<@&([0-9]+)>', self.content)]
@discord.utils.cached_slot_property('_cs_channel_mentions')
@utils.cached_slot_property('_cs_channel_mentions')
def channel_mentions(self):
if self.guild is None:
return []
it = filter(None, map(lambda m: self.guild.get_channel(m), self.raw_channel_mentions))
return discord.utils._unique(it)
return utils._unique(it)
@discord.utils.cached_slot_property('_cs_clean_content')
@utils.cached_slot_property('_cs_clean_content')
def clean_content(self):
"""A property that returns the content in a "cleaned up"
manner. This basically means that mentions are transformed
@ -352,9 +353,9 @@ class Message:
@property
def created_at(self):
"""Returns the message's creation time in UTC."""
return discord.utils.snowflake_time(self.id)
return utils.snowflake_time(self.id)
@discord.utils.cached_slot_property('_cs_system_content')
@utils.cached_slot_property('_cs_system_content')
def system_content(self):
"""A property that returns the content that is rendered
regardless of the :attr:`Message.type`.