Skip to content

Commit

Permalink
Auto Course Reactions Command (#42)
Browse files Browse the repository at this point in the history
* Create auto_course_reactions command

- This command was started as a utility command to automate the creation of all course reaction channels after the new semester command wiped them all.
- It can create course reaction channels automatically to avoid creating over 100 course reaction channels by hand which would require identifying each associated course channel, reaction, message id for each one.

* Auto format

* Add help command

- Also changes docstring to be consistent with help command note.

* Add documentation
  • Loading branch information
VishalRamesh50 authored Aug 20, 2020
1 parent b47e3fa commit bf33cd9
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
10 changes: 10 additions & 0 deletions docs/DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <member>` \
**Aliases:** `.clearCourses` \
Expand Down
46 changes: 46 additions & 0 deletions src/cogs/course_registration/course_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
37 changes: 34 additions & 3 deletions src/cogs/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -219,14 +218,19 @@ 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(
name="new_semester",
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",
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit bf33cd9

Please sign in to comment.