From 3a85af4028b4c009cbf8f9661360e3159751610d Mon Sep 17 00:00:00 2001 From: Corban-Lee Jones Date: Fri, 6 Dec 2024 13:05:11 +0000 Subject: [PATCH] fetch steam profile via API --- bot.py | 2 ++ cogs/console.py | 21 +++++++++++++++------ requirements.txt | 7 +++++++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/bot.py b/bot.py index 464050c..4164984 100644 --- a/bot.py +++ b/bot.py @@ -28,11 +28,13 @@ class DiscordBot(commands.Bot): Contains controls to interact with the bot via the Discord API. """ in_game_channel_id: int + steam_api_key: str rcon_details: dict def __init__(self): super().__init__(command_prefix="-", intents=Intents.all()) self.in_game_channel_id = int(getenv("SPIFFO__DISCORD_CHANNEL_ID")) + self.steam_api_key = getenv("SPIFFO__STEAM_API_KEY") self.rcon_details = { "host": getenv("SPIFFO__RCON_HOST"), "port": getenv("SPIFFO__RCON_PORT"), diff --git a/cogs/console.py b/cogs/console.py index b7b70c5..7f6bbd0 100644 --- a/cogs/console.py +++ b/cogs/console.py @@ -7,6 +7,7 @@ import logging from pathlib import Path from dataclasses import dataclass +import httpx import aiofiles from discord import Embed, Colour from discord.ext import commands, tasks @@ -24,14 +25,22 @@ class ZomboidUser: username: str connection_type: str - @property - def steam_profile_picture_url(self): - return f"https://steamcommunity.com/profiles/{self.steam_id}/profileimage?width=256" - @property def steam_url(self): return f"https://steamcommunity.com/profiles/{self.steam_id}" + async def get_steam_profile_picture(self, steam_api_key: str): + if not steam_api_key: + log.warning("No steam API key, can't get profile picture.") + return + + async with httpx.AsyncClient() as client: + response = await client.get(url=f"https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key={steam_api_key}&steamids={self.steam_id}") + response.raise_for_status() + + data = response.json() + return data["response"]["players"][0]["avatar"] + class ConsoleCog(commands.Cog): """ @@ -120,7 +129,7 @@ class ConsoleCog(commands.Cog): description="Player has joined the server", colour=Colour.brand_green() ) - embed.set_image(url=user.steam_profile_picture_url) + embed.set_thumbnail(url=user.get_steam_profile_picture(self.bot.steam_api_key)) await channel.send(embed=embed) @@ -154,7 +163,7 @@ class ConsoleCog(commands.Cog): description="Player has left the server", colour=Colour.brand_red() ) - embed.set_image(url=user.steam_profile_picture_url) + embed.set_thumbnail(url=user.get_steam_profile_picture(self.bot.steam_api_key)) await channel.send(embed=embed) diff --git a/requirements.txt b/requirements.txt index 5391963..3e740bb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,13 +2,20 @@ aiofiles==24.1.0 aiohappyeyeballs==2.4.4 aiohttp==3.11.9 aiosignal==1.3.1 +anyio==4.7.0 attrs==24.2.0 bump2version==1.0.1 +certifi==2024.8.30 discord.py==2.4.0 frozenlist==1.5.0 +h11==0.14.0 +httpcore==1.0.7 +httpx==0.28.0 idna==3.10 multidict==6.1.0 propcache==0.2.1 python-dotenv==1.0.1 rcon==2.4.9 +sniffio==1.3.1 +typing_extensions==4.12.2 yarl==1.18.3