From 526a2a493a718d4c4b21d24ba1d14c3b3dfc7b11 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 11 Apr 2019 19:21:09 +0200 Subject: [PATCH] Add Discord sync command --- royalnet/commands/sync.py | 42 ++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/royalnet/commands/sync.py b/royalnet/commands/sync.py index a12a75be..8601a3a3 100644 --- a/royalnet/commands/sync.py +++ b/royalnet/commands/sync.py @@ -2,7 +2,7 @@ import typing from telegram import Update, User from discord import Message, Member from ..utils import Command, Call, asyncify, UnsupportedError -from ..database.tables import Royal, Telegram +from ..database.tables import Royal, Telegram, Discord class SyncCommand(Command): @@ -11,7 +11,7 @@ class SyncCommand(Command): command_description = "Connetti il tuo account attuale a Royalnet!" command_syntax = "(royalnetusername)" - require_alchemy_tables = {Royal, Telegram} + require_alchemy_tables = {Royal, Telegram, Discord} @classmethod async def common(cls, call: Call): @@ -27,7 +27,7 @@ class SyncCommand(Command): # Find the Royal royal = await asyncify(call.session.query(call.alchemy.Royal).filter_by(username=call.args[0]).one_or_none) if royal is None: - await call.reply("⚠️ Non esiste alcun account Royalnet con quel nome.") + await call.reply("⚠️ Non esiste alcun account Royalnet con quel nome. Ricorda, gli username sono [b]case-sensitive[/b]!") return # Find if the user is already synced telegram = await asyncify(call.session.query(call.alchemy.Telegram).filter_by(tg_id=user.id).one_or_none) @@ -43,10 +43,38 @@ class SyncCommand(Command): await call.reply(f"✅ Connessione completata: {str(royal)} ⬌ {str(telegram)}") else: # Update the Telegram data - # Avatar is WIP - telegram.tg_first_name = user.first_name - telegram.tg_last_name = user.last_name - telegram.tg_username = user.username + telegram.first_name = user.first_name + telegram.last_name = user.last_name + telegram.username = user.username await call.reply(f"✅ Dati di {str(telegram)} aggiornati.") # Commit the session await asyncify(call.session.commit) + + @classmethod + async def discord(cls, call: Call): + message: Message = call.kwargs["message"] + user: typing.Optional[Member] = message.author + # Find the Royal + royal = await asyncify(call.session.query(call.alchemy.Royal).filter_by(username=call.args[0]).one_or_none) + if royal is None: + await call.reply("⚠️ Non esiste alcun account Royalnet con quel nome. Ricorda, gli username sono [b]case-sensitive[/b]!") + return + # Find if the user is already synced + discord = await asyncify(call.session.query(call.alchemy.Discord).filter_by(discord_id=user.id).one_or_none) + if discord is None: + # Create a Discord to connect to the Royal + discord = call.alchemy.Discord(royal=royal, + discord_id=user.id, + username=user.name, + discriminator=user.discriminator, + avatar_hash=user.avatar) + call.session.add(discord) + await call.reply(f"✅ Connessione completata: {str(royal)} ⬌ {str(discord)}") + else: + # Update the Discord data + discord.username = user.name + discord.discriminator = user.discriminator + discord.avatar_hash = user.avatar + await call.reply(f"✅ Dati di {str(discord)} aggiornati.") + # Commit the session + await asyncify(call.session.commit)