fetch steam profile via API
All checks were successful
Build and Push Docker Image / build (push) Successful in 22s

This commit is contained in:
Corban-Lee Jones 2024-12-06 13:05:11 +00:00
parent ed22e552a1
commit 3a85af4028
3 changed files with 24 additions and 6 deletions

2
bot.py
View File

@ -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"),

View File

@ -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)

View File

@ -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