From 0caab039c9804ae88d7b55a1d83fbba146d71036 Mon Sep 17 00:00:00 2001 From: Kozejin Date: Fri, 8 Mar 2024 15:37:30 -0500 Subject: [PATCH] Error Handling --- README.md | 1 + cogs/steam.py | 27 ++++++++++++++++----------- util/steam_protocol.py | 8 ++++++++ 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 395f88c..5d1b094 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ - **Scheduled Restarts:** Automate server reboots at set intervals for updates and maintenance, enhancing stability while minimizing disruption to players. This requires implementing a watchdog mechanism to detect when the server stops and automatically restarts it, similar to capabilities found in game panels or batch scripts. - **Status Tracking:** Enable the status tracker to monitor the collective number of players on all your servers. - **Palguard Integration**: Offers RCON integration for executing PalGuard commands, such as item and pal spawning. Features a customizable game database with a search function to easily spawn desired items or pals. + - **Steam API Integration**: Seamlessly integrate with the Steam API to fetch player profiles and enhance server management with detailed user insights. ## Guides: Please check our [wiki](https://github.com/dkoz/palworld-bot/wiki) for all guides related to the bot. \ No newline at end of file diff --git a/cogs/steam.py b/cogs/steam.py index c71dfd2..11876a6 100644 --- a/cogs/steam.py +++ b/cogs/steam.py @@ -12,19 +12,24 @@ def __init__(self, bot): async def steam(self, interaction: Interaction, profile_url: str = SlashOption(description="The full URL to the Steam profile")): await interaction.response.defer() - steamid64 = steam_protocol.extract_steamid64(profile_url) - if not steamid64: - vanity_url = steam_protocol.extract_vanity_url(profile_url) - if vanity_url: - steamid64 = await steam_protocol.resolve_vanity_url(vanity_url) - if not steamid64: - await interaction.followup.send("Could not resolve Steam profile URL.") + try: + steamid64 = steam_protocol.extract_steamid64(profile_url) + if not steamid64: + vanity_url = steam_protocol.extract_vanity_url(profile_url) + if vanity_url: + steamid64 = await steam_protocol.resolve_vanity_url(vanity_url) + if not steamid64: + await interaction.followup.send("Could not resolve Steam profile URL.") + return + else: + await interaction.followup.send("Invalid Steam profile URL.") return - else: - await interaction.followup.send("Invalid Steam profile URL.") - return - summary_data, bans_data = await steam_protocol.fetch_steam_profile(steamid64) + summary_data, bans_data = await steam_protocol.fetch_steam_profile(steamid64) + except steam_protocol.InvalidSteamAPIKeyException: + await interaction.followup.send("Error: Invalid Steam API Key. Please configure a valid API key.") + return + await self.display_steam_profile(interaction, summary_data, bans_data) async def display_steam_profile(self, interaction, summary_data, bans_data): diff --git a/util/steam_protocol.py b/util/steam_protocol.py index 434a9b9..fe755e2 100644 --- a/util/steam_protocol.py +++ b/util/steam_protocol.py @@ -3,10 +3,16 @@ import asyncio from config import steam_api_key +# exceptions +class InvalidSteamAPIKeyException(Exception): + pass + async def resolve_vanity_url(vanity_url): url = f"https://api.steampowered.com/ISteamUser/ResolveVanityURL/v1/?key={steam_api_key}&vanityurl={vanity_url}" async with aiohttp.ClientSession() as session: async with session.get(url) as response: + if response.status == 403: + raise InvalidSteamAPIKeyException("Invalid Steam API Key.") data = await response.json() if data['response']['success'] == 1: return data['response']['steamid'] @@ -29,6 +35,8 @@ async def fetch_steam_profile(steamid64): session.get(summary_url), session.get(bans_url) ) + if summary_response.status == 403 or bans_response.status == 403: + raise InvalidSteamAPIKeyException("Invalid Steam API Key.") summary_data = await summary_response.json() bans_data = await bans_response.json() return summary_data, bans_data