4343ENV_FILE = Path (".env.server" )
4444
4545COMMUNITY_FEATURE = "COMMUNITY"
46- PYTHON_HELP_CHANNEL_NAME = "python_help"
47- PYTHON_HELP_CATEGORY_NAME = "python_help_system"
4846ANNOUNCEMENTS_CHANNEL_NAME = "announcements"
4947RULES_CHANNEL_NAME = "rules"
5048GUILD_CATEGORY_TYPE = 4
51- GUILD_FORUM_TYPE = 15
5249EMOJI_REGEX = re .compile (r"<:(\w+):(\d+)>" )
5350
5451if GuildConstants .id == type (GuildConstants ).model_fields ["id" ].default :
@@ -166,7 +163,7 @@ def upgrade_server_to_community_if_necessary(
166163 self ,
167164 rules_channel_id_ : int | str ,
168165 announcements_channel_id_ : int | str ,
169- ) -> None :
166+ ) -> bool :
170167 """Fetches server info & upgrades to COMMUNITY if necessary."""
171168 payload = self .guild_info
172169
@@ -177,30 +174,8 @@ def upgrade_server_to_community_if_necessary(
177174 payload ["public_updates_channel_id" ] = announcements_channel_id_
178175 self ._guild_info = self .patch (f"/guilds/{ self .guild_id } " , json = payload ).json ()
179176 log .info ("Server %s has been successfully updated to a community." , self .guild_id )
180-
181- def create_forum_channel (self , channel_name_ : str , category_id_ : int | str | None = None ) -> str :
182- """Creates a new forum channel."""
183- payload : dict [str , Any ] = {"name" : channel_name_ , "type" : GUILD_FORUM_TYPE }
184- if category_id_ :
185- payload ["parent_id" ] = category_id_
186-
187- response = self .post (
188- f"/guilds/{ self .guild_id } /channels" ,
189- json = payload ,
190- headers = {"X-Audit-Log-Reason" : "Creating forum channel as part of PyDis botstrap" },
191- )
192- forum_channel_id = response .json ()["id" ]
193- log .info ("New forum channel: %s has been successfully created." , channel_name_ )
194- return forum_channel_id
195-
196- def is_forum_channel (self , channel_id : str ) -> bool :
197- """A boolean that indicates if a channel is of type GUILD_FORUM."""
198- return self .get_channel (channel_id )["type" ] == GUILD_FORUM_TYPE
199-
200- def delete_channel (self , channel_id : str | int ) -> None :
201- """Delete a channel."""
202- log .info ("Channel python-help: %s is not a forum channel and will be replaced with one." , channel_id )
203- self .delete (f"/channels/{ channel_id } " )
177+ return True
178+ return False
204179
205180 def get_all_roles (self ) -> dict [str , int ]:
206181 """Fetches all the roles in a guild."""
@@ -332,6 +307,13 @@ def check_guild_membership(self) -> None:
332307 )
333308 raise BotstrapError ("Bot is not a member of the configured guild." )
334309
310+ def upgrade_guild (self , announcements_channel_id : str , rules_channel_id : str ) -> bool :
311+ """Upgrade the guild to a community if necessary."""
312+ return self .client .upgrade_server_to_community_if_necessary (
313+ rules_channel_id_ = rules_channel_id ,
314+ announcements_channel_id_ = announcements_channel_id ,
315+ )
316+
335317 def get_roles (self ) -> dict [str , Any ]:
336318 """Get a config map of all of the roles in the guild."""
337319 all_roles = self .client .get_all_roles ()
@@ -350,23 +332,7 @@ def get_roles(self) -> dict[str, Any]:
350332
351333 def get_channels (self ) -> dict [str , Any ]:
352334 """Get a config map of all of the channels in the guild."""
353- all_channels , all_categories = self .client .get_all_channels_and_categories ()
354-
355- rules_channel_id = all_channels [RULES_CHANNEL_NAME ]
356- announcements_channel_id = all_channels [ANNOUNCEMENTS_CHANNEL_NAME ]
357-
358- self .client .upgrade_server_to_community_if_necessary (rules_channel_id , announcements_channel_id )
359-
360- if python_help_channel_id := all_channels .get (PYTHON_HELP_CHANNEL_NAME ):
361- if not self .client .is_forum_channel (python_help_channel_id ):
362- self .client .delete_channel (python_help_channel_id )
363- python_help_channel_id = None
364-
365- if not python_help_channel_id :
366- python_help_channel_name = PYTHON_HELP_CHANNEL_NAME .replace ("_" , "-" )
367- python_help_category_id = all_categories [PYTHON_HELP_CATEGORY_NAME ]
368- python_help_channel_id = self .client .create_forum_channel (python_help_channel_name , python_help_category_id )
369- all_channels [PYTHON_HELP_CHANNEL_NAME ] = python_help_channel_id
335+ all_channels , _categories = self .client .get_all_channels_and_categories ()
370336
371337 data : dict [str , str ] = {}
372338 for channel_name in _Channels .model_fields :
@@ -408,10 +374,10 @@ def sync_webhooks(self) -> dict[str, Any]:
408374 formatted_webhook_name = webhook_name .replace ("_" , " " ).title ()
409375 for existing_hook in existing_webhooks :
410376 if (
411- # check the existing ID matches the configured one
377+ # Check the existing ID matches the configured one
412378 existing_hook ["id" ] == str (webhook_model .id )
413379 or (
414- # check if the name and the channel ID match the configured ones
380+ # Check if the name and the channel ID match the configured ones
415381 existing_hook ["name" ] == formatted_webhook_name
416382 and existing_hook ["channel_id" ] == str (all_channels [webhook_name ])
417383 )
@@ -449,25 +415,33 @@ def sync_emojis(self) -> dict[str, Any]:
449415
450416 return data
451417
452- def write_config_env (self , config : dict [str , dict [str , Any ]], env_file : Path ) -> None :
418+ def write_config_env (self , config : dict [str , dict [str , Any ]]) -> None :
453419 """Write the configuration to the specified env_file."""
454- # in order to support commented sections, we write the following
455420 with self .env_file .open ("wb" ) as file :
456- # format the dictionary into .env style
457421 for category , category_values in config .items ():
422+ # In order to support commented sections, we write the following
458423 file .write (f"# { category .capitalize ()} \n " .encode ())
424+ # Format the dictionary into .env style
459425 for key , value in category_values .items ():
460426 file .write (f"{ category } _{ key } ={ value } \n " .encode ())
461427 file .write (b"\n " )
462428
463429 def run (self ) -> None :
464430 """Runs the botstrap process."""
465- config : dict [str , dict [str , Any ]] = {}
431+ config : dict [str , dict [str , object ]] = {}
466432 self .upgrade_client ()
467433 self .check_guild_membership ()
434+
435+ channels = self .get_channels ()
436+
437+ # Ensure the guild is upgraded to a community if necessary.
438+ # This isn't strictly necessary for bot functionality, but
439+ # it prevents weird transients since PyDis is a community server.
440+ self .upgrade_guild (channels [ANNOUNCEMENTS_CHANNEL_NAME ], channels [RULES_CHANNEL_NAME ])
441+
468442 config = {
469443 "categories" : self .get_categories (),
470- "channels" : self . get_channels () ,
444+ "channels" : channels ,
471445 "roles" : self .get_roles (),
472446 "webhooks" : self .sync_webhooks (),
473447 "emojis" : self .sync_emojis (),
0 commit comments