From 92d14d99f19331b58d9e4596121a34270dc2fb39 Mon Sep 17 00:00:00 2001 From: Luca Montaigut Date: Mon, 4 Sep 2023 18:18:56 +0200 Subject: [PATCH] fix: pr returns adjustement --- src/helpers/regex.helper.ts | 3 --- src/modules/quoiFeur/quoiFeur.helpers.ts | 21 +++++++++++++-------- src/modules/quoiFeur/quoiFeur.module.ts | 20 +++++--------------- 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/src/helpers/regex.helper.ts b/src/helpers/regex.helper.ts index bea7d2c7..7a52d3af 100644 --- a/src/helpers/regex.helper.ts +++ b/src/helpers/regex.helper.ts @@ -3,7 +3,6 @@ const socialNetworksUrlRegex = new RegExp( ); const punctuationRegex = new RegExp(/[.,!?]/g); const emojiRegex = new RegExp(/(\p{Extended_Pictographic}|\p{Emoji_Component})/gu); -const quoiDetectorRegex = new RegExp(/\b\s*[q][u][o][i]\s*$/i); export const isASocialNetworkUrl = (url: string): boolean => { return socialNetworksUrlRegex.test(url); @@ -11,5 +10,3 @@ export const isASocialNetworkUrl = (url: string): boolean => { export const removePunctuation = (text: string) => text.replaceAll(punctuationRegex, ''); export const removeEmoji = (text: string) => text.replaceAll(emojiRegex, ''); -export const endWithQuoi = (text: string) => - quoiDetectorRegex.test(removeEmoji(removePunctuation(text))); diff --git a/src/modules/quoiFeur/quoiFeur.helpers.ts b/src/modules/quoiFeur/quoiFeur.helpers.ts index 136968df..ab769b33 100644 --- a/src/modules/quoiFeur/quoiFeur.helpers.ts +++ b/src/modules/quoiFeur/quoiFeur.helpers.ts @@ -1,17 +1,21 @@ import { ChannelType, type ChatInputCommandInteraction, + Client, Guild, type Message, Role, } from 'discord.js'; import { cache } from '../../core/cache'; -import { endWithQuoi } from '../../helpers/regex.helper'; +import { removeEmoji, removePunctuation } from '../../helpers/regex.helper'; const ONE_MINUTE = 1 * 60 * 1000; const MUTED_BY_BOT = 'Muted by bot'; +const quoiDetectorRegex = new RegExp(/\b\s*[q][u][o][i]\s*$/i); +const endWithQuoi = (text: string) => quoiDetectorRegex.test(removeEmoji(removePunctuation(text))); + const reactWithFeur = async (message: Message) => { await message.react('🇫'); await message.react('🇪'); @@ -69,14 +73,15 @@ export const createRoleMutedByBot = async (guild: Guild | null): Promise = ); }; -export const deleteRoleMutedByBot = async (guild: Guild | null): Promise => { - if (!guild) { - throw new Error('Guild is null in removeRoleMutedByBot'); - } - const existingMutedByBot = guild.roles.cache.find((role) => role.name === MUTED_BY_BOT); +export const deleteRoleMutedByBot = async (client: Client): Promise => { + const guilds = await client.guilds.fetch().then((guilds) => guilds.map((guild) => guild.fetch())); + const roles = await Promise.all(guilds).then((guilds) => + guilds.map((guild) => guild.roles.cache.find((role) => role.name === MUTED_BY_BOT)), + ); - if (existingMutedByBot) { - await existingMutedByBot.delete(); + for (const role of roles) { + if (!role) continue; + await role.delete(); } }; diff --git a/src/modules/quoiFeur/quoiFeur.module.ts b/src/modules/quoiFeur/quoiFeur.module.ts index 3557dac8..45ec5428 100644 --- a/src/modules/quoiFeur/quoiFeur.module.ts +++ b/src/modules/quoiFeur/quoiFeur.module.ts @@ -1,6 +1,5 @@ import { SlashCommandBuilder } from 'discord.js'; -import { config } from '../../config'; import type { BotModule } from '../../types/bot'; import { addQuoiFeurToChannel, @@ -23,23 +22,14 @@ export const quoiFeur: BotModule = { ) .toJSON(), handler: { - add: async (interaction) => { - await addQuoiFeurToChannel(interaction).catch(console.error); - }, - remove: async (interaction) => { - await removeQuoiFeurFromChannel(interaction).catch(console.error); - }, + add: addQuoiFeurToChannel, + remove: removeQuoiFeurFromChannel, }, }, ], eventHandlers: { - ready: async (client) => { - const guild = client.guilds.cache.get(config.discord.guildId) ?? null; - // unmute everyone on bot restart - await deleteRoleMutedByBot(guild).catch(console.error); - }, - messageCreate: async (message) => { - await reactOnEndWithQuoi(message).catch(console.error); - }, + // unmute everyone in every server on bot restart + ready: deleteRoleMutedByBot, + messageCreate: reactOnEndWithQuoi, }, };