Browse Source

Make more things into properties.

A lot of the expensive getters were transformed into cached properties
instead. A lot of things that were properties were transformed into
properties as well.
pull/60/head
Rapptz 9 years ago
parent
commit
de1c74a399
  1. 20
      discord/message.py
  2. 1
      discord/role.py
  3. 13
      discord/server.py
  4. 4
      discord/state.py
  5. 15
      discord/utils.py

20
discord/message.py

@ -131,24 +131,25 @@ class Message(object):
self.mentions.append(member)
if self.server is not None:
channel_mentions = self.get_raw_channel_mentions()
for mention in channel_mentions:
for mention in self.raw_channel_mentions:
channel = utils.find(lambda m: m.id == mention, self.server.channels)
if channel is not None:
self.channel_mentions.append(channel)
def get_raw_mentions(self):
"""Returns an array of user IDs matched with the syntax of
<@user_id> in the message content.
@utils.cached_property
def raw_mentions(self):
"""A property that returns an array of user IDs matched with
the syntax of <@user_id> in the message content.
This allows you receive the user IDs of mentioned users
even in a private message context.
"""
return re.findall(r'<@(\d+)>', self.content)
def get_raw_channel_mentions(self):
"""Returns an array of channel IDs matched with the syntax of
<#channel_id> in the message content.
@utils.cached_property
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.
This allows you receive the channel IDs of mentioned users
even in a private message context.
@ -168,6 +169,3 @@ class Message(object):
found = utils.find(lambda m: m.id == self.author.id, self.server.members)
if found is not None:
self.author = found

1
discord/role.py

@ -81,6 +81,7 @@ class Role(EqualityComparable):
if 'everyone' in kwargs:
self._is_everyone = kwargs['everyone']
@property
def is_everyone(self):
"""Checks if the role is the @everyone role."""
return self._is_everyone

13
discord/server.py

@ -108,12 +108,11 @@ class Server(EqualityComparable):
self.unavailable = guild.get('unavailable', False)
self.id = guild['id']
self.roles = [Role(everyone=(self.id == r['id']), **r) for r in guild['roles']]
default_role = self.get_default_role()
owner_id = guild['owner_id']
for data in guild.get('members', []):
roles = [default_role]
roles = [self.default_role]
for role_id in data['roles']:
role = utils.find(lambda r: r.id == role_id, self.roles)
if role is not None:
@ -149,13 +148,15 @@ class Server(EqualityComparable):
for obj in guild.get('voice_states', []):
self._update_voice_state(obj)
def get_default_role(self):
@utils.cached_property
def default_role(self):
"""Gets the @everyone role that all members have by default."""
return utils.find(lambda r: r.is_everyone(), self.roles)
return utils.find(lambda r: r.is_everyone, self.roles)
def get_default_channel(self):
@utils.cached_property
def default_channel(self):
"""Gets the default :class:`Channel` for the server."""
return utils.find(lambda c: c.is_default_channel(), self.channels)
return utils.find(lambda c: c.is_default, self.channels)
@property
def icon_url(self):

4
discord/state.py

@ -169,7 +169,7 @@ class ConnectionState:
def parse_guild_member_add(self, data):
server = self._get_server(data.get('guild_id'))
member = Member(server=server, deaf=False, mute=False, **data)
member.roles.append(server.get_default_role())
member.roles.append(server.default_role)
server.members.append(member)
self.dispatch('member_join', member)
@ -195,7 +195,7 @@ class ConnectionState:
member.name = user['username']
member.discriminator = user['discriminator']
member.avatar = user['avatar']
member.roles = [server.get_default_role()]
member.roles = [server.default_role]
# update the roles
for role in server.roles:
if role.id in data['roles']:

15
discord/utils.py

@ -31,6 +31,21 @@ from base64 import b64encode
import asyncio
import json
class cached_property:
def __init__(self, function):
self.function = function
self.__doc__ = getattr(function, '__doc__')
def __get__(self, instance, owner):
if instance is None:
return self
value = self.function(instance)
setattr(instance, self.function.__name__, value)
return value
def parse_time(timestamp):
if timestamp:
return datetime.datetime(*map(int, re_split(r'[^\d]', timestamp.replace('+00:00', ''))))

Loading…
Cancel
Save