1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-30 15:04:18 +00:00
royalnet/royalpack/commands/osu.py

136 lines
6.6 KiB
Python
Raw Normal View History

2020-07-21 23:39:04 +00:00
from typing import *
import itsdangerous
2020-07-22 23:14:25 +00:00
import aiohttp
2020-07-21 23:39:04 +00:00
from royalnet.backpack import tables as rbt
import royalnet.commands as rc
2020-07-22 23:14:25 +00:00
import royalnet.utils as ru
2020-07-21 23:39:04 +00:00
from .abstract.linker import LinkerCommand
from ..types import Updatable
2020-07-22 23:14:25 +00:00
from ..tables import Osu
2020-07-21 23:39:04 +00:00
from ..stars.api_auth_login_osu import ApiAuthLoginOsuStar
class OsuCommand(LinkerCommand):
name = "osu"
description = "Connetti e sincronizza il tuo account di osu!"
@property
def client_id(self):
return self.config[self.name]['client_id']
@property
def client_secret(self):
return self.config[self.name]['client_secret']
@property
def base_url(self):
return self.config['base_url']
@property
def secret_key(self):
return self.config['secret_key']
2020-07-22 23:14:25 +00:00
async def get_updatables_of_user(self, session, user: rbt.User) -> List[Osu]:
return user.osu
2020-07-21 23:39:04 +00:00
2020-07-22 23:14:25 +00:00
async def get_updatables(self, session) -> List[Osu]:
return await ru.asyncify(session.query(self.alchemy.get(Osu)).all)
2020-07-21 23:39:04 +00:00
async def create(self,
session,
user: rbt.User,
args: rc.CommandArgs,
2020-07-22 23:14:25 +00:00
data: Optional[rc.CommandData] = None) -> Optional[Osu]:
2020-07-21 23:39:04 +00:00
serializer = itsdangerous.URLSafeSerializer(self.secret_key, salt="osu")
2020-07-22 23:14:25 +00:00
# TODO: Ensure the chat the link is being sent in is secure!!!
2020-07-21 23:39:04 +00:00
await data.reply("🔑 [b]Login necessario[/b]\n"
f"[url=https://osu.ppy.sh/oauth/authorize"
f"?client_id={self.client_id}"
f"&redirect_uri={self.base_url}{ApiAuthLoginOsuStar.path}"
f"&response_type=code"
f"&state={serializer.dumps(user.uid)}]"
2020-07-22 23:20:21 +00:00
f"Connetti account di osu! a {user.username}"
2020-07-21 23:39:04 +00:00
f"[/url]")
return None
2020-07-22 23:14:25 +00:00
async def update(self, session, obj: Osu, change: Callable[[str, Any], Awaitable[None]]):
await obj.refresh_if_expired(client_id=self.client_id,
client_secret=self.client_secret,
base_url=self.base_url,
path=ApiAuthLoginOsuStar.path)
async with aiohttp.ClientSession(headers={"Authorization": f"Bearer {obj.access_token}"}) as session:
async with session.get("https://osu.ppy.sh/api/v2/me/osu") as response:
m = await response.json()
obj.avatar_url = m["avatar_url"]
obj.username = m["username"]
if "statistics" in m:
await change("standard_pp", m["statistics"].get("pp"))
async with session.get("https://osu.ppy.sh/api/v2/me/taiko") as response:
m = await response.json()
if "statistics" in m:
await change("taiko_pp", m["statistics"].get("pp"))
async with session.get("https://osu.ppy.sh/api/v2/me/fruits") as response:
m = await response.json()
if "statistics" in m:
await change("catch_pp", m["statistics"].get("pp"))
async with session.get("https://osu.ppy.sh/api/v2/me/mania") as response:
m = await response.json()
if "statistics" in m:
await change("mania_pp", m["statistics"].get("pp"))
async def on_increase(self, session, obj: Osu, attribute: str, old: Any, new: Any) -> None:
if attribute == "standard_pp":
await self.notify(f"📈 [b]{obj.user}[/b] è salito a [b]{new:.0f}pp[/b] su [i]osu![/i]! Congratulazioni!")
elif attribute == "taiko_pp":
await self.notify(f"📈 [b]{obj.user}[/b] è salito a [b]{new:.0f}pp[/b] su [i]osu!taiko[/i]! Congratulazioni!")
elif attribute == "catch_pp":
await self.notify(f"📈 [b]{obj.user}[/b] è salito a [b]{new:.0f}pp[/b] su [i]osu!catch[/i]! Congratulazioni!")
elif attribute == "mania_pp":
await self.notify(f"📈 [b]{obj.user}[/b] è salito a [b]{new:.0f}pp[/b] su [i]osu!mania[/i]! Congratulazioni!")
async def on_unchanged(self, session, obj: Osu, attribute: str, old: Any, new: Any) -> None:
2020-07-21 23:39:04 +00:00
pass
2020-07-22 23:14:25 +00:00
async def on_decrease(self, session, obj: Osu, attribute: str, old: Any, new: Any) -> None:
if attribute == "standard_pp":
await self.notify(f"📉 [b]{obj.user}[/b] è sceso a [b]{new:.0f}pp[/b] su [i]osu![/i].")
elif attribute == "taiko_pp":
await self.notify(f"📉 [b]{obj.user}[/b] è sceso a [b]{new:.0f}pp[/b] su [i]osu!taiko[/i].")
elif attribute == "catch_pp":
await self.notify(f"📉 [b]{obj.user}[/b] è sceso a [b]{new:.0f}pp[/b] su [i]osu!catch[/i].")
elif attribute == "mania_pp":
await self.notify(f"📉 [b]{obj.user}[/b] è sceso a [b]{new:.0f}pp[/b] su [i]osu!mania[/i].")
async def on_first(self, session, obj: Osu, attribute: str, old: None, new: Any) -> None:
if attribute == "standard_pp":
await self.notify(f"⭐️ [b]{obj.user}[/b] ha guadagnato i suoi primi [b]{new:.0f}pp[/b] su [i]osu![/i]!")
elif attribute == "taiko_pp":
await self.notify(f"⭐️ [b]{obj.user}[/b] ha guadagnato i suoi primi [b]{new:.0f}pp[/b] su [i]osu!taiko[/i]!")
elif attribute == "catch_pp":
await self.notify(f"⭐️ [b]{obj.user}[/b] ha guadagnato i suoi primi [b]{new:.0f}pp[/b] su [i]osu!catch[/i]!")
elif attribute == "mania_pp":
await self.notify(f"⭐️ [b]{obj.user}[/b] ha guadagnato i suoi primi [b]{new:.0f}pp[/b] su [i]osu!mania[/i]!")
async def on_reset(self, session, obj: Osu, attribute: str, old: Any, new: None) -> None:
if attribute == "standard_pp":
await self.notify(f"⬜️ [b]{obj.user}[/b] non è più classificato su [i]osu![/i].")
elif attribute == "taiko_pp":
await self.notify(f" ⬜️[b]{obj.user}[/b] non è più classificato su [i]osu!taiko[/i].")
elif attribute == "catch_pp":
await self.notify(f"⬜️ [b]{obj.user}[/b] non è più classificato su [i]osu!catch[/i].")
elif attribute == "mania_pp":
await self.notify(f"⬜️ [b]{obj.user}[/b] non è più classificato su [i]osu!mania[/i].")
2020-07-23 00:02:20 +00:00
def describe(self, obj: Osu) -> str:
message = [
f" [url=https://osu.ppy.sh/users/{obj.osu_id}]{obj.username}[/url]",
f"osu!: [b]{obj.standard_pp:.0f}pp[/b]",
f"osu!taiko: [b]{obj.taiko_pp:.0f}pp[/b]",
f"osu!catch: [b]{obj.catch_pp:.0f}pp[/b]",
f"osu!mania: [b]{obj.mania_pp:.0f}pp[/b]",
]
return "\n".join(message)