Skip to content

Commit

Permalink
Add Away feature for leaders
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrxyz committed May 3, 2024
1 parent 54c6b49 commit 5d27558
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,13 @@ async def fetch_vars(self) -> None:
assert isinstance(alumni_role, discord.Role)
self.alumni_role = alumni_role

away_role = discord.utils.get(
self.active_guild.roles,
name="Away from MIL",
)
assert isinstance(away_role, discord.Role)
self.away_role = away_role

reports_cog = self.get_cog("ReportsCog")
if not reports_cog:
raise ResourceNotFound("Reports cog not found.")
Expand Down
71 changes: 71 additions & 0 deletions src/leaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import TYPE_CHECKING

import discord
from discord.app_commands import NoPrivateMessage
from discord.ext import commands

from .anonymous import AnonymousReportView
Expand All @@ -26,6 +27,38 @@
MEETING_DAY = calendar.TUESDAY


class AwayView(MILBotView):
def __init__(self, bot: MILBot):
self.bot = bot
super().__init__(timeout=None)

@discord.ui.button(
label="Toggle Away",
custom_id="away:toggle",
style=discord.ButtonStyle.primary,
)
async def toggle_away(
self,
interaction: discord.Interaction,
button: discord.ui.Button,
):
member = interaction.user
if not isinstance(member, discord.Member):
raise NoPrivateMessage
if self.bot.away_role in member.roles:
await member.remove_roles(self.bot.away_role)
await interaction.response.send_message(
"You are no longer marked as away. Welcome back!",
ephemeral=True,
)
else:
await member.add_roles(self.bot.away_role)
await interaction.response.send_message(
f"You are now marked as {self.bot.away_role.mention}. Enjoy your break!",
ephemeral=True,
)


class Leaders(commands.Cog):
def __init__(self, bot: MILBot):
self.bot = bot
Expand Down Expand Up @@ -122,6 +155,28 @@ async def at_reminder(self):
view=view,
)

@commands.Cog.listener()
async def on_message(self, message: discord.Message):
# Check that leaders mentioned are not away - if they are, remind the poster
# that they are away.
if message.author.bot:
return

mentioned = message.mentions
for member in mentioned:
if (
isinstance(member, discord.Member)
and self.bot.away_role in member.roles
):
delay_seconds = 15
delete_at = message.created_at + datetime.timedelta(
seconds=delay_seconds,
)
await message.reply(
f"{member.mention} is currently away from MIL for a temporary break, and may not respond immediately. Please consider reaching out to another leader or wait for their return. (deleting this message {discord.utils.format_dt(delete_at, 'R')})",
delete_after=delay_seconds,
)

@commands.command()
@commands.is_owner()
async def prepverify(self, ctx: commands.Context):
Expand Down Expand Up @@ -156,6 +211,22 @@ async def prepanonymous(self, ctx: commands.Context):
)
await ctx.message.delete()

@commands.command()
@commands.is_owner()
async def prepaway(self, ctx: commands.Context):
view = AwayView(self.bot)
embed = discord.Embed(
title="Take a Short Break",
description="""As leaders, it's important to take breaks and recharge. If you're planning to take a short break, you can mark yourself as away to let others know. When you're ready to return, you can toggle this status off.
During your break, members who ping you will be notified that you're away and unable to assist in MIL efforts until you return. This is a great way to ensure you're not disturbed during your break.
You can use the button below to toggle your away status on and off. Enjoy your break!""",
color=self.bot.away_role.color,
)
await ctx.send(embed=embed, view=view)
await ctx.message.delete()


async def setup(bot: MILBot):
await bot.add_cog(Leaders(bot))

0 comments on commit 5d27558

Please sign in to comment.