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

90 lines
3.7 KiB
Python
Raw Normal View History

2020-05-10 22:46:12 +00:00
from typing import *
2019-11-11 08:56:08 +00:00
import dateparser
import datetime
import pickle
import telegram
import discord
from sqlalchemy import and_
2020-05-10 22:46:12 +00:00
import royalnet.commands as rc
import royalnet.utils as ru
2020-08-20 01:20:53 +00:00
import royalnet.serf.telegram as rst
import royalnet.serf.discord as rsd
2020-05-10 22:46:12 +00:00
2019-11-11 08:56:08 +00:00
from ..tables import Reminder
2020-05-10 22:46:12 +00:00
class ReminderCommand(rc.Command):
2019-11-11 08:56:08 +00:00
name: str = "reminder"
aliases = ["calendar"]
description: str = "Ti ricorda di fare qualcosa dopo un po' di tempo."
syntax: str = "[ {data} ] {messaggio}"
2020-08-20 01:20:53 +00:00
def __init__(self, serf, config):
super().__init__(serf, config)
session = self.alchemy.Session()
2019-11-11 08:56:08 +00:00
reminders = (
2020-08-20 01:20:53 +00:00
session.query(self.alchemy.get(Reminder))
2019-11-11 08:56:08 +00:00
.filter(and_(
2020-08-20 01:20:53 +00:00
self.alchemy.get(Reminder).datetime >= datetime.datetime.now(),
self.alchemy.get(Reminder).interface_name == self.serf.interface_name))
2019-11-11 08:56:08 +00:00
.all()
)
for reminder in reminders:
2020-08-23 22:13:38 +00:00
self.serf.tasks.add(self._remind(reminder))
2019-11-11 08:56:08 +00:00
async def _remind(self, reminder):
2020-05-10 22:46:12 +00:00
await ru.sleep_until(reminder.datetime)
2020-08-20 01:20:53 +00:00
if isinstance(self.serf, rst.TelegramSerf):
2020-01-17 17:21:57 +00:00
chat_id: int = pickle.loads(reminder.interface_data)
2019-11-29 14:55:52 +00:00
client: telegram.Bot = self.serf.client
await self.serf.api_call(client.send_message,
chat_id=chat_id,
2020-08-20 01:20:53 +00:00
text=rst.escape(f"❗️ {reminder.message}"),
2019-11-29 14:55:52 +00:00
parse_mode="HTML",
disable_web_page_preview=True)
2020-08-20 01:20:53 +00:00
elif isinstance(self.serf, rsd.DiscordSerf):
2020-01-17 17:21:57 +00:00
channel_id: int = pickle.loads(reminder.interface_data)
2019-11-29 14:55:52 +00:00
client: discord.Client = self.serf.client
channel = client.get_channel(channel_id)
2020-08-20 01:20:53 +00:00
await channel.send(rsd.escape(f"❗️ {reminder.message}"))
2019-11-11 08:56:08 +00:00
2020-05-10 22:46:12 +00:00
async def run(self, args: rc.CommandArgs, data: rc.CommandData) -> None:
2019-11-11 08:56:08 +00:00
try:
date_str, reminder_text = args.match(r"\[\s*([^]]+)\s*]\s*([^\n]+)\s*")
2020-05-10 22:46:12 +00:00
except rc.InvalidInputError:
2019-11-11 08:56:08 +00:00
date_str, reminder_text = args.match(r"\s*(.+?)\s*\n\s*([^\n]+)\s*")
try:
2020-05-10 22:46:12 +00:00
date: Optional[datetime.datetime] = dateparser.parse(date_str, settings={
2019-11-11 08:56:08 +00:00
"PREFER_DATES_FROM": "future"
})
except OverflowError:
date = None
if date is None:
await data.reply("⚠️ La data che hai inserito non è valida.")
return
if date <= datetime.datetime.now():
await data.reply("⚠️ La data che hai specificato è nel passato.")
return
await data.reply(f"✅ Promemoria impostato per [b]{date.strftime('%Y-%m-%d %H:%M:%S')}[/b]")
2020-08-20 01:20:53 +00:00
if isinstance(self.serf, rst.TelegramSerf):
2020-01-17 17:21:57 +00:00
interface_data = pickle.dumps(data.message.chat.id)
2020-08-20 01:20:53 +00:00
elif isinstance(self.serf, rsd.DiscordSerf):
2019-11-11 08:56:08 +00:00
interface_data = pickle.dumps(data.message.channel.id)
else:
2020-05-10 22:46:12 +00:00
raise rc.UnsupportedError("This command does not support the current interface.")
2020-08-20 01:20:53 +00:00
async with data.session_acm() as session:
2020-08-23 22:13:38 +00:00
creator = await data.find_author(session=session)
2020-08-20 01:20:53 +00:00
reminder = self.alchemy.get(Reminder)(creator=creator,
interface_name=self.serf.interface_name,
interface_data=interface_data,
datetime=date,
message=reminder_text)
2020-08-23 22:13:38 +00:00
self.serf.tasks.add(self._remind(reminder))
2020-08-20 01:20:53 +00:00
session.add(reminder)
await ru.asyncify(session.commit)