mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-23 19:44:20 +00:00
Add Discord sync command
This commit is contained in:
parent
34b04acd5d
commit
526a2a493a
1 changed files with 35 additions and 7 deletions
|
@ -2,7 +2,7 @@ import typing
|
||||||
from telegram import Update, User
|
from telegram import Update, User
|
||||||
from discord import Message, Member
|
from discord import Message, Member
|
||||||
from ..utils import Command, Call, asyncify, UnsupportedError
|
from ..utils import Command, Call, asyncify, UnsupportedError
|
||||||
from ..database.tables import Royal, Telegram
|
from ..database.tables import Royal, Telegram, Discord
|
||||||
|
|
||||||
|
|
||||||
class SyncCommand(Command):
|
class SyncCommand(Command):
|
||||||
|
@ -11,7 +11,7 @@ class SyncCommand(Command):
|
||||||
command_description = "Connetti il tuo account attuale a Royalnet!"
|
command_description = "Connetti il tuo account attuale a Royalnet!"
|
||||||
command_syntax = "(royalnetusername)"
|
command_syntax = "(royalnetusername)"
|
||||||
|
|
||||||
require_alchemy_tables = {Royal, Telegram}
|
require_alchemy_tables = {Royal, Telegram, Discord}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def common(cls, call: Call):
|
async def common(cls, call: Call):
|
||||||
|
@ -27,7 +27,7 @@ class SyncCommand(Command):
|
||||||
# Find the Royal
|
# Find the Royal
|
||||||
royal = await asyncify(call.session.query(call.alchemy.Royal).filter_by(username=call.args[0]).one_or_none)
|
royal = await asyncify(call.session.query(call.alchemy.Royal).filter_by(username=call.args[0]).one_or_none)
|
||||||
if royal is 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
|
return
|
||||||
# Find if the user is already synced
|
# 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)
|
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)}")
|
await call.reply(f"✅ Connessione completata: {str(royal)} ⬌ {str(telegram)}")
|
||||||
else:
|
else:
|
||||||
# Update the Telegram data
|
# Update the Telegram data
|
||||||
# Avatar is WIP
|
telegram.first_name = user.first_name
|
||||||
telegram.tg_first_name = user.first_name
|
telegram.last_name = user.last_name
|
||||||
telegram.tg_last_name = user.last_name
|
telegram.username = user.username
|
||||||
telegram.tg_username = user.username
|
|
||||||
await call.reply(f"✅ Dati di {str(telegram)} aggiornati.")
|
await call.reply(f"✅ Dati di {str(telegram)} aggiornati.")
|
||||||
# Commit the session
|
# Commit the session
|
||||||
await asyncify(call.session.commit)
|
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)
|
||||||
|
|
Loading…
Reference in a new issue