diff options
| author | Rapptz <[email protected]> | 2015-12-17 00:15:41 -0500 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2015-12-17 00:15:41 -0500 |
| commit | 613214f1976f4f81a65f4cd4ebece07ed73611d3 (patch) | |
| tree | 67559054abd10f3fa863f525b2eb5c89a408888c | |
| parent | Change regex from \d+ to [0-9]+ for performance reasons. (diff) | |
| download | discord.py-613214f1976f4f81a65f4cd4ebece07ed73611d3.tar.xz discord.py-613214f1976f4f81a65f4cd4ebece07ed73611d3.zip | |
Add Message.clean_content property to get prettified mentions.
| -rw-r--r-- | discord/message.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/discord/message.py b/discord/message.py index f259032d..b94d50c2 100644 --- a/discord/message.py +++ b/discord/message.py @@ -144,6 +144,32 @@ class Message(object): """ return re.findall(r'<#([0-9]+)>', self.content) + @utils.cached_property + def clean_content(self): + """A property that returns the content in a "cleaned up" + manner. This basically means that mentions are transformed + into the way the client shows it. e.g. ``<#id>`` will transform + into ``#name``. + """ + + transformations = { + re.escape('<#{0.id}>'.format(channel)): '#' + channel.name + for channel in self.channel_mentions + } + + mention_transforms = { + re.escape('<@{0.id}>'.format(member)): '@' + member.name + for member in self.mentions + } + + transformations.update(mention_transforms) + + def repl(obj): + return transformations.get(re.escape(obj.group(0)), '') + + pattern = re.compile('|'.join(transformations.keys())) + return pattern.sub(repl, self.content) + def _handle_upgrades(self, channel_id): self.server = None if self.channel is None: |