Skip to content

Commit 6343ecb

Browse files
committed
Hotfix: Handle unconfigured rooms
1 parent 503f0e9 commit 6343ecb

1 file changed

Lines changed: 19 additions & 7 deletions

File tree

  • src/europython_discord/program_notifications

src/europython_discord/program_notifications/cog.py

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

3-
from discord import Client, Embed
3+
from discord import Client, Embed, TextChannel
44
from discord.ext import commands, tasks
55

66
from europython_discord.configuration import Config
@@ -73,15 +73,27 @@ async def fetch_livestreams(self) -> None:
7373

7474
async def set_room_topic(self, room: str, topic: str) -> None:
7575
"""Set the topic of a room channel."""
76-
channel_id = config.PROGRAM_CHANNELS[room.lower().replace(" ", "_")]["channel_id"]
77-
channel = self.bot.get_channel(int(channel_id))
78-
await channel.edit(topic=topic)
76+
room_channel = self._get_channel(room)
77+
if room_channel is not None:
78+
await room_channel.edit(topic=topic)
7979

8080
async def notify_room(self, room: str, embed: Embed, content: str | None = None) -> None:
8181
"""Send the given notification to the room channel."""
82-
channel_id = config.PROGRAM_CHANNELS[room.lower().replace(" ", "_")]["channel_id"]
83-
channel = self.bot.get_channel(int(channel_id))
84-
await channel.send(content=content, embed=embed)
82+
room_channel = self._get_channel(room)
83+
if room_channel is not None:
84+
await room_channel.send(content=content, embed=embed)
85+
86+
def _get_channel(self, room: str) -> TextChannel | None:
87+
room_key = room.lower().replace(" ", "_")
88+
room_channel_config = config.PROGRAM_CHANNELS.get(room_key)
89+
90+
if room_channel_config is None:
91+
# this may be intended: in 2024, there were no dedicated channels for the tutorial rooms
92+
_logger.warning(f"Cannot find configuration for room {room!r} (key: {room_key!r})")
93+
return None
94+
95+
channel_id = room_channel_config["channel_id"]
96+
return self.bot.get_channel(int(channel_id))
8597

8698
@tasks.loop()
8799
async def notify_sessions(self) -> None:

0 commit comments

Comments
 (0)