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' ;
2
8
3
- import { MUTED_BY_BOT } from '../../constants/roles' ;
4
9
import { cache } from '../../core/cache' ;
5
10
import { endWithQuoi } from '../../helpers/regex.helper' ;
6
11
7
12
const ONE_MINUTE = 1 * 60 * 1000 ;
13
+ const MUTED_BY_BOT = 'Muted by bot' ;
8
14
9
15
const reactWithFeur = async ( message : Message ) => {
10
16
await message . react ( '🇫' ) ;
@@ -33,7 +39,7 @@ const reactWithCoubeh = async (message: Message) => {
33
39
} , ONE_MINUTE * 5 ) ;
34
40
} ;
35
41
36
- export const quoiFeurReact = async ( message : Message ) => {
42
+ export const reactOnEndWithQuoi = async ( message : Message ) => {
37
43
const channelIds = await cache . get ( 'quoiFeurChannels' , [ ] ) ;
38
44
const channelHasGame = channelIds . find ( ( channelId ) => channelId === message . channelId ) ;
39
45
if ( ! channelHasGame ) return ;
@@ -49,22 +55,42 @@ export const quoiFeurReact = async (message: Message) => {
49
55
}
50
56
} ;
51
57
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 ) => {
53
84
const channel = interaction . channel ;
54
- if ( ! channel || ! channel . isTextBased ( ) ) return ;
85
+ if ( ! channel || ! channel . isTextBased ( ) || channel . type !== ChannelType . GuildText ) return ;
55
86
56
87
const channels = await cache . get ( 'quoiFeurChannels' , [ ] ) ;
57
88
if ( channels . includes ( channel . id ) ) {
58
89
await interaction . reply ( 'Quoi-feur is already enabled in this channel' ) ;
59
90
return ;
60
91
}
61
92
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 ) ;
68
94
await channel . permissionOverwrites . create ( role , {
69
95
SendMessages : false ,
70
96
CreatePublicThreads : false ,
@@ -77,9 +103,9 @@ export const addQuoiFeurChannel = async (interaction: ChatInputCommandInteractio
77
103
await interaction . reply ( 'Quoi-feur enabled in this channel' ) ;
78
104
} ;
79
105
80
- export const removeQuoiFeurChannel = async ( interaction : ChatInputCommandInteraction ) => {
106
+ export const removeQuoiFeurFromChannel = async ( interaction : ChatInputCommandInteraction ) => {
81
107
const channel = interaction . channel ;
82
- if ( ! channel || ! channel . isTextBased ( ) ) return ;
108
+ if ( ! channel || ! channel . isTextBased ( ) || channel . type !== ChannelType . GuildText ) return ;
83
109
84
110
const channels = await cache . get ( 'quoiFeurChannels' , [ ] ) ;
85
111
if ( ! channels . includes ( channel . id ) ) {
@@ -88,10 +114,9 @@ export const removeQuoiFeurChannel = async (interaction: ChatInputCommandInterac
88
114
}
89
115
90
116
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
+ }
95
120
await cache . set (
96
121
'quoiFeurChannels' ,
97
122
channels . filter ( ( channelId ) => channelId !== channel . id ) ,
0 commit comments