Skip to content

Commit f3e5c2d

Browse files
fix: dynamic paths
1 parent 32a6720 commit f3e5c2d

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed

src/gitcord/cogs/channels.py

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from dataclasses import dataclass
77
from typing import Optional, Union
8+
import os
89

910
import yaml
1011
import discord
@@ -20,6 +21,8 @@
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
2326
from ..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)

src/gitcord/constants/paths.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@
44

55
import os
66

7-
# Default YAML file paths
8-
CATEGORY_YAML_PATH = "/home/user/Projects/gitcord-template/community/category.yaml"
9-
OFFTOPIC_YAML_PATH = "/home/user/Projects/gitcord-template/community/off-topic.yaml"
10-
TEMPLATE_DIR = "/home/user/Projects/gitcord-template/community/"
11-
12-
# Add more paths as needed
7+
# GitCord data directory for storing per-guild template repositories and metadata
138
GITCORD_DATA_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), ".gitcord_data")
149
os.makedirs(GITCORD_DATA_DIR, exist_ok=True)
1510

1611
def get_template_repo_dir(guild_id):
12+
"""Get the template repository directory for a specific guild."""
1713
return os.path.join(GITCORD_DATA_DIR, "template_repo", str(guild_id))
1814

1915
def get_metadata_file(guild_id):
16+
"""Get the metadata file path for a specific guild."""
2017
return os.path.join(GITCORD_DATA_DIR, f"template_source_{guild_id}.json")

0 commit comments

Comments
 (0)