Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Spoiler matcher is very broken, should be in our parser #37

Open
github-actions bot opened this issue Oct 19, 2020 · 2 comments
Open

Spoiler matcher is very broken, should be in our parser #37

github-actions bot opened this issue Oct 19, 2020 · 2 comments
Assignees
Labels
bug Something isn't working todo
Milestone

Comments

@github-actions
Copy link

github-actions bot commented Oct 19, 2020

This giant hack has false negatives, eventually should be fixed

edit: disabled for now because its causing problems & doesn't work anyways

https://github.com/ADoesGit/rich-quotes/blob/8de6463eb9cb361c85717ad4045e915c6c2ce151/utils/embedHandler.js#L1

const spoilerMatcher = (b) => new RegExp(`\\|\\|${b.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&').replace('\/\/','\/\/(.*?)').replace(/\//g,'\\/')}(.*?)\\|\\|`);

const mime = require('../node_modules/mime-types')

module.exports = (messageData, settings, hasEmbedSpoilers) => {
    if (!settings.embedAll) {
        if (messageData.embeds?.length !== 0) {
            let colorFixes = [];
            let urls = []

            messageData.embeds = messageData.embeds?.filter((embed, i) => {
                if (spoilerMatcher(embed.url).test(messageData.content)) hasEmbedSpoilers = true;
                if (typeof embed.color !== 'string') colorFixes.push(i);

                let keepEmbed = true;

                if (embed.image) keepEmbed = settings.embedImages;
                else if (embed.video) {
                    keepEmbed = settings.embedVideos;
                    if (!embed.video.proxyURL) keepEmbed = settings.embedYouTube
                } else keepEmbed = settings.embedOther;

                if (keepEmbed) urls.push(embed.url);

                if (urls.length === 1 && messageData.content === urls[0]) messageData.content = '';

                return keepEmbed;
            });

            // @todo Move this to filter?
            if (colorFixes.length !== 0) colorFixes.forEach((e, i) => { if (messageData.embeds[i]) messageData.embeds[i].color = '#00000000' });
        }

        if (messageData.attachments?.length !== 0)
        messageData.attachments = messageData.attachments.filter((file) => {
            const mime_type = mime.lookup(file.proxy_url.split('/')[6]);
            if (mime_type) switch (mime_type.split('/')[0]) {
                case 'image': return settings.embedImages; break;
                case 'video': return settings.embedVideos; break;
                case 'audio': return settings.embedAudio; break;
                default: return settings.embedFile; break;
            }
            else return settings.embedFile;
        });
    } else if (settings.displayEmbeds) {
        let urls = [];

        messageData.embeds.forEach((embed, i) => {
            if (spoilerMatcher(embed.url).test(messageData.content)) hasEmbedSpoilers = true;
            if (typeof embed.color !== 'string') messageData.embeds[i].color = '#00000000';
            urls.push(embed.url);
        });

        if (urls.length === 1 && messageData.content === urls[0]) messageData.content = '';
    }
}
 No newline at end of file

cb38f9ae0eb9b66a56bd8507e0abdff786814bbe

@MulverineX
Copy link
Collaborator

MulverineX commented Oct 25, 2020

Even if we use parse on this it is very tricky to get working properly as a regex. I'll either write a jank parser for it or get someone to write regex jujitsu

@MulverineX MulverineX added the bug Something isn't working label Nov 5, 2020
@MulverineX MulverineX changed the title This giant hack has false negatives, eventually sh... Spoiler matcher is very broken, should be a parser Nov 14, 2020
@MulverineX MulverineX changed the title Spoiler matcher is very broken, should be a parser Spoiler matcher is very broken, should be in our parser Nov 24, 2020
@MulverineX MulverineX added this to the 1.5.0 milestone Nov 24, 2020
@MulverineX MulverineX modified the milestones: 1.5.0, 1.6.0 May 16, 2021
@MulverineX
Copy link
Collaborator

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working todo
Projects
None yet
Development

No branches or pull requests

2 participants