From 250c2d40c7b7135635de8e7d37d8a68167d3fe6d Mon Sep 17 00:00:00 2001 From: Stijn De Clercq Date: Fri, 23 Oct 2020 21:34:10 +0200 Subject: [PATCH] Create reminder task & command, fix Les aliases --- cogs/remind.py | 52 ++++++++++++++++++++++++++++++++++++ cogs/school.py | 2 +- cogs/tasks.py | 20 ++++++++++++-- data/remind.py | 16 +++++++++++ files/help.json | 4 ++- functions/database/remind.py | 43 +++++++++++++++++++++++++++++ 6 files changed, 133 insertions(+), 4 deletions(-) create mode 100644 cogs/remind.py create mode 100644 data/remind.py create mode 100644 functions/database/remind.py diff --git a/cogs/remind.py b/cogs/remind.py new file mode 100644 index 0000000..5ca428c --- /dev/null +++ b/cogs/remind.py @@ -0,0 +1,52 @@ +import discord +from discord.ext import commands +from decorators import help +from enums.help_categories import Category +from functions.database import remind + + +class Remind(commands.Cog): + def __init__(self, client): + self.client = client + + # Don't allow any commands to work when locked + def cog_check(self, ctx): + return not self.client.locked + + @commands.group(name="Remind", aliases=["Remindme"], usage="[Categorie]", case_insensitive=True, invoke_without_command=True) + async def remind(self, ctx): + """ + Command group to remind the user of a certain thing every day. + :param ctx: Discord Context + """ + categories = ["Les", "Nightly"] + embed = discord.Embed(colour=discord.Colour.blue()) + + embed.set_author(name="Remind Categorieën") + embed.description = "\n".join(sorted(categories)) + + await ctx.send(embed=embed) + + @remind.command(name="Nightly") + async def nightly(self, ctx): + """ + Command to get a daily Nightly reminder + """ + if remind.switchReminder(ctx.author.id, "nightly"): + await ctx.send("Vanaf nu wordt je er dagelijks aan herinnerd om Didier Nightly te doen.") + else: + await ctx.send("Je zal er niet langer aan herinnerd worden om Didier Nightly te doen.") + + @remind.command(name="Les", aliases=["Class", "Classes", "Sched", "Schedule"]) + async def les(self, ctx): + """ + Command to get a daily reminder with an embed of your schedule + """ + if remind.switchReminder(ctx.author.id, "les"): + await ctx.send("Vanaf nu krijg je dagelijks je lessenrooster toegestuurd.") + else: + await ctx.send("Je zal je lessenrooster niet langer toegestuurd krijgen.") + + +def setup(client): + client.add_cog(Remind(client)) diff --git a/cogs/school.py b/cogs/school.py index aa12d3d..36fe585 100644 --- a/cogs/school.py +++ b/cogs/school.py @@ -41,7 +41,7 @@ class School(commands.Cog): embed.set_footer(text="Omwille van de coronamaatregelen is er een beperkter aanbod, en kan je enkel nog eten afhalen. Ter plaatse eten is niet meer mogelijk.") await ctx.send(embed=embed) - @commands.command(name="Les", aliases=["Sched", "Schedule", "Class"], usage="[Jaargang]* [Dag]*") + @commands.command(name="Les", aliases=["Class", "Classes", "Sched", "Schedule"], usage="[Jaargang]* [Dag]*") @commands.check(checks.allowedChannels) @help.Category(category=Category.School) async def les(self, ctx, *day): diff --git a/cogs/tasks.py b/cogs/tasks.py index d161462..2acccb3 100644 --- a/cogs/tasks.py +++ b/cogs/tasks.py @@ -1,9 +1,11 @@ from data import constants +from data.remind import Reminders from discord.ext import commands, tasks from enums.numbers import Numbers from functions import timeFormatters from functions.database import currency, poke, prison, birthdays, stats import json +from random import random import requests import time @@ -181,8 +183,22 @@ class Tasks(commands.Cog): # Don't do it multiple times a day if bot dc's, ... with open("files/lastTasks.json", "r") as fp: lastTasks = json.load(fp) - if int(self.getCurrentHour()) == 0 and int(time.time()) - int(lastTasks["remind"]) > 10000: - pass + if int(self.getCurrentHour()) == 21 and int(time.time()) - int(lastTasks["remind"]) > 10000: + reminders = Reminders() + + for category in reminders.categories: + for user in category["users"]: + userInstance = await self.client.fetch_user(user) + + # User can't be fetched for whatever reason, ignore instead of crashing + if userInstance is None: + continue + + # Check if a special embed has to be attached for this reminder + if "embed" not in category: + await userInstance.send(random.choice(category["messages"])) + else: + await userInstance.send(random.choice(category["messages"]), embed=category["embed"]) # with open("files/lastTasks.json", "w") as fp: # lastTasks["remind"] = round(time.time()) diff --git a/data/remind.py b/data/remind.py new file mode 100644 index 0000000..7fbb69a --- /dev/null +++ b/data/remind.py @@ -0,0 +1,16 @@ +from functions.database import remind + + +class Reminders: + def __init__(self): + rows = remind.getAllRows() + + self._nightlyUsers = [int(user[0]) for user in rows if user[1]] + self._nightlyMessages = ["Dagelijkse herinnering om Didier Nightly te doen.", "Vrees niet, Nightly-streak-liefhebber! 't Zenne kik, Didier, me ne reminder!"] + self.nightly = {"users": self._nightlyUsers, "messages": self._nightlyMessages} + + self._les = [int(user[0]) for user in rows if user[2]] + self._lesMessages = ["Lessenrooster voor vandaag:"] + self.les = {"users": self._les, "messages": self._lesMessages, "embed": None} + + self.categories = [self.nightly, self.les] diff --git a/files/help.json b/files/help.json index 64ff0c9..4cfabbe 100644 --- a/files/help.json +++ b/files/help.json @@ -86,7 +86,9 @@ "releases": "Lijst van de games die in de volgende 30 dagen uitkomen.\nGeef een getal op om volgende/vorige pagina's te bekijken (standaard pagina 1).", "releases info": "Geeft gedetailleerde informatie over de game met de opgegeven id.", "reload": "Verwijdert [Cog] & voegt hem daarna terug toe.", - "remind": "Krijg een dagelijkse herinnering om Didier Nightly te doen.\nMeer categorieën en usages komen later:tm:.", + "remind": "Krijg een lijst van categorieën waaraan je dagelijks herinnerd kan worden.\nOm deze herinneringen niet langer te krijgen, kan je opnieuw Didier Remind [Categorie] doen.", + "remind les": "Krijg dagelijks het lessenrooster voor die dag toegestuurd.", + "remind nightly": "Krijg dagelijks een herinnering om Didier Nightly te doen.", "reverse": "Returnt tekst achterstevoren.", "rhyme": "Stuurt de 15 beste suggesties voor woorden die rijmen op [Woord].", "rob": "Probeer Didier Dinks te stelen van [Persoon].", diff --git a/functions/database/remind.py b/functions/database/remind.py new file mode 100644 index 0000000..ebbebf5 --- /dev/null +++ b/functions/database/remind.py @@ -0,0 +1,43 @@ +from functions.database import utils + + +def getAllRows(): + connection = utils.connect() + cursor = connection.cursor() + + cursor.execute("SELECT * FROM remind") + return cursor.fetchall() + + +def getOrAddUser(userid): + connection = utils.connect() + cursor = connection.cursor() + + cursor.execute("SELECT * FROM remind WHERE userid = %s", (int(userid),)) + res = cursor.fetchall() + + if not res: + cursor.execute("INSERT INTO remind(userid) VALUES %s", (int(userid),)) + connection.commit() + + return getOrAddUser(userid) + + return res[0] + + +def switchReminder(userid, column): + connection = utils.connect() + cursor = connection.cursor() + + columns = ["id", "nightly", "les"] + + res = getOrAddUser(userid) + + # Switch the column value + to = not (res[columns.index(column)]) + + cursor.execute("UPDATE remind SET %s = %s WHERE userid = %s", (column, to, int(userid),)) + connection.commit() + + return to +