Skip to content

Commit 6b7d6b0

Browse files
refactor: match new architecture
1 parent 62bd548 commit 6b7d6b0

File tree

4 files changed

+58
-34
lines changed

4 files changed

+58
-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: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
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+
} from 'discord.js';
28

3-
import { MUTED_BY_BOT } from '../../constants/roles';
49
import { cache } from '../../core/cache';
510
import { endWithQuoi } from '../../helpers/regex.helper';
611

712
const ONE_MINUTE = 1 * 60 * 1000;
13+
const MUTED_BY_BOT = 'Muted by bot';
814

915
const reactWithFeur = async (message: Message) => {
1016
await message.react('🇫');
@@ -33,7 +39,7 @@ const reactWithCoubeh = async (message: Message) => {
3339
}, ONE_MINUTE * 5);
3440
};
3541

36-
export const quoiFeurReact = async (message: Message) => {
42+
export const reactOnEndWithQuoi = async (message: Message) => {
3743
const channelIds = await cache.get('quoiFeurChannels', []);
3844
const channelHasGame = channelIds.find((channelId) => channelId === message.channelId);
3945
if (!channelHasGame) return;
@@ -49,22 +55,42 @@ export const quoiFeurReact = async (message: Message) => {
4955
}
5056
};
5157

52-
export const addQuoiFeurChannel = async (interaction: ChatInputCommandInteraction) => {
58+
export const createRoleMutedByBot = 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_BY_BOT);
63+
64+
return (
65+
existingMutedByBot ??
66+
guild.roles.create({
67+
name: MUTED_BY_BOT,
68+
})
69+
);
70+
};
71+
72+
export const deleteRoleMutedByBot = async (guild: Guild | null): Promise<void> => {
73+
if (!guild) {
74+
throw new Error('Guild is null in removeRoleMutedByBot');
75+
}
76+
const existingMutedByBot = guild.roles.cache.find((role) => role.name === MUTED_BY_BOT);
77+
78+
if (existingMutedByBot) {
79+
await existingMutedByBot.delete();
80+
}
81+
};
82+
83+
export const addQuoiFeurToChannel = async (interaction: ChatInputCommandInteraction) => {
5384
const channel = interaction.channel;
54-
if (!channel || !channel.isTextBased()) return;
85+
if (!channel || !channel.isTextBased() || channel.type !== ChannelType.GuildText) return;
5586

5687
const channels = await cache.get('quoiFeurChannels', []);
5788
if (channels.includes(channel.id)) {
5889
await interaction.reply('Quoi-feur is already enabled in this channel');
5990
return;
6091
}
6192

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-
}
66-
67-
if (!(channel instanceof TextChannel)) return;
93+
const role = await createRoleMutedByBot(interaction.guild);
6894
await channel.permissionOverwrites.create(role, {
6995
SendMessages: false,
7096
CreatePublicThreads: false,
@@ -77,9 +103,9 @@ export const addQuoiFeurChannel = async (interaction: ChatInputCommandInteractio
77103
await interaction.reply('Quoi-feur enabled in this channel');
78104
};
79105

80-
export const removeQuoiFeurChannel = async (interaction: ChatInputCommandInteraction) => {
106+
export const removeQuoiFeurFromChannel = async (interaction: ChatInputCommandInteraction) => {
81107
const channel = interaction.channel;
82-
if (!channel || !channel.isTextBased()) return;
108+
if (!channel || !channel.isTextBased() || channel.type !== ChannelType.GuildText) return;
83109

84110
const channels = await cache.get('quoiFeurChannels', []);
85111
if (!channels.includes(channel.id)) {
@@ -88,10 +114,9 @@ export const removeQuoiFeurChannel = async (interaction: ChatInputCommandInterac
88114
}
89115

90116
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);
117+
if (role) {
118+
await channel.permissionOverwrites.delete(role);
119+
}
95120
await cache.set(
96121
'quoiFeurChannels',
97122
channels.filter((channelId) => channelId !== channel.id),
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { SlashCommandBuilder } from 'discord.js';
22

33
import { config } from '../../config';
4-
import { MUTED_BY_BOT } from '../../constants/roles';
54
import type { BotModule } from '../../types/bot';
6-
import { addQuoiFeurChannel, quoiFeurReact, removeQuoiFeurChannel } from './quoiFeur.helpers';
5+
import {
6+
addQuoiFeurToChannel,
7+
deleteRoleMutedByBot,
8+
reactOnEndWithQuoi,
9+
removeQuoiFeurFromChannel,
10+
} from './quoiFeur.helpers';
711

812
export const quoiFeur: BotModule = {
913
slashCommands: [
@@ -12,36 +16,30 @@ export const quoiFeur: BotModule = {
1216
.setName('quoi-feur')
1317
.setDescription('Manage quoi-feur game in the channel')
1418
.addSubcommand((subcommand) =>
15-
subcommand.setName('add').setDescription('Add the quoi-feur game in the channel'),
19+
subcommand.setName('add').setDescription('Add the quoi-feur game to the channel'),
1620
)
1721
.addSubcommand((subcommand) =>
18-
subcommand.setName('remove').setDescription('Remove the quoi-feur game in the channel'),
22+
subcommand.setName('remove').setDescription('Remove the quoi-feur game from the channel'),
1923
)
2024
.toJSON(),
2125
handler: {
2226
add: async (interaction) => {
23-
await addQuoiFeurChannel(interaction);
27+
await addQuoiFeurToChannel(interaction).catch(console.error);
2428
},
2529
remove: async (interaction) => {
26-
await removeQuoiFeurChannel(interaction);
30+
await removeQuoiFeurFromChannel(interaction).catch(console.error);
2731
},
2832
},
2933
},
3034
],
3135
eventHandlers: {
3236
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-
});
37+
const guild = client.guilds.cache.get(config.discord.guildId) ?? null;
38+
// unmute everyone on bot restart
39+
await deleteRoleMutedByBot(guild).catch(console.error);
4240
},
4341
messageCreate: async (message) => {
44-
await quoiFeurReact(message);
42+
await reactOnEndWithQuoi(message).catch(console.error);
4543
},
4644
},
4745
};

0 commit comments

Comments
 (0)