Skip to content

Commit 16c2826

Browse files
committed
Add support for creating a scheduled event
1 parent 49a78c1 commit 16c2826

File tree

2 files changed

+74
-14
lines changed

2 files changed

+74
-14
lines changed

src/database.py

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,32 @@
44

55
import arrow
66

7-
SCHEMA = """
8-
CREATE TABLE IF NOT EXISTS "meeting_notifications" (
9-
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
10-
"date" DATE,
11-
"discord_message_id" TEXT NOT NULL,
12-
"type" TEXT NOT NULL
13-
);
14-
"""
7+
TABLES = [
8+
"""
9+
CREATE TABLE IF NOT EXISTS "meeting_notifications" (
10+
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
11+
"date" DATE,
12+
"discord_message_id" TEXT NOT NULL,
13+
"type" TEXT NOT NULL
14+
);
15+
""",
16+
"""
17+
CREATE TABLE IF NOT EXISTS "scheduled_events" (
18+
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
19+
"date" DATE,
20+
"discord_event_id" TEXT NOT NULL
21+
);
22+
""",
23+
]
1524

1625
here = pathlib.Path(__file__).parent
1726
root = here.parent
1827

1928
connection = sqlite3.connect(root / "database.db")
2029
cursor = connection.cursor()
21-
cursor.execute(SCHEMA)
30+
31+
for table_creation_statement in TABLES:
32+
cursor.execute(table_creation_statement)
2233

2334

2435
class Notification(NamedTuple):
@@ -72,3 +83,26 @@ def get_notification_for_discord_message_id(
7283
return Notification(date=arrow.get(date), discord_message_id=int(message_id))
7384

7485
return None
86+
87+
88+
def get_scheduled_event_for_date(date: str) -> str | None:
89+
cursor.execute(
90+
"SELECT discord_event_id FROM scheduled_events " "WHERE date = ?",
91+
(date,),
92+
)
93+
94+
row = cursor.fetchone()
95+
96+
if row:
97+
return row[0]
98+
99+
return None
100+
101+
102+
def add_scheduled_event_for_date(date: str, discord_event_id: int) -> None:
103+
cursor.execute(
104+
"INSERT INTO scheduled_events " "(date, discord_event_id) VALUES (?, ?)",
105+
(date, discord_event_id),
106+
)
107+
108+
connection.commit()

src/meeting.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
11
import arrow
2+
import ics
23
import nextcord
4+
from nextcord import ScheduledEventEntityType
35

46
from .calendar import get_next_meeting
57
from .config import CORE_DEVS_CHANNEL_ID, NOTES_LINK
6-
from .database import add_notification_for_date, get_notification_for_date
8+
from .database import (
9+
add_notification_for_date,
10+
add_scheduled_event_for_date,
11+
get_notification_for_date,
12+
get_scheduled_event_for_date,
13+
)
714
from .date_utils import add_localized_times_to_embed
815

916

17+
async def add_schedule_event(next_meeting: ics.Event, guild: nextcord.Guild):
18+
scheduled_event = get_scheduled_event_for_date(next_meeting.begin.isoformat())
19+
20+
if not scheduled_event:
21+
event_channel = guild.get_channel(928750531906789480)
22+
assert isinstance(event_channel, nextcord.channel.VoiceChannel)
23+
24+
event = await guild.create_scheduled_event(
25+
name="Strawberry Monthly Meeting 🍓",
26+
channel=event_channel,
27+
start_time=next_meeting.begin.datetime,
28+
entity_type=ScheduledEventEntityType.voice,
29+
)
30+
31+
add_scheduled_event_for_date(next_meeting.begin.isoformat(), event.id)
32+
33+
1034
async def find_next_event_and_notify_core_team(client: nextcord.Client):
1135
next_meeting = get_next_meeting()
1236

@@ -21,12 +45,12 @@ async def find_next_event_and_notify_core_team(client: nextcord.Client):
2145
event_date = next_meeting.begin.isoformat()
2246
notification = get_notification_for_date(event_date, "core_devs")
2347

24-
if not notification:
25-
channel = client.get_channel(CORE_DEVS_CHANNEL_ID)
26-
27-
assert isinstance(channel, nextcord.channel.TextChannel)
48+
channel = client.get_channel(CORE_DEVS_CHANNEL_ID)
49+
assert isinstance(channel, nextcord.channel.TextChannel)
2850

51+
if not notification:
2952
embed = nextcord.Embed(color=5814783)
53+
3054
add_localized_times_to_embed(embed, next_meeting.begin)
3155

3256
message = await channel.send(
@@ -39,3 +63,5 @@ async def find_next_event_and_notify_core_team(client: nextcord.Client):
3963
await message.add_reaction("✅")
4064

4165
add_notification_for_date(event_date, message.id, "core_devs")
66+
67+
await add_schedule_event(next_meeting, channel.guild)

0 commit comments

Comments
 (0)