1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 21:44:21 +00:00
royalnet/royalpack/commands/steammatch.py

105 lines
3.9 KiB
Python
Raw Normal View History

2020-01-20 23:54:55 +00:00
from typing import *
2020-05-28 14:54:43 +00:00
import steam.webapi
2020-01-20 23:54:55 +00:00
import requests.exceptions
2020-05-10 22:46:12 +00:00
import royalnet.commands as rc
import royalnet.utils as ru
import royalnet.backpack.tables as rbt
from ..tables import Steam
2020-01-20 23:54:55 +00:00
class SteamGame:
def __init__(self,
appid=None,
name=None,
playtime_forever=None,
img_icon_url=None,
img_logo_url=None,
has_community_visible_stats=None,
playtime_windows_forever=None,
playtime_mac_forever=None,
playtime_linux_forever=None,
playtime_2weeks=None):
self.appid = appid
self.name = name
self.playtime_forever = playtime_forever
self.img_icon_url = img_icon_url
self.img_logo_url = img_logo_url
self.has_community_visible_stats = has_community_visible_stats
self.playtime_windows_forever = playtime_windows_forever
self.playtime_mac_forever = playtime_mac_forever
self.playtime_linux_forever = playtime_linux_forever
self.playtime_2weeks = playtime_2weeks
def __hash__(self):
return self.appid
def __eq__(self, other):
if isinstance(other, SteamGame):
return self.appid == other.appid
return False
def __str__(self):
return self.name
def __repr__(self):
return f"<{self.__class__.__qualname__} {self.appid} ({self.name})>"
2020-05-10 22:46:12 +00:00
class SteammatchCommand(rc.Command):
2020-01-20 23:54:55 +00:00
name: str = "steammatch"
description: str = "Vedi quali giochi hai in comune con uno o più membri!"
syntax: str = "{royalnet_username}+"
2020-05-10 22:46:12 +00:00
def __init__(self, interface: rc.CommandInterface):
2020-01-20 23:54:55 +00:00
super().__init__(interface)
if "Steam" not in self.config or "web_api_key" not in self.config["Steam"]:
2020-05-10 22:46:12 +00:00
raise rc.ConfigurationError("[c]Steam.web_api_key[/c] config option is missing!")
2020-05-28 14:54:43 +00:00
self._api = steam.webapi.WebAPI(self.config["Steam"]["web_api_key"])
2020-01-20 23:54:55 +00:00
2020-05-10 22:46:12 +00:00
async def run(self, args: rc.CommandArgs, data: rc.CommandData) -> None:
2020-01-20 23:54:55 +00:00
users = []
author = await data.get_author(error_if_none=True)
users.append(author)
for arg in args:
2020-05-10 22:46:12 +00:00
user = await rbt.User.find(self.alchemy, data.session, arg)
2020-01-20 23:54:55 +00:00
users.append(user)
if len(users) < 2:
2020-05-10 22:46:12 +00:00
raise rc.InvalidInputError("Devi specificare almeno un altro utente!")
2020-01-20 23:54:55 +00:00
shared_games: Optional[set] = None
for user in users:
user_games = set()
if len(user.steam) == 0:
2020-05-10 22:46:12 +00:00
raise rc.UserError(f"{user} non ha un account Steam registrato!")
2020-01-20 23:54:55 +00:00
for steam_account in user.steam:
steam_account: Steam
try:
2020-05-10 22:46:12 +00:00
response = await ru.asyncify(self._api.IPlayerService.GetOwnedGames,
steamid=steam_account._steamid,
include_appinfo=True,
include_played_free_games=True,
2020-06-05 23:05:39 +00:00
include_free_sub=True,
2020-05-10 22:46:12 +00:00
appids_filter=0)
2020-01-20 23:54:55 +00:00
except requests.exceptions.HTTPError:
2020-05-10 22:46:12 +00:00
raise rc.ExternalError(f"L'account Steam di {user} è privato!")
2020-01-20 23:54:55 +00:00
games = response["response"]["games"]
for game in games:
user_games.add(SteamGame(**game))
if shared_games is None:
shared_games = user_games
else:
shared_games = shared_games.intersection(user_games)
2020-05-10 22:46:12 +00:00
message_rows = [f"🎮 Giochi in comune tra {ru.andformat([str(user) for user in users], final=' e ')}:"]
2020-01-20 23:54:55 +00:00
for game in sorted(list(shared_games), key=lambda g: g.name):
message_rows.append(f"- {game}")
message = "\n".join(message_rows)
await data.reply(message)