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

33 lines
1.4 KiB
Python
Raw Normal View History

2019-04-09 15:00:14 +00:00
from ..database.tables import ActiveKvGroup, Royal, Keygroup
2019-04-09 10:41:03 +00:00
from ..utils import Command, Call, asyncify
class KvactiveCommand(Command):
command_name = "kvactive"
command_description = "Seleziona un gruppo di valori kv."
command_syntax = "(nomegruppo)"
2019-04-09 15:04:21 +00:00
require_alchemy_tables = {ActiveKvGroup, Royal, Keygroup}
2019-04-09 10:41:03 +00:00
@classmethod
async def common(cls, call: Call):
group_name = call.args[0].lower()
author = await call.get_author(error_if_none=True)
active = await asyncify(call.session.query(call.alchemy.ActiveKvGroup).filter_by(royal=author).one_or_none)
if active is None:
2019-04-09 15:00:14 +00:00
group = await asyncify(call.session.query(call.alchemy.Keygroup).filter_by(group_name=group_name).one_or_none)
if group is None:
group = call.alchemy.Keygroup(group_name=group_name)
call.session.add(group)
active = call.alchemy.ActiveKvGroup(royal=author, group=group)
2019-04-09 10:41:03 +00:00
call.session.add(active)
else:
2019-04-09 15:00:14 +00:00
group = await asyncify(call.session.query(call.alchemy.Keygroup).filter_by(group_name=group_name).one_or_none)
if group is None:
group = call.alchemy.Keygroup(group_name=group_name)
call.session.add(group)
active.group = group
2019-04-09 10:41:03 +00:00
await asyncify(call.session.commit)
await call.reply(f"✅ Hai attivato il gruppo [b]{group_name}[/b].")