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' ;
9
2
10
3
import { cache } from '../../core/cache' ;
11
4
import { removeEmoji , removePunctuation } from '../../helpers/regex.helper' ;
12
5
13
6
const ONE_MINUTE = 1 * 60 * 1000 ;
14
- const MUTED_ON_COUBEH = 'Muted on Coubeh' ;
15
7
16
8
const quoiDetectorRegex = / \b q u o i \s * $ / i;
17
9
const endWithQuoi = ( text : string ) => quoiDetectorRegex . test ( removeEmoji ( removePunctuation ( text ) ) ) ;
18
10
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
+ }
24
15
} ;
25
16
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 , [ '🇨' , '🇴' , '🇺' , '🇧' , '🇪' , '🇭' , '🔇' ] ) ;
40
19
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 , [ '🇫' , '🇪' , '🇺' , '🇷' ] ) ;
45
21
46
22
export const reactOnEndWithQuoi = async ( message : Message ) => {
47
23
if ( ! endWithQuoi ( message . content ) ) return ;
@@ -52,37 +28,20 @@ export const reactOnEndWithQuoi = async (message: Message) => {
52
28
53
29
const probability = 1 / 20 ;
54
30
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 ;
61
38
}
62
- const existingMutedByBot = guild . roles . cache . find ( ( role ) => role . name === MUTED_ON_COUBEH ) ;
63
39
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 ) ;
82
41
} ;
83
42
84
43
export const addQuoiFeurToChannel = async ( interaction : ChatInputCommandInteraction ) => {
85
- const channel = interaction . channel ;
44
+ const { channel } = interaction ;
86
45
if ( ! channel || ! channel . isTextBased ( ) || channel . type !== ChannelType . GuildText ) return ;
87
46
88
47
const channels = await cache . get ( 'quoiFeurChannels' , [ ] ) ;
@@ -91,21 +50,12 @@ export const addQuoiFeurToChannel = async (interaction: ChatInputCommandInteract
91
50
return ;
92
51
}
93
52
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
- } ) ;
103
53
await cache . set ( 'quoiFeurChannels' , [ ...channels , channel . id ] ) ;
104
54
await interaction . reply ( 'Quoi-feur enabled in this channel' ) ;
105
55
} ;
106
56
107
57
export const removeQuoiFeurFromChannel = async ( interaction : ChatInputCommandInteraction ) => {
108
- const channel = interaction . channel ;
58
+ const { channel } = interaction ;
109
59
if ( ! channel || ! channel . isTextBased ( ) || channel . type !== ChannelType . GuildText ) return ;
110
60
111
61
const channels = await cache . get ( 'quoiFeurChannels' , [ ] ) ;
@@ -114,10 +64,6 @@ export const removeQuoiFeurFromChannel = async (interaction: ChatInputCommandInt
114
64
return ;
115
65
}
116
66
117
- const role = interaction . guild ?. roles . cache . find ( ( r ) => r . name === MUTED_ON_COUBEH ) ;
118
- if ( role ) {
119
- await channel . permissionOverwrites . delete ( role ) ;
120
- }
121
67
await cache . set (
122
68
'quoiFeurChannels' ,
123
69
channels . filter ( ( channelId ) => channelId !== channel . id ) ,
0 commit comments