-
Notifications
You must be signed in to change notification settings - Fork 2
Added a door webhook handler and env config #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
cbrxyz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Also, could you please add some instructions on how you tested the change?
src/env.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you made ensure_int, you might as well do:
| GUILD_ID = ensure_int("GUILD_ID") | |
| EMOJI_GUILD_ID = ensure_int("EMOJI_GUILD_ID") |
| channel = self.bot.get_channel(LAB_DOOR_STATUS_CHANNEL_ID) | ||
| if channel is None: | ||
| channel = await self.bot.fetch_channel(LAB_DOOR_STATUS_CHANNEL_ID) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The bot does not use explicit channel IDs for getting a discord.Channel resource because a channel ID refers to a specific channel in a specific guild. This means unless the bot instance running this code is in the specific guild in which this channel ID can be found, this code will not work.
Instead, create a class variable inside the bot class, and add your channel into RolesCog._load_channels(). No matter which server the bot is currently in (whether the MIL server, a test server, etc.), it should be able to load the correct channel.
Lines 198 to 215 in 2370907
| def _load_channels(self): | |
| text_channels = { | |
| "leave_channel": "leave-log", | |
| "general_channel": "general", | |
| "member_services_channel": "member-services", | |
| "alumni_channel": "alumni", | |
| "leaders_channel": "leads", | |
| "operations_leaders_channel": "operations-leadership", | |
| "software_leaders_channel": "software-leadership", | |
| "electrical_leaders_channel": "electrical-leadership", | |
| "mechanical_leaders_channel": "mechanical-leadership", | |
| "message_log_channel": "message-log", | |
| "errors_channel": "bot-errors", | |
| "software_projects_channel": "software-projects", | |
| } | |
| for attr, name in text_channels.items(): | |
| setattr(self.bot, attr, self._get_text_channel(name)) |
| channel = self.bot.get_channel(LAB_DOOR_STATUS_CHANNEL_ID) | |
| if channel is None: | |
| channel = await self.bot.fetch_channel(LAB_DOOR_STATUS_CHANNEL_ID) | |
| channel = self.bot.lab_door_status_channel |
src/webhooks.py
Outdated
| elif raw == "maybe": | ||
| text = "🤔-lab-maybe-open" | ||
| else: | ||
| text = f"Lab status: {raw}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Channel names are cut off after ~25 characters, meaning it could be hard to read whatever {raw} is here. I would throw an exception here, since this behavior is unexpected and you can be notified in #bot-log when the door sensor is returning an invalid value.
(unless you think it could be sending an invalid value regularly, in which case I would just combine this branch with the one before it to represent that the lab is possibly open)
src/webhooks.py
Outdated
|
|
||
|
|
||
| @dataclass | ||
| class DoorWebHookResponse: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| class DoorWebHookResponse: | |
| class DoorWebhookResponse: |
src/webhooks.py
Outdated
| async def ignore(self) -> bool: | ||
| if LAB_DOOR_STATUS_CHANNEL_ID is None: | ||
| logger.warning("LAB_DOOR_STATUS_CHANNEL_ID not set, skipping door update") | ||
| return True | ||
| return False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| async def ignore(self) -> bool: | |
| if LAB_DOOR_STATUS_CHANNEL_ID is None: | |
| logger.warning("LAB_DOOR_STATUS_CHANNEL_ID not set, skipping door update") | |
| return True | |
| return False |
src/webhooks.py
Outdated
| async def handle(self) -> None: | ||
| raw = str(self.payload.get("door_status", "")).strip().lower() | ||
| if not raw: | ||
| logger.warning("door_status missing in payload!") | ||
| return | ||
|
|
||
| if raw == "open": | ||
| text = "✅-lab-open" | ||
| elif raw == "closed": | ||
| text = "❌-lab-closed" | ||
| elif raw == "maybe": | ||
| text = "🤔-lab-maybe-open" | ||
| else: | ||
| text = f"Lab status: {raw}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| async def handle(self) -> None: | |
| raw = str(self.payload.get("door_status", "")).strip().lower() | |
| if not raw: | |
| logger.warning("door_status missing in payload!") | |
| return | |
| if raw == "open": | |
| text = "✅-lab-open" | |
| elif raw == "closed": | |
| text = "❌-lab-closed" | |
| elif raw == "maybe": | |
| text = "🤔-lab-maybe-open" | |
| else: | |
| text = f"Lab status: {raw}" | |
| async def door_status(self) -> str | None: | |
| return str(self.payload.get("door_status", "")).strip().lower() or None | |
| async def handle(self) -> None: | |
| door_status = await self.door_status() | |
| if not door_status: | |
| logger.warning("door_status missing in payload!") | |
| return | |
| channel_names = { | |
| "open": "✅-lab-open", | |
| "closed": "❌-lab-closed", | |
| "maybe": "🤔-lab-maybe-open", | |
| } | |
| text = channel_names.get(door_status, "❓-lab-status-unknown") |
|
|
||
|
|
||
| @dataclass | ||
| class WebhookResponse: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| class WebhookResponse: | |
| class GitHubWebhookResponse: |
Adds a dedicated door webhook flow.
Adds DoorWebHookResponse base class & DoorToggled handler