Skip to content

Commit 08ae209

Browse files
refactor: match new architecture
1 parent 62bd548 commit 08ae209

File tree

4 files changed

+44
-34
lines changed

4 files changed

+44
-34
lines changed

src/constants/roles.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/modules/modules.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { coolLinksManagement } from './coolLinksManagement/coolLinksManagement.module';
22
import { fart } from './fart/fart.module';
33
import { patternReplace } from './patternReplace/patternReplace.module';
4+
import { quoiFeur } from './quoiFeur/quoiFeur.module';
45
import { voiceOnDemand } from './voiceOnDemand/voiceOnDemand.module';
56

67
export const modules = {
78
fart,
89
voiceOnDemand,
910
coolLinksManagement,
1011
patternReplace,
12+
quoiFeur,
1113
};

src/modules/quoiFeur/quoiFeur.helpers.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
import { type ChatInputCommandInteraction, type Message, TextChannel } from 'discord.js';
1+
import {
2+
ChannelType,
3+
type ChatInputCommandInteraction,
4+
Guild,
5+
type Message,
6+
Role,
7+
TextChannel,
8+
} from 'discord.js';
29

3-
import { MUTED_BY_BOT } from '../../constants/roles';
410
import { cache } from '../../core/cache';
511
import { endWithQuoi } from '../../helpers/regex.helper';
612

713
const ONE_MINUTE = 1 * 60 * 1000;
14+
const MUTED_BY_BOT = 'Muted by bot';
815

916
const reactWithFeur = async (message: Message) => {
1017
await message.react('🇫');
@@ -33,7 +40,21 @@ const reactWithCoubeh = async (message: Message) => {
3340
}, ONE_MINUTE * 5);
3441
};
3542

36-
export const quoiFeurReact = async (message: Message) => {
43+
export const createRoleMutedByBot = async (guild: Guild | null): Promise<Role> => {
44+
if (!guild) {
45+
throw new Error('Guild is null in createRoleMutedByBot');
46+
}
47+
const hasMutedByBot = guild.roles.cache.find((role) => role.name === MUTED_BY_BOT);
48+
if (hasMutedByBot) {
49+
// delete to unmute all members and re-create it
50+
await hasMutedByBot.delete();
51+
}
52+
return guild.roles.create({
53+
name: MUTED_BY_BOT,
54+
});
55+
};
56+
57+
export const reactOnEndWithQuoi = async (message: Message) => {
3758
const channelIds = await cache.get('quoiFeurChannels', []);
3859
const channelHasGame = channelIds.find((channelId) => channelId === message.channelId);
3960
if (!channelHasGame) return;
@@ -49,7 +70,7 @@ export const quoiFeurReact = async (message: Message) => {
4970
}
5071
};
5172

52-
export const addQuoiFeurChannel = async (interaction: ChatInputCommandInteraction) => {
73+
export const addQuoiFeurToChannel = async (interaction: ChatInputCommandInteraction) => {
5374
const channel = interaction.channel;
5475
if (!channel || !channel.isTextBased()) return;
5576

@@ -59,10 +80,8 @@ export const addQuoiFeurChannel = async (interaction: ChatInputCommandInteractio
5980
return;
6081
}
6182

62-
const role = interaction.guild?.roles.cache.find((r) => r.name === MUTED_BY_BOT);
63-
if (!role) {
64-
throw new Error(`Role ${MUTED_BY_BOT} is missing`);
65-
}
83+
const existingRole = interaction.guild?.roles.cache.find((r) => r.name === MUTED_BY_BOT);
84+
const role = existingRole ?? (await createRoleMutedByBot(interaction.guild));
6685

6786
if (!(channel instanceof TextChannel)) return;
6887
await channel.permissionOverwrites.create(role, {
@@ -77,9 +96,9 @@ export const addQuoiFeurChannel = async (interaction: ChatInputCommandInteractio
7796
await interaction.reply('Quoi-feur enabled in this channel');
7897
};
7998

80-
export const removeQuoiFeurChannel = async (interaction: ChatInputCommandInteraction) => {
99+
export const removeQuoiFeurFromChannel = async (interaction: ChatInputCommandInteraction) => {
81100
const channel = interaction.channel;
82-
if (!channel || !channel.isTextBased()) return;
101+
if (!channel || !channel.isTextBased() || channel.type !== ChannelType.GuildText) return;
83102

84103
const channels = await cache.get('quoiFeurChannels', []);
85104
if (!channels.includes(channel.id)) {
@@ -88,10 +107,9 @@ export const removeQuoiFeurChannel = async (interaction: ChatInputCommandInterac
88107
}
89108

90109
const role = interaction.guild?.roles.cache.find((r) => r.name === MUTED_BY_BOT);
91-
if (!role) return;
92-
if (!(channel instanceof TextChannel)) return;
93-
94-
await channel.permissionOverwrites.delete(role);
110+
if (role) {
111+
await channel.permissionOverwrites.delete(role);
112+
}
95113
await cache.set(
96114
'quoiFeurChannels',
97115
channels.filter((channelId) => channelId !== channel.id),
Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { SlashCommandBuilder } from 'discord.js';
22

3-
import { config } from '../../config';
4-
import { MUTED_BY_BOT } from '../../constants/roles';
53
import type { BotModule } from '../../types/bot';
6-
import { addQuoiFeurChannel, quoiFeurReact, removeQuoiFeurChannel } from './quoiFeur.helpers';
4+
import {
5+
addQuoiFeurToChannel,
6+
reactOnEndWithQuoi,
7+
removeQuoiFeurFromChannel,
8+
} from './quoiFeur.helpers';
79

810
export const quoiFeur: BotModule = {
911
slashCommands: [
@@ -12,36 +14,25 @@ export const quoiFeur: BotModule = {
1214
.setName('quoi-feur')
1315
.setDescription('Manage quoi-feur game in the channel')
1416
.addSubcommand((subcommand) =>
15-
subcommand.setName('add').setDescription('Add the quoi-feur game in the channel'),
17+
subcommand.setName('add').setDescription('Add the quoi-feur game to the channel'),
1618
)
1719
.addSubcommand((subcommand) =>
18-
subcommand.setName('remove').setDescription('Remove the quoi-feur game in the channel'),
20+
subcommand.setName('remove').setDescription('Remove the quoi-feur game from the channel'),
1921
)
2022
.toJSON(),
2123
handler: {
2224
add: async (interaction) => {
23-
await addQuoiFeurChannel(interaction);
25+
await addQuoiFeurToChannel(interaction).catch(console.error);
2426
},
2527
remove: async (interaction) => {
26-
await removeQuoiFeurChannel(interaction);
28+
await removeQuoiFeurFromChannel(interaction).catch(console.error);
2729
},
2830
},
2931
},
3032
],
3133
eventHandlers: {
32-
ready: async (client) => {
33-
const guild = await client.guilds.fetch(config.discord.guildId);
34-
const hasMutedByBot = guild.roles.cache.find((role) => role.name === MUTED_BY_BOT);
35-
if (hasMutedByBot) {
36-
// delete to unmute all members and re-create it
37-
await hasMutedByBot.delete();
38-
}
39-
await guild.roles.create({
40-
name: MUTED_BY_BOT,
41-
});
42-
},
4334
messageCreate: async (message) => {
44-
await quoiFeurReact(message);
35+
await reactOnEndWithQuoi(message).catch(console.error);
4536
},
4637
},
4738
};

0 commit comments

Comments
 (0)