Refactor ModrinthTracker commands to improve error handling and response validation. Replace direct response status checks with data validation, enhance logging for exceptions, and provide clearer user feedback for search and stats commands.
Some checks are pending
Run pre-commit / Run pre-commit (push) Waiting to run

This commit is contained in:
Valerie 2025-05-23 06:29:18 -04:00
parent 07f32c7b05
commit 2dada0dd80

View file

@ -260,12 +260,11 @@ class ModrinthTracker(commands.Cog):
"limit": 5,
"index": "relevance"
}
response = await self._make_request(f"{BASE_URL}/search", params=params)
if response.status != 200:
data = await self._make_request(f"{BASE_URL}/search", params=params)
if not data:
await ctx.send("Failed to search Modrinth projects.")
return
data = await response.json()
if not data["hits"]:
await ctx.send("No projects found matching your query.")
return
@ -292,25 +291,24 @@ class ModrinthTracker(commands.Cog):
await ctx.send(embed=embed)
except Exception as e:
await ctx.send(f"An error occurred while searching: {str(e)}")
self.bot.logger.error(f"Error in modrinth search command: {str(e)}", exc_info=True)
await ctx.send("An unexpected error occurred while searching. Please try again later.")
@modrinth.command()
async def stats(self, ctx, project_id: str):
"""Show detailed statistics for a tracked project."""
try:
response = await self._make_request(f"{BASE_URL}/project/{project_id}")
if response.status != 200:
# Get project info
project_data = await self._make_request(f"{BASE_URL}/project/{project_id}")
if not project_data:
await ctx.send(f"Error: Project `{project_id}` not found on Modrinth.")
return
project_data = await response.json()
# Get version history
response = await self._make_request(f"{BASE_URL}/project/{project_id}/version")
if response.status != 200:
versions = await self._make_request(f"{BASE_URL}/project/{project_id}/version")
if not versions:
await ctx.send("Error: Could not fetch version information.")
return
versions = await response.json()
embed = discord.Embed(
title=f"📊 {project_data['title']} Statistics",
@ -355,6 +353,7 @@ class ModrinthTracker(commands.Cog):
await ctx.send(embed=embed)
except Exception as e:
self.bot.logger.error(f"Error in modrinth stats command: {str(e)}", exc_info=True)
await ctx.send(f"An error occurred while fetching statistics: {str(e)}")
@modrinth.command()
@ -369,20 +368,17 @@ class ModrinthTracker(commands.Cog):
try:
# Get project info
response = await self._make_request(f"{BASE_URL}/project/{project_id}")
if response.status != 200:
project_data = await self._make_request(f"{BASE_URL}/project/{project_id}")
if not project_data:
await ctx.send(f"Error: Project `{project_id}` not found on Modrinth.")
return
project_data = await response.json()
# Get version history
response = await self._make_request(f"{BASE_URL}/project/{project_id}/version")
if response.status != 200:
versions = await self._make_request(f"{BASE_URL}/project/{project_id}/version")
if not versions:
await ctx.send("Error: Could not fetch version information.")
return
versions = await response.json()
if not versions:
await ctx.send("No version information available for this project.")
return
@ -431,7 +427,8 @@ class ModrinthTracker(commands.Cog):
await ctx.send(embed=embed)
except Exception as e:
await ctx.send(f"An error occurred while fetching version history: {str(e)}")
self.bot.logger.error(f"Error in modrinth versions command: {str(e)}", exc_info=True)
await ctx.send("An unexpected error occurred while fetching version history. Please try again later.")
async def update_checker(self):
await self.bot.wait_until_ready()