1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00
royalnet/rpgpack/utils/getactivechar.py

37 lines
1.3 KiB
Python
Raw Normal View History

2020-02-18 13:48:21 +00:00
from typing import *
from ..tables import DndActiveCharacter
2020-02-22 01:16:54 +00:00
from ..utils import get_interface_data
2020-02-18 13:48:21 +00:00
import royalnet.utils as ru
import royalnet.commands as rc
import pickle
2020-09-16 00:37:31 +00:00
async def get_active_character(*, data: rc.CommandData, session) -> Optional[DndActiveCharacter]:
alchemy = data.alchemy
user = await data.find_author(session=session, required=True)
2020-02-18 13:48:21 +00:00
idata = get_interface_data(data)
DndAcChT = alchemy.get(DndActiveCharacter)
active_characters: List[DndActiveCharacter] = await ru.asyncify(
2020-09-16 00:37:31 +00:00
session
.query(DndAcChT)
.filter_by(interface_name=data.command.serf.__class__.__name__, user=user)
.all
2020-02-18 13:48:21 +00:00
)
for active_character in active_characters:
2020-09-16 00:37:31 +00:00
if data.command.serf.__class__.__name__ == "TelegramSerf":
2020-02-18 13:48:21 +00:00
# interface_data is chat id
chat_id = pickle.loads(active_character.interface_data)
if chat_id == idata:
return active_character
2020-09-16 00:37:31 +00:00
elif data.command.serf.__class__.__name__ == "DiscordSerf":
2020-02-18 13:48:21 +00:00
# interface_data is channel id
chat_id = pickle.loads(active_character.interface_data)
if chat_id == idata:
return active_character
else:
raise rc.UnsupportedError("This interface isn't supported yet.")
return None