1
- const {
2
- findOrCreateChannel,
3
- getChannelObject,
4
- findCategoryWithCourseName,
5
- getDefaultChannelObjects,
6
- findOrCreateRoleWithName,
7
- getCategoryObject,
8
- getCategoryChannelPermissionOverwrites,
9
- createInvitation,
10
- changeCourseRoles,
11
- updateAnnouncementChannelMessage,
12
- setEmojisLock,
13
- setEmojisUnlock,
14
- setEmojisHide,
15
- setEmojisUnhide,
16
- setCoursePositionABC,
17
- updateGuide,
18
- updateInviteLinks } = require ( "../discordBot/services/service" ) ;
19
- const { lockTelegramCourse, unlockTelegramCourse } = require ( "../bridge/service" ) ;
20
- const { findCourseFromDbById } = require ( "./services/courseService" ) ;
21
- const { findUserByDbId } = require ( "./services/userService" ) ;
22
- const { courseAdminRole, facultyRole } = require ( "../../config.json" ) ;
23
- const { Op } = require ( "sequelize" ) ;
24
- const { editChannelNames, createDefaultChannelsToDatabase } = require ( "../db/services/channelService" ) ;
25
- const { joinedUsersCounter } = require ( "../promMetrics/promCounters" ) ;
1
+ const { initCourseMemberHooks } = require ( "./hooks/courseMemberHooks" ) ;
2
+ const { initCourseHooks } = require ( "./hooks/courseHooks" ) ;
3
+ const { initChannelHooks } = require ( "./hooks/channelHooks" ) ;
4
+ const { initUserHooks } = require ( "./hooks/userHooks" ) ;
26
5
27
6
const initHooks = ( guild , models ) => {
28
7
initChannelHooks ( guild , models ) ;
@@ -31,220 +10,4 @@ const initHooks = (guild, models) => {
31
10
initCourseMemberHooks ( guild , models ) ;
32
11
} ;
33
12
34
- const initChannelHooks = ( guild , models ) => {
35
- const channelModel = models . Channel ;
36
- const courseModel = models . Course ;
37
- channelModel . addHook ( "afterBulkDestroy" , ( channel ) => {
38
- guild . channels . cache . find ( c => c . name === channel . where . name [ Op . iLike ] ) ?. delete ( ) ;
39
- } ) ;
40
-
41
- channelModel . addHook ( "afterCreate" , async ( channel ) => {
42
- const course = await findCourseFromDbById ( channel . courseId , courseModel ) ;
43
- const channelName = channel . name . replace ( `${ course . name } _` , "" ) ;
44
-
45
- if ( ! channel . defaultChannel ) {
46
- const category = findCategoryWithCourseName ( course . name , guild ) ;
47
- const channelObject = getChannelObject ( course . name , channelName , category ) ;
48
-
49
- const createdChannel = await findOrCreateChannel ( channelObject , guild ) ;
50
- await channel . update ( { discordId : createdChannel . id } ) ;
51
- }
52
- } ) ;
53
-
54
- channelModel . addHook ( "afterBulkCreate" , async ( channel ) => {
55
- const course = await findCourseFromDbById ( channel [ 0 ] . courseId , courseModel ) ;
56
-
57
- const student = await findOrCreateRoleWithName ( course . name , guild ) ;
58
- const admin = await findOrCreateRoleWithName ( `${ course . name } ${ courseAdminRole } ` , guild ) ;
59
- const categoryObject = getCategoryObject ( course . name , getCategoryChannelPermissionOverwrites ( guild , admin , student ) ) ;
60
- const category = await findOrCreateChannel ( categoryObject , guild ) ;
61
-
62
- const channelObjects = await getDefaultChannelObjects ( guild , course . name , student , admin , category ) ;
63
- await Promise . all ( channelObjects . map ( async channelObject => {
64
- const createdChannel = await findOrCreateChannel ( channelObject , guild ) ;
65
- const channelInstance = channel . find ( c => c . name === createdChannel . name ) ;
66
- await channelInstance . update ( { discordId : createdChannel . id } ) ;
67
- } ) ) ;
68
-
69
- await setCoursePositionABC ( guild , categoryObject . name , courseModel ) ;
70
- await createInvitation ( guild , course . name ) ;
71
- await guild . client . emit ( "COURSES_CHANGED" , courseModel ) ;
72
- await updateGuide ( guild , models ) ;
73
- } ) ;
74
-
75
- channelModel . addHook ( "afterUpdate" , async ( channel ) => {
76
- if ( channel . _changed . has ( "name" ) && channel . _previousDataValues . name ) {
77
- const channelObject = guild . channels . cache
78
- . find ( c => c . name === channel . _previousDataValues . name ) ;
79
- await channelObject . setName ( channel . name ) ;
80
- }
81
-
82
- if ( channel . _changed . has ( "topic" ) ) {
83
- const channelObject = guild . channels . cache . find ( c => c . name === channel . name ) ;
84
- await channelObject . setTopic ( channel . topic ) ;
85
- }
86
-
87
- if ( channel . _changed . has ( "hidden" ) ) {
88
- const course = await findCourseFromDbById ( channel . courseId , courseModel ) ;
89
- const student = await findOrCreateRoleWithName ( course . name , guild ) ;
90
- const channelObject = guild . channels . cache
91
- . find ( c => c . name === channel . dataValues . name ) ;
92
- if ( channel . hidden ) {
93
- await channelObject . permissionOverwrites . create ( student , {
94
- VIEW_CHANNEL : false ,
95
- SEND_MESSAGES : false ,
96
- } ) ;
97
- }
98
- else {
99
- await channelObject . permissionOverwrites . create ( student , {
100
- VIEW_CHANNEL : true ,
101
- SEND_MESSAGES : true ,
102
- } ) ;
103
- }
104
- }
105
- } ) ;
106
- } ;
107
-
108
- const initCourseHooks = ( guild , models ) => {
109
- models . Course . addHook ( "afterBulkDestroy" , async ( course ) => {
110
- const courseName = course . where . name [ Op . iLike ] ;
111
- const category = findCategoryWithCourseName ( courseName , guild ) ;
112
-
113
- await Promise . all ( guild . channels . cache
114
- . filter ( c => c . parent === category )
115
- . map ( async channel => await channel . delete ( ) ) ,
116
- ) ;
117
-
118
- await category ?. delete ( ) ;
119
-
120
- await Promise . all ( guild . roles . cache
121
- . filter ( r => ( r . name === `${ courseName } ${ courseAdminRole } ` || r . name . toLowerCase ( ) === courseName . toLowerCase ( ) ) )
122
- . map ( async role => await role . delete ( ) ) ,
123
- ) ;
124
-
125
- await updateGuide ( guild , models ) ;
126
- } ) ;
127
-
128
- models . Course . addHook ( "afterCreate" , async ( course ) => {
129
- const student = await findOrCreateRoleWithName ( course . name , guild ) ;
130
- const admin = await findOrCreateRoleWithName ( `${ course . name } ${ courseAdminRole } ` , guild ) ;
131
- const categoryObject = getCategoryObject ( course . name , getCategoryChannelPermissionOverwrites ( guild , admin , student ) ) ;
132
- const category = await findOrCreateChannel ( categoryObject , guild ) ;
133
- await course . update ( { categoryId : category . id } ) ;
134
-
135
- const channelObjects = await getDefaultChannelObjects ( guild , course . name , student , admin , category ) ;
136
- const defaultChannelObjects = channelObjects . map ( channelObject => {
137
- const voiceChannel = channelObject . options . type === "GUILD_VOICE" ;
138
- return {
139
- courseId : course . id ,
140
- name : channelObject . name ,
141
- defaultChannel : true ,
142
- voiceChannel : voiceChannel ,
143
- } ;
144
- } ) ;
145
-
146
- await createDefaultChannelsToDatabase ( defaultChannelObjects , models . Channel ) ;
147
- } ) ;
148
-
149
- models . Course . addHook ( "afterUpdate" , async ( course ) => {
150
- if ( ! course . _options . isNewRecord ) {
151
- const changedValue = course . _changed ;
152
- const courseName = course . name ;
153
- const previousCourseName = course . _previousDataValues . name ;
154
- let category = findCategoryWithCourseName ( courseName , guild ) ;
155
- const hidden = course . private ;
156
- const locked = course . locked ;
157
-
158
- if ( changedValue . has ( "locked" ) ) {
159
- if ( locked ) {
160
- await lockTelegramCourse ( models . Course , courseName ) ;
161
- await setEmojisLock ( category , hidden , courseName , models ) ;
162
- category . permissionOverwrites . create ( guild . roles . cache . find ( r => r . name === course . name ) , { VIEW_CHANNEL : true , SEND_MESSAGES : false } ) ;
163
- category . permissionOverwrites . create ( guild . roles . cache . find ( r => r . name === "faculty" ) , { SEND_MESSAGES : true } ) ;
164
- category . permissionOverwrites . create ( guild . roles . cache . find ( r => r . name === "admin" ) , { SEND_MESSAGES : true } ) ;
165
- }
166
- else {
167
- await unlockTelegramCourse ( models . Course , courseName ) ;
168
- await setEmojisUnlock ( category , hidden , courseName , models ) ;
169
- category . permissionOverwrites . create ( guild . roles . cache . find ( r => r . name === course . name ) , { VIEW_CHANNEL : true , SEND_MESSAGES : true } ) ;
170
- }
171
- }
172
- else if ( changedValue . has ( "private" ) ) {
173
- hidden ?
174
- await setEmojisHide ( category , locked , courseName )
175
- : await setEmojisUnhide ( category , locked , courseName ) ;
176
- }
177
- else if ( changedValue . has ( "name" ) ) {
178
- category = findCategoryWithCourseName ( previousCourseName , guild ) ;
179
- const channelAnnouncement = guild . channels . cache . find ( c => c . name === `${ previousCourseName } _announcement` ) ;
180
- const categoryEmojis = category . name . replace ( previousCourseName , "" ) . trim ( ) ;
181
- await category . setName ( `${ categoryEmojis } ${ courseName } ` ) ;
182
- await changeCourseRoles ( previousCourseName , courseName , guild ) ;
183
- await setCoursePositionABC ( guild , `${ categoryEmojis } ${ courseName } ` , models . Course ) ;
184
- await editChannelNames ( course . id , previousCourseName , courseName , models . Channel ) ;
185
- await updateAnnouncementChannelMessage ( guild , channelAnnouncement ) ;
186
- }
187
- await updateGuide ( guild , models ) ;
188
- }
189
- } ) ;
190
- } ;
191
-
192
- const initUserHooks = ( guild , models ) => {
193
- models . User . addHook ( "afterUpdate" , async ( user ) => {
194
- const changedValue = user . _changed ;
195
- const userDiscoId = user . discordId ;
196
-
197
- if ( changedValue . has ( "admin" ) ) {
198
- const adminRole = guild . roles . cache . find ( r => r . name === "admin" ) ;
199
- const userDisco = guild . members . cache . get ( userDiscoId ) ;
200
- user . admin
201
- ? userDisco . roles . add ( adminRole )
202
- : userDisco . roles . remove ( adminRole ) ;
203
- }
204
-
205
- if ( changedValue . has ( "faculty" ) ) {
206
- const facultyRoleObject = guild . roles . cache . find ( r => r . name === facultyRole ) ;
207
- const userDisco = guild . members . cache . get ( userDiscoId ) ;
208
- user . faculty
209
- ? userDisco . roles . add ( facultyRoleObject )
210
- : userDisco . roles . remove ( facultyRoleObject ) ;
211
- }
212
- } ) ;
213
- } ;
214
-
215
- const initCourseMemberHooks = ( guild , models ) => {
216
- models . CourseMember . addHook ( "afterCreate" , async ( courseMember ) => {
217
- const user = await findUserByDbId ( courseMember . dataValues . userId , models . User ) ;
218
- const course = await findCourseFromDbById ( courseMember . dataValues . courseId , models . Course ) ;
219
- const member = guild . members . cache . get ( user . dataValues . discordId ) ;
220
- const courseRole = guild . roles . cache . find ( r => r . name === course . name ) ;
221
- await member . roles . add ( courseRole ) ;
222
- await updateGuide ( guild , models ) ;
223
- joinedUsersCounter . inc ( { course : course . name } ) ;
224
- } ) ;
225
-
226
- models . CourseMember . addHook ( "afterBulkDestroy" , async ( courseMember ) => {
227
- const user = await findUserByDbId ( courseMember . where . userId , models . User ) ;
228
- const course = await findCourseFromDbById ( courseMember . where . courseId , models . Course ) ;
229
- const member = guild . members . cache . get ( user . discordId ) ;
230
- const courseRoles = guild . roles . cache
231
- . filter ( role => ( role . name === `${ course . name } ${ courseAdminRole } ` || role . name === course . name ) )
232
- . map ( role => role . name ) ;
233
-
234
- await Promise . all ( member . roles . cache
235
- . filter ( role => courseRoles . includes ( role . name ) )
236
- . map ( async role => await member . roles . remove ( role ) ) ) ;
237
- await member . fetch ( true ) ;
238
- const announcementChannel = guild . channels . cache . find ( c => c . name === `${ course . name } _announcement` ) ;
239
- await updateAnnouncementChannelMessage ( guild , announcementChannel ) ;
240
- await updateGuide ( guild , models ) ;
241
- } ) ;
242
-
243
- models . CourseMember . addHook ( "afterUpdate" , async ( courseMember ) => {
244
- if ( courseMember . _changed . has ( "instructor" ) ) {
245
- await updateInviteLinks ( guild ) ;
246
- }
247
- } ) ;
248
- } ;
249
-
250
13
module . exports = { initHooks } ;
0 commit comments