|
| 1 | +import { |
| 2 | + ChannelType, |
| 3 | + type ChatInputCommandInteraction, |
| 4 | + Client, |
| 5 | + Guild, |
| 6 | + type Message, |
| 7 | + Role, |
| 8 | +} from 'discord.js'; |
| 9 | + |
| 10 | +import { cache } from '../../core/cache'; |
| 11 | +import { removeEmoji, removePunctuation } from '../../helpers/regex.helper'; |
| 12 | + |
| 13 | +const ONE_MINUTE = 1 * 60 * 1000; |
| 14 | +const MUTED_ON_COUBEH = 'Muted on Coubeh'; |
| 15 | + |
| 16 | +const quoiDetectorRegex = /\bquoi\s*$/i; |
| 17 | +const endWithQuoi = (text: string) => quoiDetectorRegex.test(removeEmoji(removePunctuation(text))); |
| 18 | + |
| 19 | +const reactWithFeur = async (message: Message) => { |
| 20 | + await message.react('🇫'); |
| 21 | + await message.react('🇪'); |
| 22 | + await message.react('🇺'); |
| 23 | + await message.react('🇷'); |
| 24 | +}; |
| 25 | + |
| 26 | +const reactWithCoubeh = async (message: Message) => { |
| 27 | + await message.react('🇨'); |
| 28 | + await message.react('🇴'); |
| 29 | + await message.react('🇺'); |
| 30 | + await message.react('🇧'); |
| 31 | + await message.react('🇪'); |
| 32 | + await message.react('🇭'); |
| 33 | + await message.react('🔇'); |
| 34 | + |
| 35 | + const mutedRole = message.guild?.roles.cache.find((r) => r.name === MUTED_ON_COUBEH); |
| 36 | + |
| 37 | + if (!mutedRole?.id) return; |
| 38 | + |
| 39 | + await message.member?.roles.add(mutedRole.id); |
| 40 | + |
| 41 | + setTimeout(() => { |
| 42 | + message.member?.roles.remove(mutedRole.id).catch(console.error); |
| 43 | + }, ONE_MINUTE * 5); |
| 44 | +}; |
| 45 | + |
| 46 | +export const reactOnEndWithQuoi = async (message: Message) => { |
| 47 | + if (!endWithQuoi(message.content)) return; |
| 48 | + |
| 49 | + const channelIds = await cache.get('quoiFeurChannels', []); |
| 50 | + const channelHasGame = channelIds.find((channelId) => channelId === message.channelId); |
| 51 | + if (!channelHasGame) return; |
| 52 | + |
| 53 | + const probability = 1 / 20; |
| 54 | + |
| 55 | + Math.random() <= probability ? await reactWithCoubeh(message) : await reactWithFeur(message); |
| 56 | +}; |
| 57 | + |
| 58 | +export const createRoleMutedOnCoubeh = async (guild: Guild | null): Promise<Role> => { |
| 59 | + if (!guild) { |
| 60 | + throw new Error('Guild is null in createRoleMutedByBot'); |
| 61 | + } |
| 62 | + const existingMutedByBot = guild.roles.cache.find((role) => role.name === MUTED_ON_COUBEH); |
| 63 | + |
| 64 | + return ( |
| 65 | + existingMutedByBot ?? |
| 66 | + guild.roles.create({ |
| 67 | + name: MUTED_ON_COUBEH, |
| 68 | + }) |
| 69 | + ); |
| 70 | +}; |
| 71 | + |
| 72 | +export const deleteRoleMutedOnCoubeh = async (client: Client<true>): Promise<void> => { |
| 73 | + const guilds = await client.guilds.fetch().then((guilds) => guilds.map((guild) => guild.fetch())); |
| 74 | + const roles = await Promise.all(guilds).then((guilds) => |
| 75 | + guilds.map((guild) => guild.roles.cache.find((role) => role.name === MUTED_ON_COUBEH)), |
| 76 | + ); |
| 77 | + |
| 78 | + for (const role of roles) { |
| 79 | + if (!role) continue; |
| 80 | + await role.delete(); |
| 81 | + } |
| 82 | +}; |
| 83 | + |
| 84 | +export const addQuoiFeurToChannel = async (interaction: ChatInputCommandInteraction) => { |
| 85 | + const channel = interaction.channel; |
| 86 | + if (!channel || !channel.isTextBased() || channel.type !== ChannelType.GuildText) return; |
| 87 | + |
| 88 | + const channels = await cache.get('quoiFeurChannels', []); |
| 89 | + if (channels.includes(channel.id)) { |
| 90 | + await interaction.reply('Quoi-feur is already enabled in this channel'); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + const role = await createRoleMutedOnCoubeh(interaction.guild); |
| 95 | + await channel.permissionOverwrites.create(role, { |
| 96 | + SendMessages: false, |
| 97 | + CreatePublicThreads: false, |
| 98 | + CreatePrivateThreads: false, |
| 99 | + SendMessagesInThreads: false, |
| 100 | + SendTTSMessages: false, |
| 101 | + AttachFiles: false, |
| 102 | + }); |
| 103 | + await cache.set('quoiFeurChannels', [...channels, channel.id]); |
| 104 | + await interaction.reply('Quoi-feur enabled in this channel'); |
| 105 | +}; |
| 106 | + |
| 107 | +export const removeQuoiFeurFromChannel = async (interaction: ChatInputCommandInteraction) => { |
| 108 | + const channel = interaction.channel; |
| 109 | + if (!channel || !channel.isTextBased() || channel.type !== ChannelType.GuildText) return; |
| 110 | + |
| 111 | + const channels = await cache.get('quoiFeurChannels', []); |
| 112 | + if (!channels.includes(channel.id)) { |
| 113 | + await interaction.reply('Quoi-feur is not enabled in this channel'); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + const role = interaction.guild?.roles.cache.find((r) => r.name === MUTED_ON_COUBEH); |
| 118 | + if (role) { |
| 119 | + await channel.permissionOverwrites.delete(role); |
| 120 | + } |
| 121 | + await cache.set( |
| 122 | + 'quoiFeurChannels', |
| 123 | + channels.filter((channelId) => channelId !== channel.id), |
| 124 | + ); |
| 125 | + await interaction.reply('Quoi-feur disabled in this channel'); |
| 126 | +}; |
0 commit comments