mirror of
https://github.com/pacnpal/Pac-cogs.git
synced 2025-12-20 02:41:06 -05:00
Added proper exception handling for all try blocks that were missing except/finally clauses
Fixed the unclosed parenthesis in the format_message() call Completed the message formatting section with proper parameters Reorganized the code structure for better maintainability Ensured proper error handling and cleanup throughout
This commit is contained in:
@@ -134,61 +134,53 @@ class VideoProcessor:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return False, f"Download error: {str(e)}"
|
return False, f"Download error: {str(e)}"
|
||||||
|
|
||||||
|
# Get archive channel
|
||||||
|
guild = self.bot.get_guild(guild_id)
|
||||||
|
if not guild:
|
||||||
|
return False, f"Guild {guild_id} not found"
|
||||||
|
|
||||||
|
archive_channel = await self.config.get_channel(guild, "archive")
|
||||||
|
if not archive_channel:
|
||||||
|
return False, "Archive channel not configured"
|
||||||
|
|
||||||
|
# Format message
|
||||||
try:
|
try:
|
||||||
# Get archive channel
|
author = original_message.author if original_message else None
|
||||||
guild = self.bot.get_guild(guild_id)
|
message = await message_manager.format_message(
|
||||||
if not guild:
|
author=author,
|
||||||
return False, f"Guild {guild_id} not found"
|
channel=channel,
|
||||||
|
url=item.url
|
||||||
archive_channel = await self.config.get_channel(guild, "archive")
|
)
|
||||||
if not archive_channel:
|
except Exception as e:
|
||||||
return False, "Archive channel not configured"
|
return False, f"Failed to format message: {str(e)}"
|
||||||
|
|
||||||
# Format message
|
|
||||||
try:
|
|
||||||
author = original_message.author if original_message else None
|
|
||||||
message = await message_manager.format_message(
|
|
||||||
author=author,
|
|
||||||
channel=channel,
|
|
||||||
# Upload to archive channel
|
|
||||||
try:
|
|
||||||
if not os.path.exists(file_path):
|
|
||||||
return False, "Processed file not found"
|
|
||||||
|
|
||||||
await archive_channel.send(
|
|
||||||
content=message,
|
|
||||||
file=discord.File(file_path)
|
|
||||||
)
|
|
||||||
|
|
||||||
except discord.HTTPException as e:
|
|
||||||
return False, f"Failed to upload to Discord: {str(e)}"
|
|
||||||
except Exception as e:
|
|
||||||
return False, f"Failed to archive video: {str(e)}"
|
|
||||||
|
|
||||||
|
# Upload to archive channel
|
||||||
|
try:
|
||||||
|
if not os.path.exists(file_path):
|
||||||
|
return False, "Processed file not found"
|
||||||
|
|
||||||
|
await archive_channel.send(
|
||||||
|
content=message,
|
||||||
|
file=discord.File(file_path)
|
||||||
|
)
|
||||||
return True, None
|
return True, None
|
||||||
|
|
||||||
|
except discord.HTTPException as e:
|
||||||
|
return False, f"Failed to upload to Discord: {str(e)}"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return False, f"Processing error: {str(e)}"
|
return False, f"Failed to archive video: {str(e)}"
|
||||||
|
|
||||||
finally:
|
|
||||||
# Clean up downloaded file
|
|
||||||
if file_path and os.path.exists(file_path):
|
|
||||||
try:
|
|
||||||
os.unlink(file_path)
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Failed to clean up file {file_path}: {e}")
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error processing video: {traceback.format_exc()}")
|
logger.error(f"Error processing video: {traceback.format_exc()}")
|
||||||
return False, str(e)
|
return False, str(e)
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
# Ensure file cleanup even on unexpected errors
|
# Clean up downloaded file
|
||||||
if file_path and os.path.exists(file_path):
|
if file_path and os.path.exists(file_path):
|
||||||
try:
|
try:
|
||||||
os.unlink(file_path)
|
os.unlink(file_path)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Final cleanup failed for {file_path}: {e}")
|
logger.error(f"Failed to clean up file {file_path}: {e}")
|
||||||
|
|
||||||
async def cleanup(self):
|
async def cleanup(self):
|
||||||
"""Clean up resources"""
|
"""Clean up resources"""
|
||||||
|
|||||||
Reference in New Issue
Block a user