diff options
| author | Rapptz <[email protected]> | 2017-03-14 19:12:30 -0400 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2017-03-14 19:12:30 -0400 |
| commit | 51b0baeb9dde8845e48b988c45e2b30a8734ad8d (patch) | |
| tree | 9e10750da611fecbe7852ce62d38d22ac9dd4ab2 | |
| parent | Remove Message.edited_timestamp in favour of Message.edited_at (diff) | |
| download | discord.py-51b0baeb9dde8845e48b988c45e2b30a8734ad8d.tar.xz discord.py-51b0baeb9dde8845e48b988c45e2b30a8734ad8d.zip | |
Speed up message update handling.
Apparently, checking if something is not None and then calling it is
faster than having an identity function that will just return the
original value untransformed, such as a lambda or doing a str(str) call
| -rw-r--r-- | discord/message.py | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/discord/message.py b/discord/message.py index ccb4fda9..00e017f8 100644 --- a/discord/message.py +++ b/discord/message.py @@ -125,13 +125,16 @@ class Message: def __repr__(self): return '<Message id={0.id} pinned={0.pinned} author={0.author!r}>'.format(self) - def _try_patch(self, data, key, transform): + def _try_patch(self, data, key, transform=None): try: value = data[key] except KeyError: pass else: - setattr(self, key, transform(value)) + if transform is None: + setattr(self, key, value) + else: + setattr(self, key, transform(value)) def _add_reaction(self, data): emoji = self._state.get_reaction_emoji(data['emoji']) @@ -171,14 +174,14 @@ class Message: def _update(self, channel, data): self.channel = channel self._edited_timestamp = utils.parse_time(data.get('edited_timestamp')) - self._try_patch(data, 'pinned', bool) - self._try_patch(data, 'mention_everyone', bool) - self._try_patch(data, 'tts', bool) + self._try_patch(data, 'pinned') + self._try_patch(data, 'mention_everyone') + self._try_patch(data, 'tts') self._try_patch(data, 'type', lambda x: try_enum(MessageType, x)) - self._try_patch(data, 'content', str) - self._try_patch(data, 'attachments', lambda x: x) + self._try_patch(data, 'content') + self._try_patch(data, 'attachments') self._try_patch(data, 'embeds', lambda x: list(map(Embed.from_data, x))) - self._try_patch(data, 'nonce', lambda x: x) + self._try_patch(data, 'nonce') for handler in ('author', 'mentions', 'mention_roles', 'call'): try: |