diff --git a/docs/DOCUMENTATION.md b/docs/DOCUMENTATION.md index ef3b205..6e36793 100644 --- a/docs/DOCUMENTATION.md +++ b/docs/DOCUMENTATION.md @@ -59,6 +59,16 @@ Once a member (not a bot) has all 3, the "Not Registered" Role will be removed. **Note:** Will remove all courses from every member and removes all reaction channels for courses. \ **Purpose:** To initiate a fresh restart for all members and avoid having old members in courses they are not currently taking. +### Auto Course Reactions +**Command:** `.auto_course_reactions` \ +**Aliases:** `.autoCourseReactions` \ +**Example:** `.auto_course_reactions` \ +**Permissions:** Administrator \ +**Note:** This assumes the format of the course content is like: +:reaction1: -> Course1 Description (CRSN-1234)\n +:reaction2: -> Course2 Description (CRSN-4321) \ +**Purpose:** To automate creating course reaction channels if only embeds and content exist. This might happen when a completely new course registration page needs to be made or if resetting the semester also gets rid of all the reaction channels. + ### Clear Courses **Command:** `.clear_courses ` \ **Aliases:** `.clearCourses` \ diff --git a/src/cogs/course_registration/course_cleanup.py b/src/cogs/course_registration/course_cleanup.py index 411d55e..8111de3 100644 --- a/src/cogs/course_registration/course_cleanup.py +++ b/src/cogs/course_registration/course_cleanup.py @@ -59,6 +59,52 @@ async def new_semester(self, ctx: commands.Context) -> None: f"Finished reseting all reaction channels in {COURSE_REGISTRATION_CHANNEL.mention}!" ) + @is_admin() + @commands.guild_only() + @commands.command(aliases=["autoCourseReactions"]) + async def auto_course_reactions(self, ctx: commands.Context) -> None: + """Creates course reaction channels for every course in #course-registration + automatically just by looking at the course content under each course embedded + message. + This assumes the format of the course content is like: + :reaction1: -> Course1 Description (CRSN-1234)\n + :reaction2: -> Course2 Description (CRSN-4321) + + Parameters + ------------ + ctx: `commands.Context` + A class containing metadata about the command invocation. + """ + guild: discord.Guild = ctx.guild + COURSE_REGISTRATION_CHANNEL: discord.TextChannel = guild.get_channel( + COURSE_REGISTRATION_CHANNEL_ID + ) + messages = await COURSE_REGISTRATION_CHANNEL.history( + limit=None, oldest_first=True + ).flatten() + for index, message in enumerate(messages): + if message.embeds and "Add/Remove" in str(message.embeds[0].title): + newrc_command: commands.Command = self.client.get_command("newrc") + for course_line in messages[index + 1].content.split("\n"): + reaction, course_name = course_line.split(" -> ") + start_index: int = course_name.find("(") + 1 + end_index: int = course_name.find(")") + course_acronym: str = course_name[start_index:end_index].replace( + " ", "-" + ) + target_channel: discord.TextChannel = discord.utils.find( + lambda c: c.topic and course_acronym in c.topic, + guild.text_channels, + ) + await ctx.invoke( + newrc_command, + COURSE_REGISTRATION_CHANNEL, + message.id, + reaction, + target_channel, + ) + await ctx.send("All reaction channels were added back for courses!") + @is_admin() @commands.guild_only() @commands.command(aliases=["clearCourses"]) diff --git a/src/cogs/help.py b/src/cogs/help.py index 9b1c8e3..5be7119 100644 --- a/src/cogs/help.py +++ b/src/cogs/help.py @@ -29,8 +29,7 @@ def _get_embed(self, title: str) -> discord.Embed: async def help(self, ctx: commands.Context, *, args=None) -> None: if args: await ctx.send( - f"`{args}` is not a recognized option", - delete_after=5, + f"`{args}` is not a recognized option", delete_after=5, ) return @@ -219,7 +218,7 @@ async def course_cleanup(self, ctx: commands.Context) -> None: embed = self._get_embed("Course Cleanup") embed.add_field( name="Commands", - value="`.new_semester`, `.clear_courses`, `.clear_reactions`", + value="`.new_semester`, `.auto_course_reactions`, `.clear_courses`, `.clear_reactions`", inline=False, ) embed.add_field( @@ -227,6 +226,11 @@ async def course_cleanup(self, ctx: commands.Context) -> None: value="Resets course roles and course reaction roles for new semester", inline=False, ) + embed.add_field( + name="auto_course_reactions", + value="Creates course reaction channels for everything in course-registration", + inline=False, + ) embed.add_field( name="clear_courses", value="Removes all course roles from a specific member", @@ -264,6 +268,33 @@ async def new_semester(self, ctx: commands.Context) -> None: ) await ctx.send(embed=embed) + @is_admin() + @help.group(aliases=["autoCourseReactions"]) + async def auto_course_reactions(self, ctx: commands.Context) -> None: + embed = self._get_embed("auto_course_reactions") + embed.add_field(name="Command", value="`.auto_course_reactions`", inline=False) + embed.add_field(name="Aliases", value="`.autoCourseReactions`", inline=False) + embed.add_field(name="Example", value="`.auto_course_reactions`", inline=False) + embed.add_field( + name="Note", + value=( + "This assumes the format of the course content is like:\n" + ":reaction1: -> Course1 Description (CRSN-1234)\n" + ":reaction2: -> Course2 Description (CRSN-4321)" + ), + inline=False, + ) + embed.add_field( + name="Purpose", + value=( + "To automate creating course reaction channels if only embeds and content exist. " + "This might happen when a completely new course registration page needs to be made " + "or if resetting the semester also gets rid of all the reaction channels." + ), + inline=False, + ) + await ctx.send(embed=embed) + @is_admin() @help.group(aliases=["clearCourses"]) async def clear_courses(self, ctx: commands.Context) -> None: