2021-02-03 23:06:45 +01:00
|
|
|
from datetime import datetime
|
2020-05-26 17:51:21 +02:00
|
|
|
|
|
|
|
from discord.ext import commands
|
|
|
|
from discord import Embed
|
2020-07-23 21:27:34 +02:00
|
|
|
from discord.ext.commands import BadArgument
|
2020-05-26 17:51:21 +02:00
|
|
|
from discord.ext import tasks
|
|
|
|
|
2020-11-05 14:56:18 +01:00
|
|
|
from administrator.check import is_enabled
|
2020-07-23 21:22:47 +02:00
|
|
|
from administrator.logger import logger
|
|
|
|
from administrator import db
|
2020-08-02 20:50:11 +02:00
|
|
|
from administrator.utils import time_pars, seconds_to_time_string
|
2020-05-26 17:51:21 +02:00
|
|
|
|
|
|
|
extension_name = "reminders"
|
|
|
|
logger = logger.getChild(extension_name)
|
|
|
|
|
|
|
|
|
2020-07-23 21:34:32 +02:00
|
|
|
class Reminders(commands.Cog, name="Reminder"):
|
2020-05-26 17:51:21 +02:00
|
|
|
def __init__(self, bot: commands.Bot):
|
|
|
|
self.bot = bot
|
|
|
|
|
2020-07-23 21:34:32 +02:00
|
|
|
def description(self):
|
|
|
|
return "Create and manage reminders"
|
|
|
|
|
2020-05-26 17:51:21 +02:00
|
|
|
@commands.group("reminder", pass_context=True)
|
2020-11-05 14:56:18 +01:00
|
|
|
@is_enabled()
|
2020-05-26 17:51:21 +02:00
|
|
|
async def reminder(self, ctx: commands.Context):
|
|
|
|
if ctx.invoked_subcommand is None:
|
2020-07-23 21:27:34 +02:00
|
|
|
await ctx.invoke(self.reminder_help)
|
2020-05-26 17:51:21 +02:00
|
|
|
|
|
|
|
@reminder.group("help", pass_context=True)
|
|
|
|
async def reminder_help(self, ctx: commands.Context):
|
|
|
|
embed = Embed(title="Reminder help")
|
2020-07-23 21:22:47 +02:00
|
|
|
embed.add_field(name="reminder add <message> <time>", value="Add a reminder to your reminders list\n"
|
|
|
|
"Time: ?D?H?M?S", inline=False)
|
|
|
|
embed.add_field(name="reminder list", value="Show your tasks list", inline=False)
|
|
|
|
embed.add_field(name="reminder remove [N°]", value="Show your tasks list with if no id given\n"
|
|
|
|
"Remove the task withe the matching id", inline=False)
|
2020-05-26 17:51:21 +02:00
|
|
|
await ctx.send(embed=embed)
|
|
|
|
|
|
|
|
@reminder.group("add", pass_context=True)
|
|
|
|
async def reminder_add(self, ctx: commands.Context, message: str, time: str):
|
|
|
|
time = time_pars(time)
|
2020-05-27 00:25:59 +02:00
|
|
|
now = datetime.now()
|
2020-05-27 23:51:31 +02:00
|
|
|
s = db.Session()
|
2020-05-28 17:44:22 +02:00
|
|
|
s.add(db.Task(message, ctx.author.id, ctx.channel.id, now + time, ctx.message.created_at))
|
2020-05-27 23:51:31 +02:00
|
|
|
s.commit()
|
|
|
|
s.close()
|
2020-05-26 17:51:21 +02:00
|
|
|
|
2020-08-02 20:50:11 +02:00
|
|
|
await ctx.send(f"""Remind you in {seconds_to_time_string(time.total_seconds())} !""")
|
2020-05-26 17:51:21 +02:00
|
|
|
|
2020-05-27 00:25:59 +02:00
|
|
|
@reminder.group("list", pass_context=True)
|
|
|
|
async def reminder_list(self, ctx: commands.Context):
|
|
|
|
embed = Embed(title="Tasks list")
|
2020-05-27 23:51:31 +02:00
|
|
|
s = db.Session()
|
|
|
|
for t in s.query(db.Task).filter(db.Task.user == ctx.author.id).all():
|
2020-07-23 21:22:47 +02:00
|
|
|
embed.add_field(name=f"N°{t.id} | {t.date.strftime('%d/%m/%Y %H:%M')}", value=f"{t.message}", inline=False)
|
2020-05-27 23:51:31 +02:00
|
|
|
s.close()
|
2020-05-27 00:25:59 +02:00
|
|
|
await ctx.send(embed=embed)
|
|
|
|
|
2020-05-27 00:45:57 +02:00
|
|
|
@reminder.group("remove", pass_context=True)
|
|
|
|
async def reminder_remove(self, ctx: commands.Context, n: int = None):
|
|
|
|
if n is None:
|
|
|
|
await ctx.invoke(self.reminder_list)
|
|
|
|
else:
|
2020-05-27 23:51:31 +02:00
|
|
|
s = db.Session()
|
|
|
|
t = s.query(db.Task).filter(db.Task.id == n).first()
|
|
|
|
if t and t.user == ctx.author.id:
|
|
|
|
s.delete(t)
|
|
|
|
s.commit()
|
|
|
|
s.close()
|
|
|
|
await ctx.message.add_reaction("\U0001f44d")
|
|
|
|
else:
|
|
|
|
s.close()
|
|
|
|
raise BadArgument()
|
2020-05-27 00:45:57 +02:00
|
|
|
|
2020-05-26 17:51:21 +02:00
|
|
|
@tasks.loop(minutes=1)
|
|
|
|
async def reminders_loop(self):
|
2020-05-27 23:51:31 +02:00
|
|
|
s = db.Session()
|
|
|
|
for t in s.query(db.Task).filter(db.Task.date <= datetime.now()).all():
|
|
|
|
self.bot.loop.create_task(self.reminder_exec(t))
|
|
|
|
s.delete(t)
|
2020-05-27 00:25:59 +02:00
|
|
|
|
2020-05-27 23:51:31 +02:00
|
|
|
s.commit()
|
|
|
|
s.close()
|
2020-05-27 00:25:59 +02:00
|
|
|
|
2020-05-27 23:51:31 +02:00
|
|
|
async def reminder_exec(self, task: db.Task):
|
2020-05-27 00:25:59 +02:00
|
|
|
embed = Embed(title="You have a reminder !")
|
2020-05-27 23:59:51 +02:00
|
|
|
user = self.bot.get_user(task.user)
|
|
|
|
embed.set_author(name=f"{user.name}#{user.discriminator}", icon_url=user.avatar_url)
|
2020-07-23 21:22:47 +02:00
|
|
|
embed.add_field(name=str(task.creation_date.strftime('%d/%m/%Y %H:%M')), value=task.message)
|
2020-05-27 23:59:51 +02:00
|
|
|
await (await self.bot.get_channel(task.channel).send(f"{user.mention}", embed=embed)).edit(content="")
|
2020-05-26 17:51:21 +02:00
|
|
|
|
2020-05-28 17:27:35 +02:00
|
|
|
def cog_unload(self):
|
|
|
|
self.reminders_loop.stop()
|
|
|
|
|
2020-05-26 17:51:21 +02:00
|
|
|
|
|
|
|
def setup(bot):
|
|
|
|
logger.info(f"Loading...")
|
|
|
|
try:
|
|
|
|
reminders = Reminders(bot)
|
|
|
|
bot.add_cog(reminders)
|
|
|
|
reminders.reminders_loop.start()
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Error loading: {e}")
|
|
|
|
else:
|
|
|
|
logger.info(f"Load successful")
|
|
|
|
|
|
|
|
|
|
|
|
def teardown(bot):
|
|
|
|
logger.info(f"Unloading...")
|
|
|
|
try:
|
|
|
|
bot.remove_cog("Reminders")
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Error unloading: {e}")
|
|
|
|
else:
|
|
|
|
logger.info(f"Unload successful")
|