From 04d03fa2cb581660374c7c34e7c01f644beb2d17 Mon Sep 17 00:00:00 2001 From: Stijn De Clercq Date: Sun, 26 Sep 2021 21:24:34 +0200 Subject: [PATCH] Customize les reminder per minor --- cogs/tasks.py | 9 ++++++++- data/remind.py | 10 ++++++++-- data/schedule.py | 24 ++++++++++++++++++++++-- files/schedules/31.json | 3 +++ settings.py | 3 +++ 5 files changed, 44 insertions(+), 5 deletions(-) diff --git a/cogs/tasks.py b/cogs/tasks.py index 8209d19..a893338 100644 --- a/cogs/tasks.py +++ b/cogs/tasks.py @@ -203,7 +203,7 @@ class Tasks(commands.Cog): continue # Create embed once because this can be heavy - if "embed" in category: + if "embed" in category and category["embed_once"]: embed = category["embed"]() else: embed = None @@ -219,6 +219,13 @@ class Tasks(commands.Cog): if "embed" not in category: await userInstance.send(random.choice(category["messages"])) else: + # Embed has to be customized per user + if embed is None and "embed_once" in category: + # TODO clean this up, there's a better way :) + # remind category with a before- method & setup per user + args = category["argsf"](self.client, user) + embed = category["embed"](*args) + await userInstance.send(random.choice(category["messages"]), embed=embed) with open("files/lastTasks.json", "w") as fp: diff --git a/data/remind.py b/data/remind.py index 39f6dd3..107e637 100644 --- a/data/remind.py +++ b/data/remind.py @@ -1,3 +1,5 @@ +from typing import Optional + from data import schedule from functions import les, config from functions.database import remind @@ -13,11 +15,15 @@ class Reminders: 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": self.lesEmbed, "weekends": False, "disabled": True} + self.les = {"users": self._les, "messages": self._lesMessages, "embed": self.les_embed, "argsf": schedule.find_minor, "embed_once": False, "weekends": False, "disabled": False} self.categories = [self.nightly, self.les] - def lesEmbed(self): + def les_embed(self, minor: Optional[int] = None): dt = les.find_target_date() s = schedule.Schedule(dt, int(config.get("year")), int(config.get("semester"))) + + if minor is not None: + return s.create_schedule(minor_roles=[minor]).to_embed() + return s.create_schedule().to_embed() diff --git a/data/schedule.py b/data/schedule.py index 177b9d0..7fc3818 100644 --- a/data/schedule.py +++ b/data/schedule.py @@ -3,10 +3,13 @@ from dacite import from_dict from dataclasses import dataclass, field from datetime import datetime, timedelta from discord import Colour, Embed +from discord.ext import commands + from enums.platform import Platform, get_platform from functions.timeFormatters import fromArray, intToWeekday, timeFromInt +from settings import COC_ID import json -from typing import Dict, Optional, List +from typing import Dict, Optional, List, Tuple @dataclass @@ -235,7 +238,7 @@ class Schedule: return slots_today - def create_schedule(self): + def create_schedule(self, minor_roles: Optional[List[int]] = None): """ Create the schedule for the current week """ @@ -247,6 +250,10 @@ class Schedule: # Find minor slots for minor in self.schedule_dict["minors"]: + # Customized schedule + if minor_roles is not None and minor["role"] not in minor_roles: + continue + m_slots = [] for course in minor["schedule"]: # Go over every course @@ -398,3 +405,16 @@ class ScheduleEmbed(LesEmbed): return "" return "\n".join(list(f"{entry.course}: **{entry.get_link_str()}**" for entry in has_link)) + + +def find_minor(client: commands.Bot, userid: int) -> Tuple[Optional[int]]: + guild = client.get_guild(COC_ID) + user = guild.get_member(userid) + + minors_ids = [891744461405687808, 891744390035415111] + + for role in user.roles: + if role.id in minors_ids: + return role.id, + + return None, diff --git a/files/schedules/31.json b/files/schedules/31.json index 1128639..3d4a244 100644 --- a/files/schedules/31.json +++ b/files/schedules/31.json @@ -14,6 +14,7 @@ "minors": [ { "name": "Beveiliging en parallelle systemen", + "role": 891744461405687808, "online_links": { "zoom": "https://ufora.ugent.be/d2l/ext/rp/439235/lti/framedlaunch/556e197e-e87b-4c27-be5d-53adc7a41826" }, @@ -95,6 +96,7 @@ }, { "name": "Elektrotechniek en Telecommunicatie", + "role": 891744390035415111, "schedule": [ { "course": "Inleiding tot Telecommunicatie", @@ -170,6 +172,7 @@ }, { "name": "Onderwijs", + "role": -1, "schedule": [] } ], diff --git a/settings.py b/settings.py index 038fd89..b2fed8e 100644 --- a/settings.py +++ b/settings.py @@ -35,6 +35,9 @@ HOST_IPC = _to_bool(os.getenv("HOSTIPC", "false")) READY_MESSAGE = os.getenv("READYMESSAGE", "I'M READY I'M READY I'M READY I'M READY") # Yes, this is a Spongebob reference STATUS_MESSAGE = os.getenv("STATUSMESSAGE", "with your Didier Dinks.") +# Id's of servers/channels +COC_ID: int = int(os.getenv("COC_ID", "626699611192688641")) + # Guilds to test slash commands in # Ex: 123,456,789 _guilds = os.getenv("SLASHTESTGUILDS", "").replace(" ", "")