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

Start work on kv commands

This commit is contained in:
Steffo 2019-04-09 12:41:03 +02:00
parent 8f7fe39c27
commit 30b9ec4b5d
9 changed files with 115 additions and 3 deletions

View file

@ -10,7 +10,7 @@ from royalnet.database.tables import Royal, Telegram
loop = asyncio.get_event_loop()
commands = [PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, DebugCreateCommand, SyncCommand,
AuthorCommand, DiarioCommand, RageCommand, DateparserCommand, ReminderCommand]
AuthorCommand, DiarioCommand, RageCommand, DateparserCommand, ReminderCommand, KvactiveCommand, KvCommand]
master = RoyalnetServer("localhost", 1234, "sas")
tg_bot = TelegramBot(os.environ["TG_AK"], "localhost:1234", "sas", commands, os.environ["DB_PATH"], Royal, Telegram, "tg_id", error_command=ErrorHandlerCommand)

View file

@ -10,7 +10,10 @@ from .rage import RageCommand
from .dateparser import DateparserCommand
from .author import AuthorCommand
from .reminder import ReminderCommand
from .kvactive import KvactiveCommand
from .kv import KvCommand
__all__ = ["NullCommand", "PingCommand", "ShipCommand", "SmecdsCommand", "CiaoruoziCommand", "ColorCommand",
"SyncCommand", "DiarioCommand", "RageCommand", "DateparserCommand", "AuthorCommand", "ReminderCommand"]
"SyncCommand", "DiarioCommand", "RageCommand", "DateparserCommand", "AuthorCommand", "ReminderCommand",
"KvactiveCommand", "KvCommand"]

34
royalnet/commands/kv.py Normal file
View file

@ -0,0 +1,34 @@
from ..database.tables import ActiveKvGroup, Royal, Keyvalue
from ..utils import Command, Call, asyncify
class KvCommand(Command):
command_name = "kv"
command_description = "Visualizza o modifica un valore rv."
command_syntax = "(nomegruppo)"
require_alchemy_tables = {ActiveKvGroup, Royal, Keyvalue}
@classmethod
async def common(cls, call: Call):
key = call.args[0]
value = call.args.optional(1)
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)
keyvalue = await asyncify(call.session.query(call.alchemy.Keyvalue).filter_by(group=active.group_name, key=key).one_or_none)
if value is None:
# Get
if keyvalue is None:
await call.reply("⚠️ La chiave specificata non esiste.")
return
await call.reply(f" Valore della chiave:\n{keyvalue}")
else:
# Set
if keyvalue is None:
keyvalue = call.alchemy.Keyvalue(group=active, key=key, value=value)
call.session.add(keyvalue)
else:
keyvalue.value = value
await asyncify(call.session.commit)
await call.reply(f"✅ Chiave aggiornata:\n{keyvalue}")

View file

@ -0,0 +1,24 @@
from ..database.tables import ActiveKvGroup, Royal
from ..utils import Command, Call, asyncify
class KvactiveCommand(Command):
command_name = "kvactive"
command_description = "Seleziona un gruppo di valori kv."
command_syntax = "(nomegruppo)"
require_alchemy_tables = {ActiveKvGroup, Royal}
@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:
active = call.alchemy.ActiveKvGroup(royal=author, group=group_name)
call.session.add(active)
else:
active.group = group_name
await asyncify(call.session.commit)
await call.reply(f"✅ Hai attivato il gruppo [b]{group_name}[/b].")

View file

@ -2,5 +2,7 @@ from .royals import Royal
from .telegram import Telegram
from .diario import Diario
from .aliases import Alias
from .activekvgroup import ActiveKvGroup
from .keyvalue import Keyvalue
__all__ = ["Royal", "Telegram", "Diario", "Alias"]
__all__ = ["Royal", "Telegram", "Diario", "Alias", "ActiveKvGroup", "Keyvalue"]

View file

@ -0,0 +1,18 @@
from sqlalchemy import Column, \
String, \
Integer, \
ForeignKey
from sqlalchemy.orm import relationship
class ActiveKvGroup:
__tablename__ = "activekvgroups"
royal_id = Column(Integer, ForeignKey("royals.uid"), primary_key=True)
group_name = Column(String, ForeignKey("keygroups.group_name"), nullable=False)
royal = relationship("Royal", backref="active_kv_group")
group = relationship("Keygroup", backref="users_with_this_active")
def __repr__(self):
return f"<ActiveKvGroup royal={self.royal} group={self.group_name}>"

View file

@ -0,0 +1,13 @@
from sqlalchemy import Column, \
String, \
ForeignKey
class Keygroup:
__tablename__ = "keygroups"
group_name = Column(String, ForeignKey("keygroups.group_name"), primary_key=True)
def __repr__(self):
return f"<Keygroup {self.group_name}>"

View file

@ -0,0 +1,17 @@
from sqlalchemy import Column, \
String, \
ForeignKey
class Keyvalue:
__tablename__ = "keyvalues"
group = Column(String, ForeignKey("keygroups.group_name"), primary_key=True)
key = Column(String, primary_key=True)
value = Column(String, nullable=False)
def __repr__(self):
return f"<Keyvalue group={self.group} key={self.key} value={self.value}>"
def __str__(self):
return f"{self.key}: [b]{self.value}[/b]"

View file

@ -49,6 +49,7 @@ class CommandArgs(list):
except IndexError:
return None
class Command:
"""A generic command, called from any source."""