1
0
Fork 0

Add db support

This commit is contained in:
Ethanell 2020-05-27 23:51:31 +02:00
parent bc1bbed07f
commit 7a09b0f7f3

View file

@ -7,6 +7,7 @@ from discord.ext.commands import CommandNotFound, BadArgument, MissingRequiredAr
from discord.ext import tasks from discord.ext import tasks
from bot_bde.logger import logger from bot_bde.logger import logger
from bot_bde import db
extension_name = "reminders" extension_name = "reminders"
@ -26,7 +27,6 @@ def time_pars(s: str) -> timedelta:
class Reminders(commands.Cog): class Reminders(commands.Cog):
def __init__(self, bot: commands.Bot): def __init__(self, bot: commands.Bot):
self.bot = bot self.bot = bot
self.tasks = []
@commands.group("reminder", pass_context=True) @commands.group("reminder", pass_context=True)
async def reminder(self, ctx: commands.Context): async def reminder(self, ctx: commands.Context):
@ -47,13 +47,10 @@ class Reminders(commands.Cog):
async def reminder_add(self, ctx: commands.Context, message: str, time: str): async def reminder_add(self, ctx: commands.Context, message: str, time: str):
time = time_pars(time) time = time_pars(time)
now = datetime.now() now = datetime.now()
self.tasks.append({ s = db.Session()
"date": now + time, s.add(db.Task(message, ctx.author.id, ctx.channel.id, now + time))
"create": now, s.commit()
"user": ctx.author.id, s.close()
"message": message,
"channel": ctx.channel.id,
})
hours, seconds = divmod(time.seconds, 3600) hours, seconds = divmod(time.seconds, 3600)
minutes, seconds = divmod(seconds, 60) minutes, seconds = divmod(seconds, 60)
@ -65,37 +62,42 @@ class Reminders(commands.Cog):
@reminder.group("list", pass_context=True) @reminder.group("list", pass_context=True)
async def reminder_list(self, ctx: commands.Context): async def reminder_list(self, ctx: commands.Context):
embed = Embed(title="Tasks list") embed = Embed(title="Tasks list")
for i, t in enumerate(self.tasks): s = db.Session()
if t["user"] == ctx.author.id: for t in s.query(db.Task).filter(db.Task.user == ctx.author.id).all():
embed.add_field(name=t["date"], value=f"{i} | {t['message']}", inline=False) embed.add_field(name=f"{t.id} | {t.date}", value=f"{t.message}", inline=False)
s.close()
await ctx.send(embed=embed) await ctx.send(embed=embed)
@reminder.group("remove", pass_context=True) @reminder.group("remove", pass_context=True)
async def reminder_remove(self, ctx: commands.Context, n: int = None): async def reminder_remove(self, ctx: commands.Context, n: int = None):
tasks =list(filter(lambda t: t["user"] == ctx.author.id, self.tasks))
if n is None: if n is None:
await ctx.invoke(self.reminder_list) await ctx.invoke(self.reminder_list)
elif n >= len(tasks):
raise BadArgument()
else: else:
del self.tasks[n] s = db.Session()
await ctx.message.add_reaction("\U0001f44d") 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()
@tasks.loop(minutes=1) @tasks.loop(minutes=1)
async def reminders_loop(self): async def reminders_loop(self):
trash = [] s = db.Session()
for t in self.tasks: for t in s.query(db.Task).filter(db.Task.date <= datetime.now()).all():
if t["date"] <= datetime.now(): self.bot.loop.create_task(self.reminder_exec(t))
self.bot.loop.create_task(self.reminder_exec(t)) s.delete(t)
trash.append(t)
for t in trash: s.commit()
del self.tasks[self.tasks.index(t)] s.close()
async def reminder_exec(self, task: dict): async def reminder_exec(self, task: db.Task):
embed = Embed(title="You have a reminder !") embed = Embed(title="You have a reminder !")
embed.add_field(name=task["date"], value=task["message"]) embed.add_field(name=str(task.date), value=task.message)
await self.bot.get_channel(task["channel"]).send(f"<@{task['user']}>", embed=embed) await self.bot.get_channel(task.channel).send(f"<@{task.user}>", embed=embed)
@commands.Cog.listener() @commands.Cog.listener()
async def on_command_error(self, ctx: commands.Context, error): async def on_command_error(self, ctx: commands.Context, error):