55
66from dataclasses import dataclass
77from typing import Optional , Union
8+ import os
89
910import yaml
1011import discord
2021 create_channel_by_type ,
2122 check_channel_exists ,
2223)
24+ from ..utils import template_metadata
25+ from ..constants .paths import get_template_repo_dir
2326from ..views import DeleteExtraChannelsView
2427
2528
@@ -43,6 +46,24 @@ def __init__(self, bot: commands.Bot):
4346 super ().__init__ (bot )
4447 self .logger .info ("Channels cog loaded" )
4548
49+ def _get_template_path (self , guild_id : int , file_name : str = None , folder : str = None ) -> Optional [str ]:
50+ """Get the template path for a guild, falling back to None if no template repo exists."""
51+ meta = template_metadata .load_metadata (guild_id )
52+ if not meta or not os .path .exists (meta .get ("local_path" , "" )):
53+ return None
54+
55+ base_path = meta ["local_path" ]
56+
57+ if folder :
58+ base_path = os .path .join (base_path , folder )
59+ if not os .path .isdir (base_path ):
60+ return None
61+
62+ if file_name :
63+ return os .path .join (base_path , file_name )
64+
65+ return base_path
66+
4667 def _ensure_guild (
4768 self , ctx_or_interaction : Union [commands .Context , discord .Interaction ]
4869 ) -> Optional [discord .Guild ]:
@@ -202,9 +223,11 @@ async def _process_channel_in_category(
202223 ]:
203224 """Process a single channel in a category."""
204225 try :
205- channel_yaml_path = (
206- f"/home/user/Projects/gitcord-template/community/{ channel_name } .yaml"
207- )
226+ channel_yaml_path = self ._get_template_path (guild .id , f"{ channel_name } .yaml" )
227+ if not channel_yaml_path :
228+ self .logger .error ("No template repo found for guild %s. Use !git clone first." , guild .id )
229+ return None , None , ""
230+
208231 channel_config = parse_channel_config (channel_yaml_path )
209232
210233 existing_channel = check_channel_exists (
@@ -289,7 +312,11 @@ async def _create_new_category(
289312
290313 for channel_name in category_config ["channels" ]:
291314 try :
292- channel_yaml_path = f"/home/user/Projects/gitcord-template/community/{ channel_name } .yaml"
315+ channel_yaml_path = self ._get_template_path (guild .id , f"{ channel_name } .yaml" )
316+ if not channel_yaml_path :
317+ self .logger .error ("No template repo found for guild %s. Use !git clone first." , guild .id )
318+ continue
319+
293320 channel_config = parse_channel_config (channel_yaml_path )
294321
295322 existing_channel = check_channel_exists (
@@ -419,7 +446,11 @@ def _create_category_result_embed(self, result: CategoryResult) -> discord.Embed
419446 @commands .has_permissions (manage_channels = True )
420447 async def createchannel (self , ctx : commands .Context ) -> None :
421448 """Create a channel based on properties defined in a YAML file."""
422- yaml_path = "/home/user/Projects/gitcord-template/community/off-topic.yaml"
449+ yaml_path = self ._get_template_path (ctx .guild .id , "off-topic.yaml" )
450+ if not yaml_path :
451+ await self .send_error (ctx , "❌ No Template Repository" ,
452+ "No template repository found for this server. Use `!git clone <url>` first to set up a template repository." )
453+ return
423454 await self ._create_single_channel (ctx , yaml_path )
424455
425456 @commands .command (name = "createcategory" )
@@ -431,7 +462,11 @@ async def createcategory(self, ctx: commands.Context) -> None:
431462 await self .send_error (ctx , "❌ Error" , "Guild not found" )
432463 return
433464
434- yaml_path = "/home/user/Projects/gitcord-template/community/category.yaml"
465+ yaml_path = self ._get_template_path (guild .id , "category.yaml" )
466+ if not yaml_path :
467+ await self .send_error (ctx , "❌ No Template Repository" ,
468+ "No template repository found for this server. Use `!git clone <url>` first to set up a template repository." )
469+ return
435470
436471 try :
437472 result = await self ._create_category_common (guild , yaml_path )
@@ -504,7 +539,15 @@ async def createcategory_slash(
504539
505540 # Use default path if none provided
506541 if yaml_path is None :
507- yaml_path = "/home/user/Projects/gitcord-template/community/category.yaml"
542+ yaml_path = self ._get_template_path (guild .id , "category.yaml" )
543+ if not yaml_path :
544+ embed = create_embed (
545+ title = "❌ No Template Repository" ,
546+ description = "No template repository found for this server. Use `!git clone <url>` first to set up a template repository." ,
547+ color = discord .Color .red (),
548+ )
549+ await interaction .followup .send (embed = embed )
550+ return
508551
509552 try :
510553 result = await self ._create_category_common (guild , yaml_path )
0 commit comments