~nova/fletcher#52: 
Ping Sync

Translate username pings to simple usernames over bridge

Status
RESOLVED DUPLICATE
Submitter
~nova
Assigned to
No-one
Submitted
1 year, 7 months ago
Updated
1 year, 7 months ago
Labels
Core Regression

~nova 1 year, 7 months ago

discord.ext.commands.clean_content() should do the trick, but we want to do some fancy markdown conversion if possible to link to the right place across servers, so here's the raw source for adaptation (discord/ext/commands/converter.py):

    async def convert(self, ctx, argument):
        message = ctx.message
        transformations = {}

        if self.fix_channel_mentions and ctx.guild:
            def resolve_channel(id, *, _get=ctx.guild.get_channel):
                ch = _get(id)
                return ('<#%s>' % id), ('#' + ch.name if ch else '#deleted-channel')

            transformations.update(resolve_channel(channel) for channel in message.raw_channel_mentions)

        if self.use_nicknames and ctx.guild:
            def resolve_member(id, *, _get=ctx.guild.get_member):
                m = _get(id)
                return '@' + m.display_name if m else '@deleted-user'
        else:
            def resolve_member(id, *, _get=ctx.bot.get_user):
                m = _get(id)
                return '@' + m.name if m else '@deleted-user'


        transformations.update(
            ('<@%s>' % member_id, resolve_member(member_id))
            for member_id in message.raw_mentions
        )

        transformations.update(
            ('<@!%s>' % member_id, resolve_member(member_id))
            for member_id in message.raw_mentions
        )

        if ctx.guild:
            def resolve_role(_id, *, _find=ctx.guild.get_role):
                r = _find(_id)
                return '@' + r.name if r else '@deleted-role'

            transformations.update(
                ('<@&%s>' % role_id, resolve_role(role_id))
                for role_id in message.raw_role_mentions
            )

        def repl(obj):
            return transformations.get(obj.group(0), '')

        pattern = re.compile('|'.join(transformations.keys()))
        result = pattern.sub(repl, argument)

        if self.escape_markdown:
            transformations = {
                re.escape(c): '\\' + c
                for c in ('*', '`', '_', '~', '\\')
            }

            def replace(obj):
                return transformations.get(re.escape(obj.group(0)), '')

            pattern = re.compile('|'.join(transformations.keys()))
            result = pattern.sub(replace, result)

        # Completely ensure no mentions escape:
        return re.sub(r'@(everyone|here|[!&]?[0-9]{17,21})', '@\u200b\\1', result)

~nova REPORTED DUPLICATE 1 year, 7 months ago

Dupes #35.

Register here or Log in to comment, or comment via email.