From 5c0b127c96347b3ccf1d194b47eb941f33b43dae Mon Sep 17 00:00:00 2001 From: Rapptz Date: Tue, 27 Aug 2019 18:34:40 -0400 Subject: [PATCH] Create Permissions and Colour objects when necessary in Role This should have some memory savings of ~24 bytes per role. * 8 bytes for Permissions * 8 bytes for Colour * 8 bytes for the alias I'm not sure how significant it is but it should be a decent amount. --- discord/role.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/discord/role.py b/discord/role.py index 8608c5e10..647afb55f 100644 --- a/discord/role.py +++ b/discord/role.py @@ -73,12 +73,8 @@ class Role(Hashable): The ID for the role. name: :class:`str` The name of the role. - permissions: :class:`Permissions` - Represents the role's permissions. guild: :class:`Guild` The guild the role belongs to. - colour: :class:`Colour` - Represents the role colour. An alias exists under ``color``. hoist: :class:`bool` Indicates if the role will be displayed separately from other members. position: :class:`int` @@ -91,7 +87,7 @@ class Role(Hashable): Indicates if the role can be mentioned by users. """ - __slots__ = ('id', 'name', 'permissions', 'color', 'colour', 'position', + __slots__ = ('id', 'name', '_permissions', '_colour', 'position', 'managed', 'mentionable', 'hoist', 'guild', '_state') def __init__(self, *, guild, state, data): @@ -144,18 +140,29 @@ class Role(Hashable): def _update(self, data): self.name = data['name'] - self.permissions = Permissions(data.get('permissions', 0)) + self._permissions = data.get('permissions', 0) self.position = data.get('position', 0) - self.colour = Colour(data.get('color', 0)) + self._colour = data.get('color', 0) self.hoist = data.get('hoist', False) self.managed = data.get('managed', False) self.mentionable = data.get('mentionable', False) - self.color = self.colour def is_default(self): """Checks if the role is the default role.""" return self.guild.id == self.id + @property + def permissions(self): + """:class:`Permissions`: Returns the role's permissions.""" + return Permissions(self._permissions) + + @property + def colour(self): + """:class:`Colour`: Returns the role colour. An alias exists under ``color``.""" + return Colour(self._colour) + + color = colour + @property def created_at(self): """:class:`datetime.datetime`: Returns the role's creation time in UTC."""