@@ -252,6 +252,8 @@ async def git_command(self, ctx: commands.Context, *args):
252252 await ctx .send (embed = warning_embed )
253253
254254 try :
255+ # Always apply template to ensure Discord matches the template
256+ # (Even if git says "up to date", Discord might not match the template)
255257 result_msgs = await self ._apply_template_from_dir (ctx .guild , meta ["local_path" ], ctx = ctx )
256258
257259 # Convert to git-style diff
@@ -534,8 +536,11 @@ async def _apply_template_from_dir(
534536 category = existing_category
535537 msg = f"ℹ️ Category '{ category_name } ' already exists. Will update channels."
536538
537- # Update category position based on order
538- if category .position != category_index :
539+ # Check if category is in correct relative order (smart positioning)
540+ guild_categories = sorted (guild .categories , key = lambda cat : cat .position )
541+ current_relative_index = guild_categories .index (category )
542+
543+ if current_relative_index != category_index :
539544 try :
540545 await category .edit (position = category_index )
541546 msg += f" Moved to position { category_index } ."
@@ -592,9 +597,15 @@ async def _apply_template_from_dir(
592597 ):
593598 update_kwargs ["nsfw" ] = channel_config .get ("nsfw" , False )
594599
595- # Update position based on channel order in YAML
596- if hasattr (existing_channel , "position" ) and existing_channel .position != channel_index :
597- update_kwargs ["position" ] = channel_index
600+ # Check if channel is in correct relative order (smart positioning)
601+ if hasattr (existing_channel , "position" ):
602+ # Get all channels in category sorted by position
603+ category_channels = sorted (category .channels , key = lambda ch : ch .position )
604+ current_relative_index = category_channels .index (existing_channel )
605+
606+ # Only move if the channel is not at the correct relative position
607+ if current_relative_index != channel_index :
608+ update_kwargs ["position" ] = channel_index
598609
599610 if update_kwargs :
600611 await existing_channel .edit (** update_kwargs )
@@ -732,12 +743,15 @@ async def _apply_monolithic_template(self, guild, template_path, ctx=None, inter
732743 category = existing_category
733744 msg = f"ℹ️ Category '{ category_name } ' already exists. Will update channels."
734745
735- # Update category position based on YAML order
736- desired_position = category_index
737- if category .position != desired_position :
746+ # Check if category is in correct relative order
747+ desired_yaml_index = category_index
748+ guild_categories = sorted (guild .categories , key = lambda cat : cat .position )
749+ current_relative_index = guild_categories .index (category )
750+
751+ if current_relative_index != desired_yaml_index :
738752 try :
739- await category .edit (position = desired_position )
740- msg += f" Moved to position { desired_position } ."
753+ await category .edit (position = desired_yaml_index )
754+ msg += f" Moved to position { desired_yaml_index } ."
741755 except (discord .Forbidden , discord .HTTPException ) as e :
742756 self .logger .warning (f"Failed to update category position: { e } " )
743757 else :
@@ -779,15 +793,21 @@ async def _apply_monolithic_template(self, guild, template_path, ctx=None, inter
779793 ):
780794 update_kwargs ["nsfw" ] = channel_config .get ("nsfw" , False )
781795
782- # Update position based on YAML order
783- desired_position = channel_index
784- if hasattr (existing_channel , "position" ) and existing_channel .position != desired_position :
785- update_kwargs ["position" ] = desired_position
796+ # Check if channel is in correct relative order (smart positioning)
797+ desired_yaml_index = channel_index
798+ if hasattr (existing_channel , "position" ):
799+ # Get all channels in category sorted by position
800+ category_channels = sorted (category .channels , key = lambda ch : ch .position )
801+ current_relative_index = category_channels .index (existing_channel )
802+
803+ # Only move if the channel is not at the correct relative position
804+ if current_relative_index != desired_yaml_index :
805+ update_kwargs ["position" ] = desired_yaml_index
786806
787807 if update_kwargs :
788808 await existing_channel .edit (** update_kwargs )
789809 updated += 1
790- position_msg = f" (position { desired_position } )" if "position" in update_kwargs else ""
810+ position_msg = f" (position { desired_yaml_index } )" if "position" in update_kwargs else ""
791811 msg = f"🔄 Updated channel: { existing_channel .name } in { category_name } { position_msg } "
792812 else :
793813 skipped += 1
0 commit comments