|
1 | 1 | import logging |
2 | 2 |
|
3 | | -from discord import Client, Embed |
| 3 | +from discord import Client, Embed, TextChannel |
4 | 4 | from discord.ext import commands, tasks |
5 | 5 |
|
6 | 6 | from europython_discord.configuration import Config |
@@ -73,15 +73,27 @@ async def fetch_livestreams(self) -> None: |
73 | 73 |
|
74 | 74 | async def set_room_topic(self, room: str, topic: str) -> None: |
75 | 75 | """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) |
79 | 79 |
|
80 | 80 | async def notify_room(self, room: str, embed: Embed, content: str | None = None) -> None: |
81 | 81 | """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)) |
85 | 97 |
|
86 | 98 | @tasks.loop() |
87 | 99 | async def notify_sessions(self) -> None: |
|
0 commit comments