Skip to content

fix: muted on quoi-feur feature #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 18 additions & 72 deletions src/modules/quoiFeur/quoiFeur.helpers.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Role> => {
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<true>): Promise<void> => {
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', []);
Expand All @@ -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', []);
Expand All @@ -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),
Expand Down
3 changes: 0 additions & 3 deletions src/modules/quoiFeur/quoiFeur.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { SlashCommandBuilder } from 'discord.js';
import type { BotModule } from '../../types/bot';
import {
addQuoiFeurToChannel,
deleteRoleMutedOnCoubeh,
reactOnEndWithQuoi,
removeQuoiFeurFromChannel,
} from './quoiFeur.helpers';
Expand All @@ -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'],
Expand Down