Skip to content

Commit 58ccddd

Browse files
fix: muted on quoi-feur feature (#61)
1 parent a2eea82 commit 58ccddd

File tree

2 files changed

+18
-75
lines changed

2 files changed

+18
-75
lines changed

src/modules/quoiFeur/quoiFeur.helpers.ts

Lines changed: 18 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,23 @@
1-
import {
2-
ChannelType,
3-
type ChatInputCommandInteraction,
4-
Client,
5-
Guild,
6-
type Message,
7-
Role,
8-
} from 'discord.js';
1+
import { ChannelType, type ChatInputCommandInteraction, type Message } from 'discord.js';
92

103
import { cache } from '../../core/cache';
114
import { removeEmoji, removePunctuation } from '../../helpers/regex.helper';
125

136
const ONE_MINUTE = 1 * 60 * 1000;
14-
const MUTED_ON_COUBEH = 'Muted on Coubeh';
157

168
const quoiDetectorRegex = /\bquoi\s*$/i;
179
const endWithQuoi = (text: string) => quoiDetectorRegex.test(removeEmoji(removePunctuation(text)));
1810

19-
const reactWithFeur = async (message: Message) => {
20-
await message.react('🇫');
21-
await message.react('🇪');
22-
await message.react('🇺');
23-
await message.react('🇷');
11+
const reactWith = async (message: Message, reactions: string[]) => {
12+
for (const reaction of reactions) {
13+
await message.react(reaction);
14+
}
2415
};
2516

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);
17+
const reactWithCoubeh = async (message: Message) =>
18+
reactWith(message, ['🇨', '🇴', '🇺', '🇧', '🇪', '🇭', '🔇']);
4019

41-
setTimeout(() => {
42-
message.member?.roles.remove(mutedRole.id).catch(console.error);
43-
}, ONE_MINUTE * 5);
44-
};
20+
const reactWithFeur = async (message: Message) => reactWith(message, ['🇫', '🇪', '🇺', '🇷']);
4521

4622
export const reactOnEndWithQuoi = async (message: Message) => {
4723
if (!endWithQuoi(message.content)) return;
@@ -52,37 +28,20 @@ export const reactOnEndWithQuoi = async (message: Message) => {
5228

5329
const probability = 1 / 20;
5430

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');
31+
if (Math.random() <= probability) {
32+
await reactWithCoubeh(message);
33+
await message.member?.timeout(
34+
ONE_MINUTE * 5,
35+
`${message.member.displayName} have the cramptés`,
36+
);
37+
return;
6138
}
62-
const existingMutedByBot = guild.roles.cache.find((role) => role.name === MUTED_ON_COUBEH);
6339

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-
}
40+
await reactWithFeur(message);
8241
};
8342

8443
export const addQuoiFeurToChannel = async (interaction: ChatInputCommandInteraction) => {
85-
const channel = interaction.channel;
44+
const { channel } = interaction;
8645
if (!channel || !channel.isTextBased() || channel.type !== ChannelType.GuildText) return;
8746

8847
const channels = await cache.get('quoiFeurChannels', []);
@@ -91,21 +50,12 @@ export const addQuoiFeurToChannel = async (interaction: ChatInputCommandInteract
9150
return;
9251
}
9352

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-
});
10353
await cache.set('quoiFeurChannels', [...channels, channel.id]);
10454
await interaction.reply('Quoi-feur enabled in this channel');
10555
};
10656

10757
export const removeQuoiFeurFromChannel = async (interaction: ChatInputCommandInteraction) => {
108-
const channel = interaction.channel;
58+
const { channel } = interaction;
10959
if (!channel || !channel.isTextBased() || channel.type !== ChannelType.GuildText) return;
11060

11161
const channels = await cache.get('quoiFeurChannels', []);
@@ -114,10 +64,6 @@ export const removeQuoiFeurFromChannel = async (interaction: ChatInputCommandInt
11464
return;
11565
}
11666

117-
const role = interaction.guild?.roles.cache.find((r) => r.name === MUTED_ON_COUBEH);
118-
if (role) {
119-
await channel.permissionOverwrites.delete(role);
120-
}
12167
await cache.set(
12268
'quoiFeurChannels',
12369
channels.filter((channelId) => channelId !== channel.id),

src/modules/quoiFeur/quoiFeur.module.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { SlashCommandBuilder } from 'discord.js';
33
import type { BotModule } from '../../types/bot';
44
import {
55
addQuoiFeurToChannel,
6-
deleteRoleMutedOnCoubeh,
76
reactOnEndWithQuoi,
87
removeQuoiFeurFromChannel,
98
} from './quoiFeur.helpers';
@@ -28,8 +27,6 @@ export const quoiFeur: BotModule = {
2827
},
2928
],
3029
eventHandlers: {
31-
// unmute everyone in every server on bot restart
32-
ready: deleteRoleMutedOnCoubeh,
3330
messageCreate: reactOnEndWithQuoi,
3431
},
3532
intents: ['Guilds', 'GuildMessages', 'MessageContent', 'GuildMessageReactions'],

0 commit comments

Comments
 (0)