diff --git a/royalgames.py b/royalgames.py index b94ff602..14a079a7 100644 --- a/royalgames.py +++ b/royalgames.py @@ -1,10 +1,8 @@ import os import asyncio from royalnet.bots import TelegramBot -from royalnet.commands import PingCommand, ShipCommand, SmecdsCommand, ColorCommand, CiaoruoziCommand, SyncCommand, DiarioCommand, RageCommand +from royalnet.commands import * from royalnet.commands.debug_create import DebugCreateCommand -from royalnet.commands.author import AuthorCommand -from royalnet.commands.dateparser import DateparserCommand from royalnet.commands.error_handler import ErrorHandlerCommand from royalnet.network import RoyalnetServer from royalnet.database.tables import Royal, Telegram @@ -12,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] + AuthorCommand, DiarioCommand, RageCommand, DateparserCommand, ReminderCommand] 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) diff --git a/royalnet/commands/__init__.py b/royalnet/commands/__init__.py index 78f7657f..d9670135 100644 --- a/royalnet/commands/__init__.py +++ b/royalnet/commands/__init__.py @@ -7,7 +7,10 @@ from .color import ColorCommand from .sync import SyncCommand from .diario import DiarioCommand from .rage import RageCommand +from .dateparser import DateparserCommand +from .author import AuthorCommand +from .reminder import ReminderCommand __all__ = ["NullCommand", "PingCommand", "ShipCommand", "SmecdsCommand", "CiaoruoziCommand", "ColorCommand", - "SyncCommand", "DiarioCommand", "RageCommand"] + "SyncCommand", "DiarioCommand", "RageCommand", "DateparserCommand", "AuthorCommand", "ReminderCommand"] diff --git a/royalnet/commands/reminder.py b/royalnet/commands/reminder.py new file mode 100644 index 00000000..9b3882c1 --- /dev/null +++ b/royalnet/commands/reminder.py @@ -0,0 +1,27 @@ +import datetime +import dateparser +import typing +from ..utils import Command, Call, sleep_until + + +class ReminderCommand(Command): + + command_name = "reminder" + command_description = "Ripete quello che gli avevi chiesto dopo un po' di tempo." + command_syntax = "[ (data) ] (testo)" + + @classmethod + async def common(cls, call: Call): + match = call.args.match(r"\[ *(.+?) *] *(.+?) *$") + date_str = match.group(1) + reminder_text = match.group(2) + date: typing.Optional[datetime.datetime] + try: + date = dateparser.parse(date_str) + except OverflowError: + date = None + if date is None: + await call.reply("⚠️ La data che hai inserito non è valida.") + await call.reply(f"✅ Promemoria impostato per [b]{date.strftime('%Y-%m-%d %H:%M:%S')}[/b]") + await sleep_until(date) + await call.reply(f"❗️ Promemoria: [b]{reminder_text}[/b]") diff --git a/royalnet/utils/__init__.py b/royalnet/utils/__init__.py index c75aaa79..7768a23b 100644 --- a/royalnet/utils/__init__.py +++ b/royalnet/utils/__init__.py @@ -3,6 +3,7 @@ from .call import Call from .command import Command, CommandArgs, InvalidInputError, UnsupportedError, InvalidConfigError, ExternalError from .safeformat import safeformat from .classdictjanitor import cdj +from .sleepuntil import sleep_until __all__ = ["asyncify", "Call", "Command", "safeformat", "InvalidInputError", "UnsupportedError", "CommandArgs", - "cdj", "InvalidConfigError", "ExternalError"] + "cdj", "InvalidConfigError", "ExternalError", "sleep_until"] diff --git a/royalnet/utils/command.py b/royalnet/utils/command.py index 098f78d0..6b548d2e 100644 --- a/royalnet/utils/command.py +++ b/royalnet/utils/command.py @@ -1,3 +1,4 @@ +import re import typing if typing.TYPE_CHECKING: from .call import Call @@ -35,6 +36,13 @@ class CommandArgs(list): raise InvalidInputError(f'Tried to get invalid [{item}] slice from CommandArgs') raise ValueError(f"Invalid type passed to CommandArgs.__getattr__: {type(item)}") + def match(self, pattern: typing.Pattern) -> typing.Match: + text = " ".join(self) + match = re.match(pattern, text) + if match is None: + raise InvalidInputError("Pattern didn't match") + return match + class Command: """A generic command, called from any source."""