From 531555832fa51e08b99b645c897f633f7d705f64 Mon Sep 17 00:00:00 2001 From: Luca Montaigut Date: Wed, 6 Sep 2023 22:40:23 +0200 Subject: [PATCH 1/4] fix: muted with discord timeout instead of role --- src/modules/quoiFeur/quoiFeur.helpers.ts | 59 +----------------------- src/modules/quoiFeur/quoiFeur.module.ts | 3 -- 2 files changed, 2 insertions(+), 60 deletions(-) diff --git a/src/modules/quoiFeur/quoiFeur.helpers.ts b/src/modules/quoiFeur/quoiFeur.helpers.ts index aef13d1..3602a3b 100644 --- a/src/modules/quoiFeur/quoiFeur.helpers.ts +++ b/src/modules/quoiFeur/quoiFeur.helpers.ts @@ -1,17 +1,9 @@ -import { - ChannelType, - type ChatInputCommandInteraction, - Client, - Guild, - type Message, - Role, -} from 'discord.js'; +import { ChannelType, type ChatInputCommandInteraction, type Message } from 'discord.js'; import { cache } from '../../core/cache'; import { removeEmoji, removePunctuation } from '../../helpers/regex.helper'; const ONE_MINUTE = 1 * 60 * 1000; -const MUTED_ON_COUBEH = 'Muted on Coubeh'; const quoiDetectorRegex = /\bquoi\s*$/i; const endWithQuoi = (text: string) => quoiDetectorRegex.test(removeEmoji(removePunctuation(text))); @@ -32,15 +24,7 @@ const reactWithCoubeh = async (message: Message) => { await message.react('πŸ‡­'); await message.react('πŸ”‡'); - const mutedRole = message.guild?.roles.cache.find((r) => r.name === MUTED_ON_COUBEH); - - if (!mutedRole?.id) return; - - await message.member?.roles.add(mutedRole.id); - - setTimeout(() => { - message.member?.roles.remove(mutedRole.id).catch(console.error); - }, ONE_MINUTE * 5); + await message.member?.timeout(ONE_MINUTE * 5, 'User have the cramptΓ©s'); }; export const reactOnEndWithQuoi = async (message: Message) => { @@ -55,32 +39,6 @@ export const reactOnEndWithQuoi = async (message: Message) => { Math.random() <= probability ? await reactWithCoubeh(message) : await reactWithFeur(message); }; -export const createRoleMutedOnCoubeh = async (guild: Guild | null): Promise => { - if (!guild) { - throw new Error('Guild is null in createRoleMutedByBot'); - } - const existingMutedByBot = guild.roles.cache.find((role) => role.name === MUTED_ON_COUBEH); - - return ( - existingMutedByBot ?? - guild.roles.create({ - name: MUTED_ON_COUBEH, - }) - ); -}; - -export const deleteRoleMutedOnCoubeh = 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_ON_COUBEH)), - ); - - for (const role of roles) { - if (!role) continue; - await role.delete(); - } -}; - export const addQuoiFeurToChannel = async (interaction: ChatInputCommandInteraction) => { const channel = interaction.channel; if (!channel || !channel.isTextBased() || channel.type !== ChannelType.GuildText) return; @@ -91,15 +49,6 @@ export const addQuoiFeurToChannel = async (interaction: ChatInputCommandInteract return; } - const role = await createRoleMutedOnCoubeh(interaction.guild); - await channel.permissionOverwrites.create(role, { - SendMessages: false, - CreatePublicThreads: false, - CreatePrivateThreads: false, - SendMessagesInThreads: false, - SendTTSMessages: false, - AttachFiles: false, - }); await cache.set('quoiFeurChannels', [...channels, channel.id]); await interaction.reply('Quoi-feur enabled in this channel'); }; @@ -114,10 +63,6 @@ export const removeQuoiFeurFromChannel = async (interaction: ChatInputCommandInt return; } - const role = interaction.guild?.roles.cache.find((r) => r.name === MUTED_ON_COUBEH); - if (role) { - await channel.permissionOverwrites.delete(role); - } await cache.set( 'quoiFeurChannels', channels.filter((channelId) => channelId !== channel.id), diff --git a/src/modules/quoiFeur/quoiFeur.module.ts b/src/modules/quoiFeur/quoiFeur.module.ts index d24837d..ebc88d2 100644 --- a/src/modules/quoiFeur/quoiFeur.module.ts +++ b/src/modules/quoiFeur/quoiFeur.module.ts @@ -3,7 +3,6 @@ import { SlashCommandBuilder } from 'discord.js'; import type { BotModule } from '../../types/bot'; import { addQuoiFeurToChannel, - deleteRoleMutedOnCoubeh, reactOnEndWithQuoi, removeQuoiFeurFromChannel, } from './quoiFeur.helpers'; @@ -28,8 +27,6 @@ export const quoiFeur: BotModule = { }, ], eventHandlers: { - // unmute everyone in every server on bot restart - ready: deleteRoleMutedOnCoubeh, messageCreate: reactOnEndWithQuoi, }, intents: ['Guilds', 'GuildMessages', 'MessageContent', 'GuildMessageReactions'], From 6f0bf6dd55ba86db13758aff801b058758ee96ba Mon Sep 17 00:00:00 2001 From: Luca Montaigut Date: Wed, 6 Sep 2023 22:53:21 +0200 Subject: [PATCH 2/4] refactor: better react function --- src/modules/quoiFeur/quoiFeur.helpers.ts | 35 ++++++++++++------------ 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/modules/quoiFeur/quoiFeur.helpers.ts b/src/modules/quoiFeur/quoiFeur.helpers.ts index 3602a3b..d17b443 100644 --- a/src/modules/quoiFeur/quoiFeur.helpers.ts +++ b/src/modules/quoiFeur/quoiFeur.helpers.ts @@ -8,23 +8,18 @@ const ONE_MINUTE = 1 * 60 * 1000; const quoiDetectorRegex = /\bquoi\s*$/i; const endWithQuoi = (text: string) => quoiDetectorRegex.test(removeEmoji(removePunctuation(text))); -const reactWithFeur = async (message: Message) => { - await message.react('πŸ‡«'); - await message.react('πŸ‡ͺ'); - await message.react('πŸ‡Ί'); - await message.react('πŸ‡·'); +const reactWith = async (message: Message, reactions: string[]) => { + for (const reaction of reactions) { + await message.react(reaction); + } }; const reactWithCoubeh = async (message: Message) => { - await message.react('πŸ‡¨'); - await message.react('πŸ‡΄'); - await message.react('πŸ‡Ί'); - await message.react('πŸ‡§'); - await message.react('πŸ‡ͺ'); - await message.react('πŸ‡­'); - await message.react('πŸ”‡'); - - await message.member?.timeout(ONE_MINUTE * 5, 'User have the cramptΓ©s'); + await reactWith(message, ['πŸ‡¨', 'πŸ‡΄', 'πŸ‡Ί', 'πŸ‡§', 'πŸ‡ͺ', 'πŸ‡­', 'πŸ”‡']); +}; + +const reactWithFeur = async (message: Message) => { + await reactWith(message, ['πŸ‡«', 'πŸ‡ͺ', 'πŸ‡Ί', 'πŸ‡·']); }; export const reactOnEndWithQuoi = async (message: Message) => { @@ -36,11 +31,17 @@ export const reactOnEndWithQuoi = async (message: Message) => { const probability = 1 / 20; - Math.random() <= probability ? await reactWithCoubeh(message) : await reactWithFeur(message); + if (Math.random() <= probability) { + await reactWithCoubeh(message); + await message.member?.timeout(ONE_MINUTE * 5, 'User have the cramptΓ©s'); + return; + } + + await reactWithFeur(message); }; export const addQuoiFeurToChannel = async (interaction: ChatInputCommandInteraction) => { - const channel = interaction.channel; + const { channel } = interaction; if (!channel || !channel.isTextBased() || channel.type !== ChannelType.GuildText) return; const channels = await cache.get('quoiFeurChannels', []); @@ -54,7 +55,7 @@ export const addQuoiFeurToChannel = async (interaction: ChatInputCommandInteract }; export const removeQuoiFeurFromChannel = async (interaction: ChatInputCommandInteraction) => { - const channel = interaction.channel; + const { channel } = interaction; if (!channel || !channel.isTextBased() || channel.type !== ChannelType.GuildText) return; const channels = await cache.get('quoiFeurChannels', []); From 8444e4189749e5f655e3e2271a65ce6a11e4a10e Mon Sep 17 00:00:00 2001 From: Luca Montaigut Date: Wed, 6 Sep 2023 23:39:07 +0200 Subject: [PATCH 3/4] fix: return promise instead of await --- src/modules/quoiFeur/quoiFeur.helpers.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/modules/quoiFeur/quoiFeur.helpers.ts b/src/modules/quoiFeur/quoiFeur.helpers.ts index d17b443..e0335a3 100644 --- a/src/modules/quoiFeur/quoiFeur.helpers.ts +++ b/src/modules/quoiFeur/quoiFeur.helpers.ts @@ -14,13 +14,10 @@ const reactWith = async (message: Message, reactions: string[]) => { } }; -const reactWithCoubeh = async (message: Message) => { - await reactWith(message, ['πŸ‡¨', 'πŸ‡΄', 'πŸ‡Ί', 'πŸ‡§', 'πŸ‡ͺ', 'πŸ‡­', 'πŸ”‡']); -}; +const reactWithCoubeh = async (message: Message) => + reactWith(message, ['πŸ‡¨', 'πŸ‡΄', 'πŸ‡Ί', 'πŸ‡§', 'πŸ‡ͺ', 'πŸ‡­', 'πŸ”‡']); -const reactWithFeur = async (message: Message) => { - await reactWith(message, ['πŸ‡«', 'πŸ‡ͺ', 'πŸ‡Ί', 'πŸ‡·']); -}; +const reactWithFeur = async (message: Message) => reactWith(message, ['πŸ‡«', 'πŸ‡ͺ', 'πŸ‡Ί', 'πŸ‡·']); export const reactOnEndWithQuoi = async (message: Message) => { if (!endWithQuoi(message.content)) return; From 6235953b1998c3f453f00c3522f4edb00af66a4e Mon Sep 17 00:00:00 2001 From: Luca Montaigut Date: Wed, 6 Sep 2023 23:52:58 +0200 Subject: [PATCH 4/4] feat: better reason message --- src/modules/quoiFeur/quoiFeur.helpers.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/modules/quoiFeur/quoiFeur.helpers.ts b/src/modules/quoiFeur/quoiFeur.helpers.ts index e0335a3..67beb63 100644 --- a/src/modules/quoiFeur/quoiFeur.helpers.ts +++ b/src/modules/quoiFeur/quoiFeur.helpers.ts @@ -30,7 +30,10 @@ export const reactOnEndWithQuoi = async (message: Message) => { if (Math.random() <= probability) { await reactWithCoubeh(message); - await message.member?.timeout(ONE_MINUTE * 5, 'User have the cramptΓ©s'); + await message.member?.timeout( + ONE_MINUTE * 5, + `${message.member.displayName} have the cramptΓ©s`, + ); return; }