Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/__tests__/regex.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';

import { isASocialNetworkUrl, removeEmoji } from '../helpers/regex.helper';
import { isASocialNetworkUrl, removeEmoji, removeMarkdown } from '../helpers/regex.helper';

describe('Helpers: Regex', () => {
describe('Rule: isASocialNetworkUrl should regex correctly an url', () => {
Expand All @@ -22,4 +22,11 @@ describe('Helpers: Regex', () => {
expect(result).toBe(' Hello, World!');
});
});
describe('Rule: removeMarkdown should remove all markdown from a string', () => {
it('removeMarkdown() should remove all markdown from a string', () => {
const text = 'Hello, **World!** This is _me_ I **give** you `reactions` and ~more~ **quoi**';
const result = removeMarkdown(text);
expect(result).toBe('Hello, World! This is me I give you reactions and more quoi');
});
});
});
2 changes: 2 additions & 0 deletions src/helpers/regex.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const socialNetworksUrlRegex = new RegExp(
'^(https?://)?(www.)?(facebook.com|fb.me|twitter.com|vxtwitter.com|instagram.com|linkedin.com|youtube.com|youtu.be|pinterest.com|snapchat.com|tiktok.com)/[a-zA-Z0-9.-/?=&#_]+$',
);
const punctuationRegex = /[.,!?]/g;
const markdownRegex = /(\*\*|__|\*|_|`|~)(.*?)\1/g;
const emojiRegex = /<a?:.+?:\d{10,30}>|\p{Extended_Pictographic}/gu;

export const isASocialNetworkUrl = (url: string): boolean => {
Expand All @@ -10,3 +11,4 @@ export const isASocialNetworkUrl = (url: string): boolean => {

export const removePunctuation = (text: string) => text.replaceAll(punctuationRegex, '');
export const removeEmoji = (text: string) => text.replaceAll(emojiRegex, '');
export const removeMarkdown = (text: string) => text.replaceAll(markdownRegex, '$2');
5 changes: 3 additions & 2 deletions src/modules/quoiFeur/quoiFeur.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import {
} from 'discord.js';

import { cache } from '../../core/cache';
import { removeEmoji, removePunctuation } from '../../helpers/regex.helper';
import { removeEmoji, removeMarkdown, removePunctuation } from '../../helpers/regex.helper';

const ONE_MINUTE = 1 * 60 * 1000;

const quoiDetectorRegex = /\bquoi\s*$/i;
const endWithQuoi = (text: string) => quoiDetectorRegex.test(removeEmoji(removePunctuation(text)));
const endWithQuoi = (text: string) =>
quoiDetectorRegex.test(removeEmoji(removePunctuation(removeMarkdown(text))));

const reactWith = async (message: Message, reactions: string[]) => {
for (const reaction of reactions) {
Expand Down