1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
Administrator-py/extensions/reminder.py

110 lines
4 KiB
Python
Raw Permalink Normal View History

2021-02-03 23:06:45 +01:00
from datetime import datetime
from discord.ext import commands
from discord import Embed
2020-07-23 21:27:34 +02:00
from discord.ext.commands import BadArgument
from discord.ext import tasks
2021-02-04 10:52:13 +01:00
from discord_slash import SlashContext, cog_ext, SlashCommandOptionType
from discord_slash.utils import manage_commands
from administrator.check import is_enabled
from administrator.logger import logger
2021-02-04 10:52:13 +01:00
from administrator import db, slash
2020-08-02 20:50:11 +02:00
from administrator.utils import time_pars, seconds_to_time_string
extension_name = "reminders"
logger = logger.getChild(extension_name)
class Reminders(commands.Cog, name="Reminder"):
def __init__(self, bot: commands.Bot):
self.bot = bot
2021-02-04 10:52:13 +01:00
slash.get_cog_commands(self)
def description(self):
return "Create and manage reminders"
2021-02-04 10:52:13 +01:00
@cog_ext.cog_subcommand(base="reminder", name="add", description="Add a reminder to your reminders list", options=[
manage_commands.create_option("message", "The message", SlashCommandOptionType.STRING, True),
manage_commands.create_option("time", "When, ?D?H?M?S", SlashCommandOptionType.STRING, True)
])
@is_enabled()
2021-02-04 10:52:13 +01:00
async def reminder_add(self, ctx: SlashContext, message: str, time: str):
time = time_pars(time)
now = datetime.now()
2020-05-27 23:51:31 +02:00
s = db.Session()
2021-02-04 10:52:13 +01:00
s.add(db.Task(message, ctx.author.id, ctx.channel.id, now + time, datetime.now()))
2020-05-27 23:51:31 +02:00
s.commit()
s.close()
2021-02-04 10:52:13 +01:00
await ctx.send(content=f"""Remind you in {seconds_to_time_string(time.total_seconds())} !""")
2021-02-04 10:52:13 +01:00
@cog_ext.cog_subcommand(base="reminder", name="list", description="Show your tasks list")
@is_enabled()
async def reminder_list(self, ctx: SlashContext):
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():
embed.add_field(name=f"{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()
2021-02-04 10:52:13 +01:00
await ctx.send(embeds=[embed])
2021-02-04 10:52:13 +01:00
@cog_ext.cog_subcommand(base="reminder", name="remove", description="Remove the task withe the matching id",
options=[
manage_commands.create_option("id", "The reminder id",
SlashCommandOptionType.INTEGER, True)])
@is_enabled()
async def reminder_remove(self, ctx: SlashContext, n: int):
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.send(content="\U0001f44d")
2020-05-27 00:45:57 +02:00
else:
2021-02-04 10:52:13 +01:00
s.close()
raise BadArgument()
2020-05-27 00:45:57 +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 23:51:31 +02:00
s.commit()
s.close()
2020-05-27 23:51:31 +02:00
async def reminder_exec(self, task: db.Task):
embed = Embed(title="You have a reminder !")
user = self.bot.get_user(task.user)
embed.set_author(name=f"{user.name}#{user.discriminator}", icon_url=user.avatar_url)
embed.add_field(name=str(task.creation_date.strftime('%d/%m/%Y %H:%M')), value=task.message)
await (await self.bot.get_channel(task.channel).send(f"{user.mention}", embed=embed)).edit(content="")
2020-05-28 17:27:35 +02:00
def cog_unload(self):
self.reminders_loop.stop()
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")