@@ -8,27 +8,38 @@ import { BotComponent } from "../../../bot-component.js";
88import { Wheatley } from "../../../wheatley.js" ;
99import { unwrap } from "../../../utils/misc.js" ;
1010
11+ const categories_map = {
12+ staff_logs : "1135927261472755712" ,
13+ staff : "873125551064363028" ,
14+ meta : "360691699288113163" ,
15+ tutoring : "923430684041818153" ,
16+ cpp_help : "897465499535949874" ,
17+ c_help : "931970218442493992" ,
18+ discussion : "855220194887335977" ,
19+ specialized : "360691955031867392" ,
20+ community : "1131921460034801747" ,
21+ off_topic : "360691500985745409" ,
22+ misc : "506274316623544320" ,
23+ bot_dev : "1166516815472640050" ,
24+ voice : "360692425242705921" ,
25+ archive : "910306041969913938" ,
26+ private_archive : "455278783352537099" ,
27+ challenges_archive : "429594248099135488" ,
28+ meta_archive : "910308747929321492" ,
29+ } ;
30+
1131type permissions_entry = {
1232 allow ?: bigint [ ] ;
1333 deny ?: bigint [ ] ;
1434} ;
1535
1636type permission_overwrites = Record < string , permissions_entry > ;
1737
18- type category_permission_entry = {
19- category : Discord . CategoryChannel ;
20- permissions : permission_overwrites ;
21- } ;
22-
23- type channel_permission_entry = {
24- permissions : permission_overwrites ;
25- } ;
26-
2738const SET_VOICE_STATUS_PERMISSION_BIT = 1n << 48n ; // TODO: Replace once discord.js supports this in PermissionsBitField
2839
2940export default class PermissionManager extends BotComponent {
30- category_permissions : Record < string , category_permission_entry > = { } ;
31- channel_overrides : Record < string , channel_permission_entry > = { } ;
41+ category_permissions : Record < string , permission_overwrites > = { } ;
42+ channel_overrides : Record < string , permission_overwrites > = { } ;
3243
3344 setup_permissions_map ( ) {
3445 // permission sets
@@ -149,21 +160,21 @@ export default class PermissionManager extends BotComponent {
149160 } ,
150161 } ;
151162
152- this . add_entry ( this . wheatley . categories . staff_logs , mod_only_channel ) ;
153- this . add_entry ( this . wheatley . categories . meta , default_permissions ) ;
154- this . add_entry ( this . wheatley . categories . cpp_help , default_permissions ) ;
155- this . add_entry ( this . wheatley . categories . c_help , default_permissions ) ;
156- this . add_entry ( this . wheatley . categories . discussion , default_permissions ) ;
157- this . add_entry ( this . wheatley . categories . specialized , default_permissions ) ;
158- this . add_entry ( this . wheatley . categories . community , default_permissions ) ;
159- this . add_entry ( this . wheatley . categories . off_topic , off_topic_permissions ) ;
160- this . add_entry ( this . wheatley . categories . misc , default_permissions ) ;
161- this . add_entry ( this . wheatley . categories . bot_dev , default_permissions ) ;
162- this . add_entry ( this . wheatley . categories . voice , voice_permissions ) ;
163- this . add_entry ( this . wheatley . categories . archive , read_only_archive_channel ) ;
164- this . add_entry ( this . wheatley . categories . private_archive , mod_only_channel ) ;
165- this . add_entry ( this . wheatley . categories . challenges_archive , mod_only_channel ) ;
166- this . add_entry ( this . wheatley . categories . meta_archive , mod_only_channel ) ;
163+ this . add_category_permissions ( categories_map . staff_logs , mod_only_channel ) ;
164+ this . add_category_permissions ( categories_map . meta , default_permissions ) ;
165+ this . add_category_permissions ( categories_map . cpp_help , default_permissions ) ;
166+ this . add_category_permissions ( categories_map . c_help , default_permissions ) ;
167+ this . add_category_permissions ( categories_map . discussion , default_permissions ) ;
168+ this . add_category_permissions ( categories_map . specialized , default_permissions ) ;
169+ this . add_category_permissions ( categories_map . community , default_permissions ) ;
170+ this . add_category_permissions ( categories_map . off_topic , off_topic_permissions ) ;
171+ this . add_category_permissions ( categories_map . misc , default_permissions ) ;
172+ this . add_category_permissions ( categories_map . bot_dev , default_permissions ) ;
173+ this . add_category_permissions ( categories_map . voice , voice_permissions ) ;
174+ this . add_category_permissions ( categories_map . archive , read_only_archive_channel ) ;
175+ this . add_category_permissions ( categories_map . private_archive , mod_only_channel ) ;
176+ this . add_category_permissions ( categories_map . challenges_archive , mod_only_channel ) ;
177+ this . add_category_permissions ( categories_map . meta_archive , mod_only_channel ) ;
167178
168179 // meta overrides
169180 this . add_channel_overwrite ( this . wheatley . channels . rules , {
@@ -340,20 +351,15 @@ export default class PermissionManager extends BotComponent {
340351 } ) ;
341352 }
342353
343- add_entry ( category : Discord . CategoryChannel , permissions : permission_overwrites ) {
344- this . category_permissions [ category . id ] = {
345- category,
346- permissions,
347- } ;
354+ add_category_permissions ( category_id : string , permissions : permission_overwrites ) {
355+ this . category_permissions [ category_id ] = permissions ;
348356 }
349357
350358 add_channel_overwrite ( channel_id : string , permissions : permission_overwrites ) {
351- this . channel_overrides [ channel_id ] = {
352- permissions,
353- } ;
359+ this . channel_overrides [ channel_id ] = permissions ;
354360 }
355361
356- async set_channel_permissions ( channel : Discord . CategoryChildChannel , { category } : category_permission_entry ) {
362+ async sync_channel_permissions ( channel : Discord . CategoryChildChannel , category : Discord . CategoryChannel ) {
357363 if ( channel . id in this . channel_overrides ) {
358364 M . log (
359365 `Setting permissions for channel ${ channel . id } ${ channel . name } ` +
@@ -371,28 +377,37 @@ export default class PermissionManager extends BotComponent {
371377 }
372378 }
373379
374- async set_category_permissions ( { category, permissions } : category_permission_entry ) {
380+ async sync_category_permissions ( category : Discord . CategoryChannel , permissions : permission_overwrites ) {
375381 M . log ( `Setting permissions for category ${ category . id } ${ category . name } ` ) ;
376382 await category . permissionOverwrites . set (
377383 Object . entries ( permissions ) . map ( ( [ id , permissions ] ) => ( { id, ...permissions } ) ) ,
378384 ) ;
379385 const channels = category . children . cache . map ( channel => channel ) ;
380386 for ( const channel of channels ) {
381- await this . set_channel_permissions ( channel , { category, permissions } ) ;
387+ await this . sync_channel_permissions ( channel , category ) ;
382388 }
383389 }
384390
385- async set_permissions ( ) {
386- for ( const entry of Object . values ( this . category_permissions ) ) {
387- await this . set_category_permissions ( entry ) ;
388- }
391+ async sync_permissions ( ) {
392+ await Promise . all (
393+ Object . entries ( this . category_permissions ) . map ( async ( [ id , permissions ] ) => {
394+ const category = await this . wheatley . client . channels . fetch ( id ) ;
395+ if ( ! category ) {
396+ throw Error ( `Category ${ id } not found` ) ;
397+ }
398+ if ( ! ( category instanceof Discord . CategoryChannel ) ) {
399+ throw Error ( `Category ${ id } not of the expected type` ) ;
400+ }
401+ await this . sync_category_permissions ( category , permissions ) ;
402+ } ) ,
403+ ) ;
389404 }
390405
391406 override async on_ready ( ) {
392407 this . setup_permissions_map ( ) ;
393- this . set_permissions ( ) . catch ( this . wheatley . critical_error . bind ( this . wheatley ) ) ;
408+ this . sync_permissions ( ) . catch ( this . wheatley . critical_error . bind ( this . wheatley ) ) ;
394409 setTimeout ( ( ) => {
395- this . set_permissions ( ) . catch ( this . wheatley . critical_error . bind ( this . wheatley ) ) ;
410+ this . sync_permissions ( ) . catch ( this . wheatley . critical_error . bind ( this . wheatley ) ) ;
396411 } , HOUR ) ;
397412 }
398413}
0 commit comments