Skip to content

Commit

Permalink
Error Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoz committed Mar 8, 2024
1 parent b824c16 commit 0caab03
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
27 changes: 16 additions & 11 deletions cogs/steam.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 8 additions & 0 deletions util/steam_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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

0 comments on commit 0caab03

Please sign in to comment.