Skip to content

Commit 8492212

Browse files
committed
Add support for assigning roles for meeting
1 parent 91094c2 commit 8492212

File tree

4 files changed

+72
-15
lines changed

4 files changed

+72
-15
lines changed

.env.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ DISCORD_TOKEN=123
44
CALENDAR_URL=abc
55
NOTES_LINK=abc
66
SCHEDULED_EVENT_CHANNEL_ID=123
7+
REACT_FOR_MEETINGS_NOTIFICATION_MESSAGE_ID=123
8+
MEETING_WATCHERS_ROLE_ID=123

src/bot.py

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22

33
import nextcord
4+
from discord import RawReactionActionEvent
45

56
from src.database import (
67
add_notification_for_date,
@@ -9,7 +10,12 @@
910
)
1011
from src.date_utils import add_localized_times_to_embed
1112

12-
from .config import GENERAL_CHANNEL_ID, NOTES_LINK
13+
from .config import (
14+
GENERAL_CHANNEL_ID,
15+
MEETING_WATCHERS_ROLE_ID,
16+
NOTES_LINK,
17+
REACT_FOR_MEETINGS_NOTIFICATION_MESSAGE_ID,
18+
)
1319
from .meeting import find_next_event_and_notify_core_team
1420

1521

@@ -19,44 +25,89 @@ def __init__(self, *args, **kwargs):
1925

2026
self.bg_task = self.loop.create_task(self.check_events_for_next_week())
2127

22-
async def on_raw_reaction_add(self, reaction):
28+
async def on_remove_checkmark_reaction(
29+
self, reaction: RawReactionActionEvent
30+
) -> None:
31+
message_id = reaction.message_id
2332

24-
assert self.user
33+
if message_id == REACT_FOR_MEETINGS_NOTIFICATION_MESSAGE_ID:
34+
# reaction.member happens to be always `None`, so we use the
35+
# `user_id` instead
2536

26-
if reaction.user_id == self.user.id:
27-
return
37+
if reaction.guild_id is None:
38+
print("Guild is None")
39+
return
40+
41+
guild = await self.fetch_guild(reaction.guild_id)
2842

43+
assert guild
44+
45+
member = await guild.fetch_member(reaction.user_id)
46+
47+
if member:
48+
await member.remove_roles(
49+
nextcord.Object(id=MEETING_WATCHERS_ROLE_ID),
50+
reason="Opted out of meeting notifications via reaction",
51+
)
52+
53+
async def on_add_checkmark_reaction(self, reaction: RawReactionActionEvent) -> None:
2954
message_id = reaction.message_id
30-
emoji = reaction.emoji
3155

32-
if emoji.name == "✅" and (
33-
notification := get_notification_for_discord_message_id(
34-
message_id, "core_devs"
56+
if message_id == REACT_FOR_MEETINGS_NOTIFICATION_MESSAGE_ID:
57+
if reaction.member is None:
58+
print("Member is None")
59+
return
60+
61+
await reaction.member.add_roles(
62+
nextcord.Object(id=MEETING_WATCHERS_ROLE_ID),
63+
reason="Opted in for meeting notifications via reaction",
3564
)
65+
66+
return
67+
68+
if meeting_notification := get_notification_for_discord_message_id(
69+
message_id, "core_devs"
3670
):
37-
event_date = notification.date.isoformat()
38-
public_notification = get_notification_for_date(event_date, "public")
71+
event_date = meeting_notification.date.isoformat()
3972

40-
if public_notification:
73+
if get_notification_for_date(event_date, "public") is not None:
4174
return
4275

4376
channel = client.get_channel(GENERAL_CHANNEL_ID)
4477

4578
assert isinstance(channel, nextcord.channel.TextChannel)
4679

4780
embed = nextcord.Embed(color=5814783)
48-
add_localized_times_to_embed(embed, notification.date)
81+
add_localized_times_to_embed(embed, meeting_notification.date)
4982

5083
message = await channel.send(
5184
"Hey @everyone 👋 the next monthly meeting will happen "
52-
f"{notification.date.humanize()} 📅\n"
85+
f"{meeting_notification.date.humanize()} 📅\n"
5386
f"Realtime notes will be posted here: {NOTES_LINK}.\n\n"
5487
"Feel free to add any topics you'd like to discuss in the meeting! 🍓",
5588
embed=embed,
5689
)
5790

5891
add_notification_for_date(event_date, message.id, "public")
5992

93+
async def on_raw_reaction_add(self, reaction: RawReactionActionEvent) -> None:
94+
assert self.user
95+
96+
if reaction.user_id == self.user.id:
97+
return
98+
99+
if reaction.emoji.name == "✅":
100+
await self.on_add_checkmark_reaction(reaction)
101+
102+
async def on_raw_reaction_remove(self, reaction: RawReactionActionEvent) -> None:
103+
assert self.user
104+
105+
if reaction.user_id == self.user.id:
106+
return
107+
108+
if reaction.emoji.name == "✅":
109+
await self.on_remove_checkmark_reaction(reaction)
110+
60111
async def check_events_for_next_week(self):
61112
await self.wait_until_ready()
62113
print("Bot is ready!")

src/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@
1010
DISCORD_TOKEN = os.environ["DISCORD_TOKEN"]
1111
CALENDAR_URL = os.environ["CALENDAR_URL"]
1212
NOTES_LINK = os.environ["NOTES_LINK"]
13+
REACT_FOR_MEETINGS_NOTIFICATION_MESSAGE_ID = int(
14+
os.environ["REACT_FOR_MEETINGS_NOTIFICATION_MESSAGE_ID"]
15+
)
16+
MEETING_WATCHERS_ROLE_ID = int(os.environ["MEETING_WATCHERS_ROLE_ID"])

src/database.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def add_notification_for_date(
6767

6868

6969
def get_notification_for_discord_message_id(
70-
discord_message_id: str, notification_type: str
70+
discord_message_id: int, notification_type: str
7171
) -> Notification | None:
7272
cursor.execute(
7373
"SELECT date, discord_message_id FROM meeting_notifications "

0 commit comments

Comments
 (0)