Skip to content

Commit

Permalink
src/roles.py: Add notification for new alumni members, unprotect #alu…
Browse files Browse the repository at this point in the history
…mni, refactor audit log fetching, delete unused src/helper.py
  • Loading branch information
cbrxyz committed Dec 20, 2024
1 parent be33116 commit 6b97d9b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 31 deletions.
29 changes: 29 additions & 0 deletions src/bot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import asyncio
import datetime
import logging
import logging.handlers
import os
Expand Down Expand Up @@ -98,6 +99,7 @@ class MILBot(commands.Bot):
leave_channel: discord.TextChannel
general_channel: discord.TextChannel
member_services_channel: discord.TextChannel
alumni_channel: discord.TextChannel
errors_channel: discord.TextChannel
software_github_channel: discord.TextChannel
electrical_github_channel: discord.TextChannel
Expand Down Expand Up @@ -229,6 +231,26 @@ async def get_or_fetch_member(self, user_id: int) -> discord.Member:
member = await self.active_guild.fetch_member(user_id)
return member

async def fetch_audit_log_targeting(
self,
target_id: int,
actions: list[discord.AuditLogAction],
) -> discord.AuditLogEntry | None:
entry = [
x
async for x in self.active_guild.audit_logs(
limit=1,
after=discord.utils.utcnow() - datetime.timedelta(seconds=5),
)
if x.action
in [
discord.AuditLogAction.member_role_update,
]
and x.target is not None
and x.target.id == target_id
]
return entry[0] if entry else None

async def setup_hook(self) -> None:
# Load extensions
extensions = (
Expand Down Expand Up @@ -300,6 +322,13 @@ async def fetch_vars(self) -> None:
assert isinstance(member_services_channel, discord.TextChannel)
self.member_services_channel = member_services_channel

alumni_channel = discord.utils.get(
self.active_guild.text_channels,
name="alumni",
)
assert isinstance(alumni_channel, discord.TextChannel)
self.alumni_channel = alumni_channel

leaders_channel = discord.utils.get(
self.active_guild.text_channels,
name="leads",
Expand Down
5 changes: 0 additions & 5 deletions src/helper.py

This file was deleted.

37 changes: 11 additions & 26 deletions src/leaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ def __init__(self, bot: MILBot):
"^students-only$",
"^.*-travel$",
r"^.*-leadership$",
r"^alumni$",
]
self.perm_notify_lock = asyncio.Lock()

Expand Down Expand Up @@ -492,22 +491,16 @@ async def on_guild_channel_update(
after_members = after.members
removed_members = set(before_members) - set(after_members)
added_members = set(after_members) - set(before_members)
entry = [
x
async for x in after.guild.audit_logs(
limit=2,
after=discord.utils.utcnow() - datetime.timedelta(seconds=5),
)
if x.action
in [
entry = await self.bot.fetch_audit_log_targeting(
after.id,
[
discord.AuditLogAction.channel_update,
discord.AuditLogAction.overwrite_update,
discord.AuditLogAction.overwrite_create,
discord.AuditLogAction.overwrite_delete,
]
and x.target.id == after.id
]
user = "A user" if not entry else entry[0].user.mention
],
)
user = "A user" if not entry else entry.user.mention
if removed_members:
removed_name_str = ", ".join(
f"**{member.display_name}**" for member in removed_members
Expand Down Expand Up @@ -539,19 +532,11 @@ async def on_member_update(
async with self.perm_notify_lock:
after = await after.guild.fetch_member(after.id)
await asyncio.sleep(1)
entry = [
x
async for x in after.guild.audit_logs(
limit=1,
after=discord.utils.utcnow() - datetime.timedelta(seconds=5),
)
if x.action
in [
discord.AuditLogAction.member_role_update,
]
and x.target.id == after.id
]
user = "A user" if not entry else entry[0].user.mention
entry = await self.bot.fetch_audit_log_targeting(
after.id,
[discord.AuditLogAction.member_role_update],
)
user = "A user" if not entry else entry.user.mention
important_channels = [
c
for c in before.guild.text_channels
Expand Down
19 changes: 19 additions & 0 deletions src/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,25 @@ async def summer(self, ctx):
view = SummerRolesView(self.bot)
await ctx.send(embed=embed, view=view)

@commands.Cog.listener()
async def on_member_update(self, before: discord.Member, after: discord.Member):
# If a user is given @Alumni, send a message welcoming them to alumni channel
alumni_added = (
self.bot.alumni_role in after.roles
and self.bot.alumni_role not in before.roles
)
if not alumni_added:
return

entry = await self.bot.fetch_audit_log_targeting(
after.id,
[discord.AuditLogAction.role_update],
)
author = entry.user.mention if entry else "A user"
await self.bot.alumni_channel.send(
f"{author} gave {after.mention} the alumni role. Please welcome them to this channel! :wave:",
)


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

0 comments on commit 6b97d9b

Please sign in to comment.