2929Requires bot privileges for receiving 'GUILD_MEMBER' events.
3030
3131It will:
32- - Create categories, text channels, forums
33- - Update positions of categories, text channels, forums
34- - Update topics of text channels, forums
35- - Add tags to forums
32+ - Create enable 'Community Server' features
33+ - Create missing categories, text channels, forums
34+ - Update positions
35+ - Update topics
36+ - Add missing forum tags
37+ - Update 'mandatory/optional' state of forum tags
3638
3739It will not:
3840- Delete categories
@@ -347,6 +349,25 @@ async def configure_text_channel(
347349 )
348350
349351
352+ async def configure_tags (
353+ channel : discord .ForumChannel , expected_tags : list [str ], * , required : bool
354+ ) -> None :
355+ logger .info ("Configure tags %s for channel %s" , channel .name , channel .name )
356+ existing_tags = {tag .name : tag for tag in channel .available_tags }
357+ tags_to_create = set (expected_tags ) - set (existing_tags )
358+
359+ if tags_to_create :
360+ for tag_name in tags_to_create :
361+ logger .debug ("Create tag %s" , tag_name )
362+ existing_tags [tag_name ] = await channel .create_tag (name = tag_name )
363+
364+ logger .debug ("Update available tags for channel %s" , channel .name )
365+ await channel .edit (available_tags = (list (existing_tags .values ())))
366+
367+ if required and not channel .flags .require_tag :
368+ await channel .edit (require_tag = required )
369+
370+
350371async def configure_forum_channel (
351372 guild : discord .Guild ,
352373 category : discord .CategoryChannel ,
@@ -356,43 +377,24 @@ async def configure_forum_channel(
356377 logger .info ("Configure forum channel %s at position %d" , template .name , position )
357378 for channel in guild .forums :
358379 if channel .name == template .name :
359- if channel .category is None or channel .category . id != category . id :
380+ if channel .category is None or channel .category != category :
360381 logger .debug ("Update category" )
361382 await channel .edit (category = category )
362-
363383 if channel .position != position :
364384 logger .debug ("Update position" )
365385 await channel .edit (position = position )
366-
367386 if channel .topic != template .topic :
368387 logger .debug ("Update topic" )
369388 await channel .edit (topic = template .topic )
370389
371- known_tags_by_name = {tag .name : tag for tag in channel .available_tags }
372- if set (known_tags_by_name .keys ()) != set (template .tags ):
373- tags_to_set : list [discord .ForumTag ] = []
374- for tag_name in template .tags :
375- if tag_name in known_tags_by_name :
376- logger .debug ("Found tag %s" , tag_name )
377- tags_to_set .append (known_tags_by_name [tag_name ])
378- else :
379- logger .debug ("Create tag %s" , tag_name )
380- tags_to_set .append (await channel .create_tag (name = tag_name ))
381- logger .debug ("Update tags" )
382- await channel .edit (available_tags = tags_to_set )
390+ await configure_tags (channel , template .tags , required = template .require_tags )
383391 return channel
384392
385393 logger .debug ("Create forum channel %s" , template .name )
386394 channel = await guild .create_forum (
387395 template .name , category = category , topic = template .topic , position = position
388396 )
389- if template .tags is not None :
390- new_tags : list [discord .ForumTag ] = []
391- for tag_name in template .tags :
392- logger .debug ("Create tag %s" , tag_name )
393- new_tags .append (await channel .create_tag (name = tag_name ))
394- logger .debug ("Update tags" )
395- await channel .edit (available_tags = new_tags )
397+ await configure_tags (channel , template .tags , required = template .require_tags )
396398
397399 return channel
398400
@@ -416,19 +418,23 @@ async def configure_guild(guild: discord.Guild, configuration: GuildConfig) -> N
416418 )
417419 channels_by_name [channel_template .name ] = text_channel
418420
419- logger .debug ("Enabling guild features" )
421+ logger .debug ("Raising verification level to medium" )
422+ if guild .verification_level < discord .VerificationLevel .medium :
423+ await guild .edit (verification_level = discord .VerificationLevel .medium )
424+
425+ logger .debug ("Enabling guild feature" )
420426 await guild .edit (
421427 community = True ,
422428 rules_channel = channels_by_name [configuration .rules_channel_name ],
423429 public_updates_channel = channels_by_name [configuration .discord_updates_channel_name ],
424- verification_level = discord .VerificationLevel .medium ,
425430 explicit_content_filter = discord .ContentFilter .all_members ,
426431 )
427432
428433 # create channels
429434 logger .info ("Creating channels" )
430435 channel_position = 0
431- for category_position , category in enumerate (configuration .categories ):
436+ category_position = 0
437+ for category in configuration .categories :
432438 d_category = await configure_category (guild , category .name , category_position )
433439 for channel_template in category .channels :
434440 if isinstance (channel_template , TextChannel ):
@@ -438,6 +444,7 @@ async def configure_guild(guild: discord.Guild, configuration: GuildConfig) -> N
438444 else :
439445 assert_never (channel_template )
440446 channel_position += 1
447+ category_position += 1
441448
442449
443450class GuildConfigurationBot (Bot ):
0 commit comments