From bc9aef714ae2cd830da0ead4581caf49240abca6 Mon Sep 17 00:00:00 2001 From: dilaouid Date: Thu, 7 Sep 2023 21:33:44 +0200 Subject: [PATCH 1/9] change emoji regular expression to include custom emojis --- src/helpers/regex.helper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/regex.helper.ts b/src/helpers/regex.helper.ts index de9c326..4c5431b 100644 --- a/src/helpers/regex.helper.ts +++ b/src/helpers/regex.helper.ts @@ -2,7 +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 emojiRegex = /(\p{Extended_Pictographic}|\p{Emoji_Component})/gu; +const emojiRegex = /|\p{Extended_Pictographic}/gu; export const isASocialNetworkUrl = (url: string): boolean => { return socialNetworksUrlRegex.test(url); From 4873510ac7157fa19732fcdc1f0c76f5dd39da09 Mon Sep 17 00:00:00 2001 From: dilaouid Date: Thu, 7 Sep 2023 23:37:04 +0200 Subject: [PATCH 2/9] fix: regex of custom emojis - ids can go from 17 to 19 digits --- src/helpers/regex.helper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/regex.helper.ts b/src/helpers/regex.helper.ts index 4c5431b..d0e07c4 100644 --- a/src/helpers/regex.helper.ts +++ b/src/helpers/regex.helper.ts @@ -2,7 +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 emojiRegex = /|\p{Extended_Pictographic}/gu; +const emojiRegex = /|\p{Extended_Pictographic}/gu; export const isASocialNetworkUrl = (url: string): boolean => { return socialNetworksUrlRegex.test(url); From 536bd131f9c1791faa9cebb157d8b0c83ffd419f Mon Sep 17 00:00:00 2001 From: dilaouid Date: Thu, 7 Sep 2023 23:38:20 +0200 Subject: [PATCH 3/9] feat(test): add test of removeEmoji regex --- src/__tests__/regex.spec.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/__tests__/regex.spec.ts b/src/__tests__/regex.spec.ts index 9010bc0..0aa208d 100644 --- a/src/__tests__/regex.spec.ts +++ b/src/__tests__/regex.spec.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest'; -import { isASocialNetworkUrl } from '../helpers/regex.helper'; +import { isASocialNetworkUrl, removeEmoji } from '../helpers/regex.helper'; describe('Helpers: Regex', () => { describe('Rule: isASocialNetworkUrl should regex correctly an url', () => { @@ -15,4 +15,11 @@ describe('Helpers: Regex', () => { expect(result).toBe(false); }); }); + describe('Rule: removeEmoji should remove all emojis from a string', () => { + it('removeEmoji() should remove all emojis from a string', () => { + const text = '👋 Hello, World!<:SpongebobMock:1136008737669259407>'; + const result = removeEmoji(text); + expect(result).toBe(' Hello, World!'); + }); + }); }); From fbf1e73b2eabbc2a0e265d899351ecdaa8e288f7 Mon Sep 17 00:00:00 2001 From: dilaouid Date: Fri, 8 Sep 2023 19:50:13 +0200 Subject: [PATCH 4/9] fix: remove md to catch the 'quoi' properly --- src/__tests__/regex.spec.ts | 9 ++++++++- src/helpers/regex.helper.ts | 2 ++ src/modules/quoiFeur/quoiFeur.helpers.ts | 4 ++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/__tests__/regex.spec.ts b/src/__tests__/regex.spec.ts index 0aa208d..6b6cf60 100644 --- a/src/__tests__/regex.spec.ts +++ b/src/__tests__/regex.spec.ts @@ -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', () => { @@ -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'); + }); + }); }); diff --git a/src/helpers/regex.helper.ts b/src/helpers/regex.helper.ts index d0e07c4..7633b62 100644 --- a/src/helpers/regex.helper.ts +++ b/src/helpers/regex.helper.ts @@ -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 = /|\p{Extended_Pictographic}/gu; export const isASocialNetworkUrl = (url: string): boolean => { @@ -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'); \ No newline at end of file diff --git a/src/modules/quoiFeur/quoiFeur.helpers.ts b/src/modules/quoiFeur/quoiFeur.helpers.ts index 60469dc..e1ad51c 100644 --- a/src/modules/quoiFeur/quoiFeur.helpers.ts +++ b/src/modules/quoiFeur/quoiFeur.helpers.ts @@ -7,12 +7,12 @@ import { } from 'discord.js'; import { cache } from '../../core/cache'; -import { removeEmoji, removePunctuation } from '../../helpers/regex.helper'; +import { removeEmoji, removePunctuation, removeMarkdown } 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) { From 76f5fc74e8311bbb734f562b9a0bbfa585d320da Mon Sep 17 00:00:00 2001 From: dilaouid Date: Mon, 11 Sep 2023 21:04:12 +0200 Subject: [PATCH 5/9] fix: use the quoifeurbot in public thread of allowed channels --- src/modules/quoiFeur/quoiFeur.helpers.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/modules/quoiFeur/quoiFeur.helpers.ts b/src/modules/quoiFeur/quoiFeur.helpers.ts index 66d595a..ef023d1 100644 --- a/src/modules/quoiFeur/quoiFeur.helpers.ts +++ b/src/modules/quoiFeur/quoiFeur.helpers.ts @@ -7,7 +7,7 @@ import { } from 'discord.js'; import { cache } from '../../core/cache'; -import { removeEmoji, removePunctuation, removeMarkdown } from '../../helpers/regex.helper'; +import { removeEmoji, removeMarkdown,removePunctuation } from '../../helpers/regex.helper'; const ONE_MINUTE = 1 * 60 * 1000; @@ -29,8 +29,18 @@ export const reactOnEndWithQuoi = async (message: Message) => { if (!endWithQuoi(message.content)) return; const channelIds = await cache.get('quoiFeurChannels', []); - const channelHasGame = channelIds.find((channelId) => channelId === message.channelId); - if (!channelHasGame) return; + + let messageParentId = null; + if (message.channel.type === ChannelType.PublicThread) { + messageParentId = message.channel.parentId; + } + + const isMessageInQuoiFeurChannel = ( + channelIds.includes(message.channelId) || + (messageParentId && channelIds.includes(messageParentId)) + ); + + if (!isMessageInQuoiFeurChannel) return; const probability = 1 / 6; From 7e5f48737073bd61185479fa7063274ee7c8b761 Mon Sep 17 00:00:00 2001 From: dilaouid Date: Mon, 11 Sep 2023 21:33:34 +0200 Subject: [PATCH 6/9] fix: revert commit because stupid dilaouid --- src/modules/quoiFeur/quoiFeur.helpers.ts | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/src/modules/quoiFeur/quoiFeur.helpers.ts b/src/modules/quoiFeur/quoiFeur.helpers.ts index ef023d1..66d595a 100644 --- a/src/modules/quoiFeur/quoiFeur.helpers.ts +++ b/src/modules/quoiFeur/quoiFeur.helpers.ts @@ -7,7 +7,7 @@ import { } from 'discord.js'; import { cache } from '../../core/cache'; -import { removeEmoji, removeMarkdown,removePunctuation } from '../../helpers/regex.helper'; +import { removeEmoji, removePunctuation, removeMarkdown } from '../../helpers/regex.helper'; const ONE_MINUTE = 1 * 60 * 1000; @@ -29,18 +29,8 @@ export const reactOnEndWithQuoi = async (message: Message) => { if (!endWithQuoi(message.content)) return; const channelIds = await cache.get('quoiFeurChannels', []); - - let messageParentId = null; - if (message.channel.type === ChannelType.PublicThread) { - messageParentId = message.channel.parentId; - } - - const isMessageInQuoiFeurChannel = ( - channelIds.includes(message.channelId) || - (messageParentId && channelIds.includes(messageParentId)) - ); - - if (!isMessageInQuoiFeurChannel) return; + const channelHasGame = channelIds.find((channelId) => channelId === message.channelId); + if (!channelHasGame) return; const probability = 1 / 6; From 5e431fcd6195e95797a903e9094d0a2f002f608a Mon Sep 17 00:00:00 2001 From: dilaouid Date: Tue, 12 Sep 2023 00:17:32 +0200 Subject: [PATCH 7/9] fix: prettier --- src/helpers/regex.helper.ts | 2 +- src/modules/quoiFeur/quoiFeur.helpers.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/helpers/regex.helper.ts b/src/helpers/regex.helper.ts index 7633b62..2044aa0 100644 --- a/src/helpers/regex.helper.ts +++ b/src/helpers/regex.helper.ts @@ -11,4 +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'); \ No newline at end of file +export const removeMarkdown = (text: string) => text.replaceAll(markdownRegex, '$2'); diff --git a/src/modules/quoiFeur/quoiFeur.helpers.ts b/src/modules/quoiFeur/quoiFeur.helpers.ts index 66d595a..bf62b32 100644 --- a/src/modules/quoiFeur/quoiFeur.helpers.ts +++ b/src/modules/quoiFeur/quoiFeur.helpers.ts @@ -12,7 +12,8 @@ import { removeEmoji, removePunctuation, removeMarkdown } from '../../helpers/re const ONE_MINUTE = 1 * 60 * 1000; const quoiDetectorRegex = /\bquoi\s*$/i; -const endWithQuoi = (text: string) => quoiDetectorRegex.test(removeEmoji(removePunctuation(removeMarkdown(text)))); +const endWithQuoi = (text: string) => + quoiDetectorRegex.test(removeEmoji(removePunctuation(removeMarkdown(text)))); const reactWith = async (message: Message, reactions: string[]) => { for (const reaction of reactions) { From 9d0a3214a87c8e1a1794d380431261e6325facdf Mon Sep 17 00:00:00 2001 From: dilaouid Date: Tue, 12 Sep 2023 15:53:29 +0200 Subject: [PATCH 8/9] fix: eslint import --- src/modules/quoiFeur/quoiFeur.helpers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/quoiFeur/quoiFeur.helpers.ts b/src/modules/quoiFeur/quoiFeur.helpers.ts index bf62b32..3f2e419 100644 --- a/src/modules/quoiFeur/quoiFeur.helpers.ts +++ b/src/modules/quoiFeur/quoiFeur.helpers.ts @@ -7,7 +7,7 @@ import { } from 'discord.js'; import { cache } from '../../core/cache'; -import { removeEmoji, removePunctuation, removeMarkdown } from '../../helpers/regex.helper'; +import { removeEmoji, removeMarkdown,removePunctuation } from '../../helpers/regex.helper'; const ONE_MINUTE = 1 * 60 * 1000; From 6cd8cad6949d73c97a5aa15feff121f48569d978 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pe=C3=AFo=20Thibault?= Date: Tue, 12 Sep 2023 21:40:39 +0200 Subject: [PATCH 9/9] Update src/modules/quoiFeur/quoiFeur.helpers.ts --- src/modules/quoiFeur/quoiFeur.helpers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/quoiFeur/quoiFeur.helpers.ts b/src/modules/quoiFeur/quoiFeur.helpers.ts index 3f2e419..99ed1b0 100644 --- a/src/modules/quoiFeur/quoiFeur.helpers.ts +++ b/src/modules/quoiFeur/quoiFeur.helpers.ts @@ -7,7 +7,7 @@ import { } from 'discord.js'; import { cache } from '../../core/cache'; -import { removeEmoji, removeMarkdown,removePunctuation } from '../../helpers/regex.helper'; +import { removeEmoji, removeMarkdown, removePunctuation } from '../../helpers/regex.helper'; const ONE_MINUTE = 1 * 60 * 1000;