1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 03:24:20 +00:00

Add Discord login support

This commit is contained in:
Steffo 2021-04-30 13:01:17 +02:00
parent 521b3d9251
commit c8d187d100
Signed by: steffo
GPG key ID: 6965406171929D01
2 changed files with 66 additions and 1 deletions

View file

@ -83,11 +83,37 @@ register_telegram(tg_router, commands.fiorygi_transactions_other, ["transactions
register_telegram(tg_router, commands.fiorygi_dig, ["dig"], r"(?P<slug>[a-z0-9-]+)")
register_telegram(tg_router, commands.fiorygi_bury, ["bury"], r"(?P<slug>[a-z0-9-]+)\s+(?P<value>[0-9]+)(?:\s+(?P<message>.+))?")
register_telegram(tg_router, commands.version, ["version"])
register_telegram(tg_router, commands.cv, ["cv"])
register_telegram(tg_router, commands.cv, ["cv", "civù"])
ds_router = engi.Router()
register_discord(ds_router, commands.ahnonlosoio, ["ahnonlosoio"])
register_discord(ds_router, commands.answer, ["answer"], r".+")
register_discord(ds_router, commands.cat, ["cat", "catto", "gatto", "nyaa", "nya"])
register_discord(ds_router, commands.ciaoruozi, ["ciaoruozi"])
register_discord(ds_router, commands.color, ["color"])
register_discord(ds_router, commands.ping, ["ping"])
register_discord(ds_router, commands.ship, ["ship"], r"(?P<first>[A-Za-z]+)[\s+&]+(?P<second>[A-Za-z]+)")
register_discord(ds_router, commands.emojify, ["emojify"], r"(?P<message>.+)")
register_discord(ds_router, commands.dog_any, ["dog", "doggo", "cane", "woof", "bau"])
register_discord(ds_router, commands.dog_breedlist, ["dog", "doggo", "cane", "woof", "bau"], r"(?:list|help|aiuto)")
register_discord(ds_router, commands.dog_breed, ["dog", "doggo", "cane", "woof", "bau"], r"(?P<breed>[A-Za-z/]+)")
register_discord(ds_router, commands.fortune, ["fortune"])
register_discord(ds_router, commands.pmots, ["pmots"])
register_discord(ds_router, commands.spell, ["spell", "cast"], r"(?P<spellname>.+)")
register_discord(ds_router, commands.smecds, ["smecds"])
register_discord(ds_router, commands.man, ["man", "help"], r"(?P<commandname>[A-Za-z]+)")
register_discord(ds_router, commands.login, ["login"])
register_discord(ds_router, commands.whoami, ["whoami"])
register_discord(ds_router, commands.fiorygi_balance_self, ["balance"])
register_discord(ds_router, commands.fiorygi_balance_other, ["balance"], r"(?P<target>\S+)")
register_discord(ds_router, commands.fiorygi_give, ["give"], r"(?P<target>\S+)\s+(?P<amount>[0-9]+)\s+(?P<reason>.+)")
register_discord(ds_router, commands.fiorygi_magick, ["magick"], r"(?P<target>\S+)\s+(?P<amount>[0-9]+)\s+(?P<reason>.+)")
register_discord(ds_router, commands.fiorygi_transactions_self, ["transactions"])
register_discord(ds_router, commands.fiorygi_transactions_other, ["transactions"], r"(?P<target>\S+)")
register_discord(ds_router, commands.fiorygi_dig, ["dig"], r"(?P<slug>[a-z0-9-]+)")
register_discord(ds_router, commands.fiorygi_bury, ["bury"], r"(?P<slug>[a-z0-9-]+)\s+(?P<value>[0-9]+)(?:\s+(?P<message>.+))?")
register_discord(ds_router, commands.version, ["version"])
pda.implementations["telethon.1"].register_conversation(tg_router)
pda.implementations["discordpy.2"].register_conversation(ds_router)

View file

@ -1,9 +1,11 @@
import discord
import royalnet.royaltyping as t
import royalnet.engineer as engi
import sqlalchemy.orm as so
import royalpack.database as db
import royalpack.config as cfg
import royalnet_telethon.bullet.contents
import royalnet_discordpy
import aiohttp
import asyncio
import logging
@ -84,6 +86,16 @@ async def login(*, _msg: engi.Message, _session: so.Session, _imp, **__):
log.debug(f"Done, notifying the user...")
await private.send_message(text=f"↔️ Sincronizzazione con Telegram riuscita! Sei loggato come {tg.mention()}!")
elif isinstance(_imp, royalnet_discordpy.DiscordpyPDAImplementation):
sender = await _msg.sender
tg = await register_user_discord(session=_session, user_info=ui, discord_user=sender._user)
log.debug(f"Committing session...")
_session.commit()
log.debug(f"Done, notifying the user...")
await private.send_message(text=f"↔️ Sincronizzazione con Telegram riuscita! Sei loggato come {tg.mention()}!")
async def enforce_private_message(msg: engi.Message) -> engi.Channel:
"""
@ -314,4 +326,31 @@ async def register_user_telethon(
return tg
async def register_user_discord(
session: so.Session,
user_info: dict[str, t.Any],
discord_user: discord.User,
) -> db.DiscordAccount:
"""
Sync an user's Discord account via a Discord.py message.
:param session: The :class:`~.so.Session` to use.
:param user_info: The user_info obtained by the Identity Provider.
:param discord_user: The Discord.py user to base the user data on.
:return: The created/updated :class:`~.db.DiscordAccount`
"""
log.debug("Syncing telethon user...")
ds = db.DiscordAccount(
user_fk=user_info["sub"],
id=discord_user.id,
username=discord_user.name,
discriminator=discord_user.discriminator,
avatar_url=str(discord_user.avatar_url),
)
session.merge(ds)
return ds
__all__ = ("login",)