2020-01-20 21:47:45 +00:00
|
|
|
|
from typing import *
|
2020-05-28 14:54:43 +00:00
|
|
|
|
import steam.steamid
|
|
|
|
|
import steam.webapi
|
2020-01-20 21:47:45 +00:00
|
|
|
|
import datetime
|
2020-05-10 22:46:12 +00:00
|
|
|
|
import royalnet.commands as rc
|
|
|
|
|
import royalnet.utils as ru
|
|
|
|
|
|
|
|
|
|
from ..tables import Steam, FiorygiTransaction
|
2020-01-20 21:47:45 +00:00
|
|
|
|
|
|
|
|
|
|
2020-05-10 22:46:12 +00:00
|
|
|
|
class SteampoweredCommand(rc.Command):
|
2020-01-20 21:47:45 +00:00
|
|
|
|
name: str = "steampowered"
|
|
|
|
|
|
|
|
|
|
description: str = "Connetti il tuo account di Steam!"
|
|
|
|
|
|
2020-01-20 23:54:55 +00:00
|
|
|
|
syntax: str = "{profile_url}"
|
2020-01-20 21:47:45 +00:00
|
|
|
|
|
2020-05-10 22:46:12 +00:00
|
|
|
|
def __init__(self, interface: rc.CommandInterface):
|
2020-01-20 21:47:45 +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 21:47:45 +00:00
|
|
|
|
|
2020-04-02 14:56:52 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def _display(account: Steam):
|
2020-07-09 02:13:54 +00:00
|
|
|
|
string = f"ℹ️ [url={account.profile_url}]{account.persona_name}[/url]\n" \
|
|
|
|
|
f"[b]Level {account.account_level}[/b]\n" \
|
|
|
|
|
f"\n" \
|
|
|
|
|
f"Owned games: [b]{account.owned_games_count}[/b]\n" \
|
|
|
|
|
f"Most played 2 weeks: [url=https://store.steampowered.com/app/{account.most_played_game_2weeks}]{account.most_played_game_2weeks}[/url]\n" \
|
|
|
|
|
f"Most played forever: [url=https://store.steampowered.com/app/{account.most_played_game_forever}]{account.most_played_game_forever}[/url]\n" \
|
2020-01-20 21:47:45 +00:00
|
|
|
|
f"\n" \
|
2020-01-20 23:54:55 +00:00
|
|
|
|
f"SteamID: [c]{account.steamid.as_32}[/c]\n" \
|
|
|
|
|
f"SteamID2: [c]{account.steamid.as_steam2}[/c]\n" \
|
|
|
|
|
f"SteamID3: [c]{account.steamid.as_steam3}[/c]\n" \
|
|
|
|
|
f"SteamID64: [c]{account.steamid.as_64}[/c]\n" \
|
|
|
|
|
f"\n" \
|
|
|
|
|
f"Created on: [b]{account.account_creation_date}[/b]\n"
|
2020-01-20 21:47:45 +00:00
|
|
|
|
return string
|
|
|
|
|
|
2020-04-29 21:46:50 +00:00
|
|
|
|
async def _call(self, method, *args, **kwargs):
|
2020-04-02 14:56:52 +00:00
|
|
|
|
try:
|
2020-06-11 12:21:11 +00:00
|
|
|
|
return await ru.asyncify(method, *args, **kwargs)
|
2020-04-29 21:46:50 +00:00
|
|
|
|
except Exception as e:
|
2020-05-10 22:46:12 +00:00
|
|
|
|
raise rc.ExternalError("\n".join(e.args).replace(self.config["Steam"]["web_api_key"], "HIDDEN"))
|
2020-04-02 14:56:52 +00:00
|
|
|
|
|
2020-01-20 21:47:45 +00:00
|
|
|
|
async def _update(self, account: Steam):
|
2020-04-02 14:56:52 +00:00
|
|
|
|
# noinspection PyProtectedMember
|
|
|
|
|
response = await self._call(self._api.ISteamUser.GetPlayerSummaries_v2, steamids=account._steamid)
|
2020-01-20 21:47:45 +00:00
|
|
|
|
r = response["response"]["players"][0]
|
|
|
|
|
account.persona_name = r["personaname"]
|
|
|
|
|
account.profile_url = r["profileurl"]
|
|
|
|
|
account.avatar = r["avatar"]
|
|
|
|
|
account.primary_clan_id = r["primaryclanid"]
|
|
|
|
|
account.account_creation_date = datetime.datetime.fromtimestamp(r["timecreated"])
|
|
|
|
|
|
2020-07-09 02:13:54 +00:00
|
|
|
|
# noinspection PyProtectedMember
|
|
|
|
|
response = await self._call(self._api.IPlayerService.GetSteamLevel_v1, steamid=account._steamid)
|
|
|
|
|
account.account_level = response["response"]["player_level"]
|
|
|
|
|
|
|
|
|
|
# noinspection PyProtectedMember
|
|
|
|
|
response = await self._call(self._api.IPlayerService.GetOwnedGames_v1,
|
|
|
|
|
steamid=account._steamid,
|
|
|
|
|
include_appinfo=False,
|
|
|
|
|
include_played_free_games=True,
|
|
|
|
|
include_free_sub=False,
|
|
|
|
|
appids_filter=None)
|
|
|
|
|
account.owned_games_count = response["response"]["game_count"]
|
|
|
|
|
if response["response"]["game_count"] >= 0:
|
|
|
|
|
account.most_played_game_2weeks = sorted(response["response"]["games"], key=lambda g: -g.get("playtime_2weeks", 0))[0]["appid"]
|
|
|
|
|
account.most_played_game_forever = sorted(response["response"]["games"], key=lambda g: -g.get("playtime_forever", 0))[0]["appid"]
|
|
|
|
|
|
2020-05-10 22:46:12 +00:00
|
|
|
|
async def run(self, args: rc.CommandArgs, data: rc.CommandData) -> None:
|
2020-01-20 21:47:45 +00:00
|
|
|
|
author = await data.get_author()
|
|
|
|
|
if len(args) > 0:
|
2020-01-20 23:54:55 +00:00
|
|
|
|
url = args.joined()
|
2020-04-02 14:56:52 +00:00
|
|
|
|
steamid64 = await self._call(steam.steamid.steam64_from_url, url)
|
2020-04-29 21:46:50 +00:00
|
|
|
|
if steamid64 is None:
|
2020-05-10 22:46:12 +00:00
|
|
|
|
raise rc.InvalidInputError("Quel link non è associato ad alcun account Steam.")
|
2020-04-02 14:56:52 +00:00
|
|
|
|
response = await self._call(self._api.ISteamUser.GetPlayerSummaries_v2, steamids=steamid64)
|
2020-01-20 21:47:45 +00:00
|
|
|
|
r = response["response"]["players"][0]
|
|
|
|
|
steam_account = self.alchemy.get(Steam)(
|
|
|
|
|
user=author,
|
2020-01-20 23:54:55 +00:00
|
|
|
|
_steamid=int(steamid64),
|
2020-01-20 21:47:45 +00:00
|
|
|
|
persona_name=r["personaname"],
|
|
|
|
|
profile_url=r["profileurl"],
|
|
|
|
|
avatar=r["avatarfull"],
|
|
|
|
|
primary_clan_id=r["primaryclanid"],
|
|
|
|
|
account_creation_date=datetime.datetime.fromtimestamp(r["timecreated"])
|
|
|
|
|
)
|
|
|
|
|
data.session.add(steam_account)
|
|
|
|
|
await data.session_commit()
|
|
|
|
|
await data.reply(f"↔️ Account {steam_account} connesso a {author}!")
|
2020-04-29 16:57:32 +00:00
|
|
|
|
await FiorygiTransaction.spawn_fiorygi(data, author, 1,
|
|
|
|
|
"aver connesso il proprio account di Steam a Royalnet")
|
2020-01-20 21:47:45 +00:00
|
|
|
|
else:
|
|
|
|
|
# Update and display the Steam info for the current account
|
|
|
|
|
if len(author.steam) == 0:
|
2020-05-10 22:46:12 +00:00
|
|
|
|
raise rc.UserError("Nessun account di Steam trovato.")
|
2020-01-20 21:47:45 +00:00
|
|
|
|
message = ""
|
|
|
|
|
for account in author.steam:
|
|
|
|
|
await self._update(account)
|
|
|
|
|
message += self._display(account)
|
|
|
|
|
message += "\n"
|
|
|
|
|
await data.session_commit()
|
|
|
|
|
await data.reply(message)
|