1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-22 19:14:20 +00:00

💥 Login and many other things!

This commit is contained in:
Steffo 2021-04-19 05:26:20 +02:00
parent 80e9b92e83
commit 80cf8fc83d
Signed by: steffo
GPG key ID: 6965406171929D01
18 changed files with 169 additions and 16 deletions

8
poetry.lock generated
View file

@ -303,7 +303,7 @@ socks = ["PySocks (>=1.5.6,!=1.5.7)", "win-inet-pton"]
[[package]]
name = "royalnet"
version = "6.5.0"
version = "6.5.4"
description = "A multipurpose bot framework"
category = "main"
optional = false
@ -512,7 +512,7 @@ multidict = ">=4.0"
[metadata]
lock-version = "1.1"
python-versions = "^3.8"
content-hash = "d09a25572708f3d976eec40c086da014232918d5d1cd689866c831bc324b598d"
content-hash = "c30fdf09f35a2430d64998b966b2648d1bed68a9a1e58e45339f5a1a53895263"
[metadata.files]
aiohttp = [
@ -894,8 +894,8 @@ requests = [
{file = "requests-2.25.1.tar.gz", hash = "sha256:27973dd4a904a4f13b263a19c866c13b92a39ed1c964655f025f3f8d3d75b804"},
]
royalnet = [
{file = "royalnet-6.5.0-py3-none-any.whl", hash = "sha256:1c1f0ddb4891d1951bd8fe2e9a4139cc5b3a039d9b2318c0cabd489dc2253ee3"},
{file = "royalnet-6.5.0.tar.gz", hash = "sha256:dc4e5a284e8e2f36f259b5fe88e36a5947984c78600fd22e695e62277c787951"},
{file = "royalnet-6.5.4-py3-none-any.whl", hash = "sha256:68d30fdd00653a5e79eab2b22a8c5bb16103e08e8674ec8f4b13ff1bdf1d33d2"},
{file = "royalnet-6.5.4.tar.gz", hash = "sha256:fbab17b911c2ee200c0f7bfd5d2bcc4d1d3f9771b41ab64a848a08cdd464e3d8"},
]
royalnet-console = [
{file = "royalnet-console-2.0.4.tar.gz", hash = "sha256:049948a558a2ee7359f808cf61faef08789ce9e3969a0dfb94e65bd912ffcffc"},

View file

@ -19,7 +19,7 @@ python = "^3.8"
coloredlogs = "^15.0"
aiohttp = "^3.7.4"
psycopg2 = "^2.8.6"
royalnet = "~6.5.0"
royalnet = "~6.5.3"
royalnet_telethon = "^2.0.0"
royalnet_console = "^2.0.4"
alembic = "^1.5.8"

View file

@ -55,6 +55,7 @@ register_telegram(commands.pmots, ["pmots"])
register_telegram(commands.spell, ["spell"], "(?P<spellname>.+)")
register_telegram(commands.smecds, ["smecds"])
register_telegram(commands.man, ["man", "help"], "(?P<commandname>[A-Za-z]+)")
register_telegram(commands.login, ["login"])
pda.implementations["telethon.1"].register_conversation(r)

View file

@ -12,3 +12,4 @@ from .pmots import *
from .spell import *
from .smecds import *
from .man import *
from .login import *

View file

@ -2,7 +2,7 @@ import royalnet.engineer as engi
@engi.TeleportingConversation
async def ahnonlosoio(*, _sentry: engi.Sentry, _msg: engi.Message, **__):
async def ahnonlosoio(*, _msg: engi.Message, **__):
"""
Ah, non lo so io!
"""

View file

@ -74,7 +74,7 @@ ANSWERS = [
@engi.TeleportingConversation
async def answer(*, _sentry: engi.Sentry, _msg: engi.Message, **__):
async def answer(*, _msg: engi.Message, **__):
"""
Fai una domanda al bot, che possa essere risposta con un o un no: lui ti risponderà!
"""

View file

@ -8,7 +8,7 @@ log = logging.getLogger(__name__)
@engi.TeleportingConversation
async def cat(*, _sentry: engi.Sentry, _msg: engi.Message, **__):
async def cat(*, _msg: engi.Message, **__):
"""
Invia un gatto in chat! 🐈
"""

View file

@ -5,7 +5,7 @@ import royalnet_telethon.bullet.contents
@engi.TeleportingConversation
async def ciaoruozi(*, _sentry: engi.Sentry, _msg: engi.Message, _imp, **__):
async def ciaoruozi(*, _msg: engi.Message, _imp, **__):
"""
Saluta Ruozi, una creatura leggendaria che potrebbe esistere o non esistere in Royal Games.
"""

View file

@ -3,7 +3,7 @@ import royalnet.engineer.conversation as c
@engi.TeleportingConversation
async def color(*, _sentry: engi.Sentry, _msg: engi.Message, **__):
async def color(*, _msg: engi.Message, **__):
"""
Invia un colore in chat...?
"""

View file

@ -80,7 +80,7 @@ _emojis = {
@engi.TeleportingConversation
async def emojify(*, _msg: engi.Message, message: str, **__):
async def emojify(*, message: str, **__):
"""
Converti un messaggio in emoji.
"""

View file

@ -56,7 +56,7 @@ _fortunes = [
@engi.TeleportingConversation
async def fortune(*, _sentry: engi.Sentry, _msg: engi.Message, **__):
async def fortune(*, _msg: engi.Message, **__):
"""
Come sarà la giornata di oggi?
"""

142
royalpack/commands/login.py Normal file
View file

@ -0,0 +1,142 @@
import royalnet.engineer as engi
import sqlalchemy.sql as ss
import sqlalchemy.orm as so
import royalpack.database as db
import royalpack.config as cfg
import royalnet_telethon
import royalnet_telethon.bullet.contents
import aiohttp
import asyncio
import logging
import arrow
import datetime
log = logging.getLogger(__name__)
# FIXME: Properly handle errors in this function!
@engi.use_database(db.lazy_session_class)
@engi.TeleportingConversation
async def login(*, _msg: engi.Message, _session: so.Session, _imp, **__):
"""
Fai il login al tuo account Royalnet.
"""
log.debug("Evaluating config...")
config = cfg.lazy_config.evaluate()
log.debug("Sliding into DMs...")
sender: engi.User = await _msg.sender
current: engi.Channel = await _msg.channel
private: engi.Channel = await sender.slide()
if hash(current) != hash(private):
await _msg.reply(text="👤 Ti ho inviato un messaggio in chat privata contenente le istruzioni per il login!")
async with aiohttp.ClientSession() as http_session:
log.debug("Generating device code...")
async with http_session.post(config["auth.url.device"], data={
"client_id": config["auth.client.id"],
"scope": "profile email openid",
"prompt": "consent",
}) as request:
response = await request.json()
start = arrow.now()
log.debug("Asking user to login...")
await private.send_message(
text=f"🌍 Effettua il RYGlogin al seguente URL, poi premi Confirm:\n"
f"{response['verification_uri_complete']}\n"
f"\n"
f"(Codice: {response['user_code']})"
)
expiration = start + datetime.timedelta(seconds=response["expires_in"])
while arrow.now() < expiration:
log.debug("Sleeping for 10 seconds...")
await asyncio.sleep(10)
async with http_session.post(config["auth.url.token"], data={
"client_id": config["auth.client.id"],
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
"device_code": response["device_code"],
}) as request:
response = await request.json()
if "error" in response:
log.debug(f"Response returned error {response['error']!r}, retrying...")
continue
elif "access_token" in response:
log.debug(f"Obtained access token...")
break
else:
log.error(f"Didn't get an access token, but didn't get an error either?!")
continue
else:
log.debug("Login request expired.")
await private.send_message(text="🕒 La tua richiesta di login è scaduta. "
"Riinvia il comando per ricominciare!")
return
async with http_session.post(config["auth.url.userinfo"], headers={
"Authorization": f"{response['token_type']} {response['access_token']}"
}) as request:
response = await request.json()
log.debug("Checking if the user already exists...")
user: db.User = _session.execute(
ss.select(db.User).where(db.User.sub == response["sub"])
).scalar()
log.debug("Creating user dict...")
user_dict = {
"sub": response['sub'],
"last_update": arrow.now(),
"name": response['name'],
"nickname": response['nickname'],
"avatar": response['picture'],
"email": response['email'],
}
if user is None:
log.info(f"Creating new user: {response['sub']}")
user = db.User(**user_dict)
_session.add(user)
else:
log.debug(f"Updating existing user: {response['sub']}")
user.update(**user_dict)
if isinstance(_imp, royalnet_telethon.TelethonPDAImplementation):
log.debug("Found out I'm running on Telethon...")
sender: royalnet_telethon.bullet.contents.TelegramUser
log.debug("Checking if the TelegramAccount already exists...")
tg: db.TelegramAccount = _session.execute(
ss.select(db.TelegramAccount).where(db.TelegramAccount.id == sender._user.id)
).scalar()
log.debug("Creating tg_dict...")
tg_dict = {
"user_fk": response["sub"],
"id": sender._user.id,
"first_name": sender._user.first_name,
"last_name": sender._user.last_name,
"username": sender._user.username,
"avatar_url": None, # TODO: avatars
}
if tg is None:
log.info(f"Creating new TelegramAccount: {sender._user.id}")
tg = db.TelegramAccount(**tg_dict)
_session.add(tg)
else:
log.debug(f"Updating existing TelegramAccount: {sender._user.id}")
tg.update(**tg_dict)
log.debug(f"Committing session...")
_session.commit()
log.debug(f"Done, notifying the user...")
await private.send_message(text=f"✅ Login riuscito! Sei loggato come {response['name']}!")
__all__ = ("login",)

View file

@ -2,7 +2,7 @@ import royalnet.engineer as engi
@engi.TeleportingConversation
async def ping(*, _sentry: engi.Sentry, _msg: engi.Message, **__):
async def ping(*, _msg: engi.Message, **__):
"""
Gioca a ping pong con il bot. 🏓
"""

View file

@ -2,7 +2,7 @@ import royalnet.engineer as engi
@engi.TeleportingConversation
async def pmots(*, _sentry: engi.Sentry, _msg: engi.Message, **__):
async def pmots(*, _msg: engi.Message, **__):
"""
Riprenditi da uno stomp colossale!

View file

@ -6,7 +6,7 @@ log = logging.getLogger(__name__)
@engi.TeleportingConversation
async def ship(*, _sentry: engi.Sentry, _msg: engi.Message, first: str, second: str, **__):
async def ship(*, _msg: engi.Message, first: str, second: str, **__):
"""
Shippa insieme due persone! 💞
"""

View file

@ -55,7 +55,7 @@ _ds_list = ["della secca", "del seccatore", "del secchiello", "del secchio", "de
@engi.TeleportingConversation
async def smecds(*, _sentry: engi.Sentry, _msg: engi.Message, **__):
async def smecds(*, _msg: engi.Message, **__):
"""
Secondo me, è colpa dello stagista...
"""

View file

@ -0,0 +1,2 @@
from .base import *
from .engine import *

View file

@ -9,6 +9,13 @@ lazy_engine = royalnet.lazy.Lazy(lambda c: sqlalchemy.create_engine(c["database.
The uninitialized sqlalchemy engine.
"""
lazy_session_class = royalnet.lazy.Lazy(lambda e: sqlalchemy.orm.sessionmaker(bind=e), e=lazy_engine)
"""
The uninitialized sqlalchemy session class.
"""
__all__ = (
"lazy_engine",
"lazy_session_class",
)