1313from pathlib import Path
1414from typing import Optional
1515
16+ import nextcord
1617from git import Repo
1718from git .exc import GitCommandError
1819
@@ -240,14 +241,25 @@ async def flush_and_commit(self, channel) -> None:
240241 return
241242
242243 repo_path = Path (self ._get_repo_path (server_id ))
243- channel_name = channel .name
244244
245- # Sanitize channel name for filesystem
246- safe_channel_name = "" .join (
247- c if c .isalnum () or c in '-_' else '_' for c in channel_name
248- )
245+ # Sanitize name for filesystem (preserve emojis, block problematic chars)
246+ def sanitize_name (name : str ) -> str :
247+ unsafe_chars = '/\\ :*?"<>|'
248+ return "" .join (
249+ c if c not in unsafe_chars and c .isprintable () else '_' for c in name
250+ )
251+
252+ # Check if this is a thread and adjust directory structure
253+ is_thread = isinstance (channel , nextcord .Thread )
254+ if is_thread :
255+ parent_name = channel .parent .name
256+ safe_parent_name = sanitize_name (parent_name )
257+ safe_thread_name = sanitize_name (channel .name )
258+ channel_dir = repo_path / safe_parent_name / '_threads' / safe_thread_name
259+ else :
260+ safe_channel_name = sanitize_name (channel .name )
261+ channel_dir = repo_path / safe_channel_name
249262
250- channel_dir = repo_path / safe_channel_name
251263 attachments_dir = channel_dir / 'attachments'
252264
253265 # Group messages by date
@@ -274,16 +286,22 @@ async def flush_and_commit(self, channel) -> None:
274286 if new_count > 0 :
275287 date_range .append (date_str )
276288
289+ # Build display name for logging
290+ if is_thread :
291+ display_name = f"{ channel .parent .name } /{ channel .name } (thread)"
292+ else :
293+ display_name = channel .name
294+
277295 if total_new == 0 :
278- print (f'\t [Git Archive] [{ server_id } ] No new messages to commit for #{ channel_name } ' )
296+ print (f'\t [Git Archive] [{ server_id } ] No new messages to commit for #{ display_name } ' )
279297 return
280298
281299 # Stage all changes
282300 repo .git .add (A = True )
283301
284302 # Check if there are staged changes
285303 if not repo .is_dirty (index = True ):
286- print (f'\t [Git Archive] [{ server_id } ] No changes to commit for #{ channel_name } ' )
304+ print (f'\t [Git Archive] [{ server_id } ] No changes to commit for #{ display_name } ' )
287305 return
288306
289307 # Create commit message
@@ -293,15 +311,15 @@ async def flush_and_commit(self, channel) -> None:
293311 else :
294312 date_info = f"{ date_range [0 ]} to { date_range [- 1 ]} "
295313
296- commit_message = f"Archive { total_new } messages from #{ channel_name } ({ date_info } )"
314+ commit_message = f"Archive { total_new } messages from #{ display_name } ({ date_info } )"
297315 print (f'\t [Git Archive] [{ server_id } ] { commit_message } ' )
298316
299317 repo .index .commit (commit_message )
300318
301319 # Push changes
302320 try :
303321 repo .remotes .origin .push ()
304- print (f'\t [Git Archive] [{ server_id } ] Pushed changes for #{ channel_name } ' )
322+ print (f'\t [Git Archive] [{ server_id } ] Pushed changes for #{ display_name } ' )
305323 except GitCommandError as e :
306324 print (f'\t [Git Archive] [{ server_id } ] Warning: Could not push: { e } ' )
307325
0 commit comments