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' ;
2
9
3
- import { MUTED_BY_BOT } from '../../constants/roles' ;
4
10
import { cache } from '../../core/cache' ;
5
11
import { endWithQuoi } from '../../helpers/regex.helper' ;
6
12
7
13
const ONE_MINUTE = 1 * 60 * 1000 ;
14
+ const MUTED_BY_BOT = 'Muted by bot' ;
8
15
9
16
const reactWithFeur = async ( message : Message ) => {
10
17
await message . react ( '🇫' ) ;
@@ -33,7 +40,21 @@ const reactWithCoubeh = async (message: Message) => {
33
40
} , ONE_MINUTE * 5 ) ;
34
41
} ;
35
42
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 ) => {
37
58
const channelIds = await cache . get ( 'quoiFeurChannels' , [ ] ) ;
38
59
const channelHasGame = channelIds . find ( ( channelId ) => channelId === message . channelId ) ;
39
60
if ( ! channelHasGame ) return ;
@@ -49,7 +70,7 @@ export const quoiFeurReact = async (message: Message) => {
49
70
}
50
71
} ;
51
72
52
- export const addQuoiFeurChannel = async ( interaction : ChatInputCommandInteraction ) => {
73
+ export const addQuoiFeurToChannel = async ( interaction : ChatInputCommandInteraction ) => {
53
74
const channel = interaction . channel ;
54
75
if ( ! channel || ! channel . isTextBased ( ) ) return ;
55
76
@@ -59,10 +80,8 @@ export const addQuoiFeurChannel = async (interaction: ChatInputCommandInteractio
59
80
return ;
60
81
}
61
82
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 ) ) ;
66
85
67
86
if ( ! ( channel instanceof TextChannel ) ) return ;
68
87
await channel . permissionOverwrites . create ( role , {
@@ -77,9 +96,9 @@ export const addQuoiFeurChannel = async (interaction: ChatInputCommandInteractio
77
96
await interaction . reply ( 'Quoi-feur enabled in this channel' ) ;
78
97
} ;
79
98
80
- export const removeQuoiFeurChannel = async ( interaction : ChatInputCommandInteraction ) => {
99
+ export const removeQuoiFeurFromChannel = async ( interaction : ChatInputCommandInteraction ) => {
81
100
const channel = interaction . channel ;
82
- if ( ! channel || ! channel . isTextBased ( ) ) return ;
101
+ if ( ! channel || ! channel . isTextBased ( ) || channel . type !== ChannelType . GuildText ) return ;
83
102
84
103
const channels = await cache . get ( 'quoiFeurChannels' , [ ] ) ;
85
104
if ( ! channels . includes ( channel . id ) ) {
@@ -88,10 +107,9 @@ export const removeQuoiFeurChannel = async (interaction: ChatInputCommandInterac
88
107
}
89
108
90
109
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
+ }
95
113
await cache . set (
96
114
'quoiFeurChannels' ,
97
115
channels . filter ( ( channelId ) => channelId !== channel . id ) ,
0 commit comments