mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-23 19:44:20 +00:00
Create reminder command
This commit is contained in:
parent
094a961b51
commit
12805f5bc1
5 changed files with 43 additions and 6 deletions
|
@ -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)
|
||||
|
|
|
@ -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"]
|
||||
|
|
27
royalnet/commands/reminder.py
Normal file
27
royalnet/commands/reminder.py
Normal file
|
@ -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]")
|
|
@ -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"]
|
||||
|
|
|
@ -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."""
|
||||
|
|
Loading…
Reference in a new issue