diff --git a/src/modules/quoiFeur/quoiFeur.helpers.ts b/src/modules/quoiFeur/quoiFeur.helpers.ts index aef13d1..67beb63 100644 --- a/src/modules/quoiFeur/quoiFeur.helpers.ts +++ b/src/modules/quoiFeur/quoiFeur.helpers.ts @@ -1,47 +1,23 @@ -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))); -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('πŸ”‡'); - - const mutedRole = message.guild?.roles.cache.find((r) => r.name === MUTED_ON_COUBEH); - - if (!mutedRole?.id) return; - - await message.member?.roles.add(mutedRole.id); +const reactWithCoubeh = async (message: Message) => + reactWith(message, ['πŸ‡¨', 'πŸ‡΄', 'πŸ‡Ί', 'πŸ‡§', 'πŸ‡ͺ', 'πŸ‡­', 'πŸ”‡']); - setTimeout(() => { - message.member?.roles.remove(mutedRole.id).catch(console.error); - }, ONE_MINUTE * 5); -}; +const reactWithFeur = async (message: Message) => reactWith(message, ['πŸ‡«', 'πŸ‡ͺ', 'πŸ‡Ί', 'πŸ‡·']); export const reactOnEndWithQuoi = async (message: Message) => { if (!endWithQuoi(message.content)) return; @@ -52,37 +28,20 @@ export const reactOnEndWithQuoi = async (message: Message) => { const probability = 1 / 20; - 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'); + if (Math.random() <= probability) { + await reactWithCoubeh(message); + await message.member?.timeout( + ONE_MINUTE * 5, + `${message.member.displayName} have the cramptΓ©s`, + ); + return; } - 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(); - } + 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', []); @@ -91,21 +50,12 @@ 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'); }; 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', []); @@ -114,10 +64,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'],