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
Some checks are pending
Run pre-commit / Run pre-commit (push) Waiting to run
This commit is contained in:
parent
07f32c7b05
commit
2dada0dd80
1 changed files with 16 additions and 19 deletions
|
@ -260,12 +260,11 @@ class ModrinthTracker(commands.Cog):
|
||||||
"limit": 5,
|
"limit": 5,
|
||||||
"index": "relevance"
|
"index": "relevance"
|
||||||
}
|
}
|
||||||
response = await self._make_request(f"{BASE_URL}/search", params=params)
|
data = await self._make_request(f"{BASE_URL}/search", params=params)
|
||||||
if response.status != 200:
|
if not data:
|
||||||
await ctx.send("Failed to search Modrinth projects.")
|
await ctx.send("Failed to search Modrinth projects.")
|
||||||
return
|
return
|
||||||
|
|
||||||
data = await response.json()
|
|
||||||
if not data["hits"]:
|
if not data["hits"]:
|
||||||
await ctx.send("No projects found matching your query.")
|
await ctx.send("No projects found matching your query.")
|
||||||
return
|
return
|
||||||
|
@ -292,25 +291,24 @@ class ModrinthTracker(commands.Cog):
|
||||||
await ctx.send(embed=embed)
|
await ctx.send(embed=embed)
|
||||||
|
|
||||||
except Exception as e:
|
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()
|
@modrinth.command()
|
||||||
async def stats(self, ctx, project_id: str):
|
async def stats(self, ctx, project_id: str):
|
||||||
"""Show detailed statistics for a tracked project."""
|
"""Show detailed statistics for a tracked project."""
|
||||||
try:
|
try:
|
||||||
response = await self._make_request(f"{BASE_URL}/project/{project_id}")
|
# Get project info
|
||||||
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.")
|
await ctx.send(f"Error: Project `{project_id}` not found on Modrinth.")
|
||||||
return
|
return
|
||||||
|
|
||||||
project_data = await response.json()
|
|
||||||
|
|
||||||
# Get version history
|
# Get version history
|
||||||
response = await self._make_request(f"{BASE_URL}/project/{project_id}/version")
|
versions = await self._make_request(f"{BASE_URL}/project/{project_id}/version")
|
||||||
if response.status != 200:
|
if not versions:
|
||||||
await ctx.send("Error: Could not fetch version information.")
|
await ctx.send("Error: Could not fetch version information.")
|
||||||
return
|
return
|
||||||
versions = await response.json()
|
|
||||||
|
|
||||||
embed = discord.Embed(
|
embed = discord.Embed(
|
||||||
title=f"📊 {project_data['title']} Statistics",
|
title=f"📊 {project_data['title']} Statistics",
|
||||||
|
@ -355,6 +353,7 @@ class ModrinthTracker(commands.Cog):
|
||||||
await ctx.send(embed=embed)
|
await ctx.send(embed=embed)
|
||||||
|
|
||||||
except Exception as e:
|
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)}")
|
await ctx.send(f"An error occurred while fetching statistics: {str(e)}")
|
||||||
|
|
||||||
@modrinth.command()
|
@modrinth.command()
|
||||||
|
@ -369,20 +368,17 @@ class ModrinthTracker(commands.Cog):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Get project info
|
# Get project info
|
||||||
response = await self._make_request(f"{BASE_URL}/project/{project_id}")
|
project_data = await self._make_request(f"{BASE_URL}/project/{project_id}")
|
||||||
if response.status != 200:
|
if not project_data:
|
||||||
await ctx.send(f"Error: Project `{project_id}` not found on Modrinth.")
|
await ctx.send(f"Error: Project `{project_id}` not found on Modrinth.")
|
||||||
return
|
return
|
||||||
|
|
||||||
project_data = await response.json()
|
|
||||||
|
|
||||||
# Get version history
|
# Get version history
|
||||||
response = await self._make_request(f"{BASE_URL}/project/{project_id}/version")
|
versions = await self._make_request(f"{BASE_URL}/project/{project_id}/version")
|
||||||
if response.status != 200:
|
if not versions:
|
||||||
await ctx.send("Error: Could not fetch version information.")
|
await ctx.send("Error: Could not fetch version information.")
|
||||||
return
|
return
|
||||||
|
|
||||||
versions = await response.json()
|
|
||||||
if not versions:
|
if not versions:
|
||||||
await ctx.send("No version information available for this project.")
|
await ctx.send("No version information available for this project.")
|
||||||
return
|
return
|
||||||
|
@ -431,7 +427,8 @@ class ModrinthTracker(commands.Cog):
|
||||||
await ctx.send(embed=embed)
|
await ctx.send(embed=embed)
|
||||||
|
|
||||||
except Exception as e:
|
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):
|
async def update_checker(self):
|
||||||
await self.bot.wait_until_ready()
|
await self.bot.wait_until_ready()
|
||||||
|
|
Loading…
Add table
Reference in a new issue