diff options
author | 2022-02-03 19:51:52 +0000 | |
---|---|---|
committer | 2022-02-03 19:51:52 +0000 | |
commit | 1a53b6b7ea9203e348f4b1b1678c30b140e16544 (patch) | |
tree | 995aa4748be4d1472eead988f5228729dafab715 | |
parent | 💡 Comment usage of arbitrarily old date (diff) |
♻️Move notifications into `_message.py` with predicate
-rw-r--r-- | bot/exts/help_channels/_cog.py | 33 | ||||
-rw-r--r-- | bot/exts/help_channels/_message.py | 52 |
2 files changed, 55 insertions, 30 deletions
diff --git a/bot/exts/help_channels/_cog.py b/bot/exts/help_channels/_cog.py index 8c93b084d..aefa0718e 100644 --- a/bot/exts/help_channels/_cog.py +++ b/bot/exts/help_channels/_cog.py @@ -79,7 +79,6 @@ class HelpChannels(commands.Cog): self.name_queue: t.Deque[str] = None # Notifications - self.notify_interval_seconds = constants.HelpChannels.notify_minutes * 60 # Using a very old date so that we don't have to use Optional typing. self.last_none_remaining_notification = arrow.get('1815-12-10T18:00:00.00000+00:00') self.last_running_low_notification = arrow.get('1815-12-10T18:00:00.00000+00:00') @@ -233,29 +232,31 @@ class HelpChannels(commands.Cog): try: channel = self.channel_queue.get_nowait() - - time_since_last_notify_seconds = (arrow.utcnow() - self.last_running_low_notification).seconds - within_interval = time_since_last_notify_seconds >= self.notify_interval_seconds - if within_interval and self.channel_queue.qsize() <= constants.HelpChannels.notify_running_low_threshold: - await _message.notify_running_low( - self.bot.get_channel(constants.HelpChannels.notify_channel), - self.channel_queue.qsize() - ) - self.last_running_low_notification = arrow.utcnow() - except asyncio.QueueEmpty: log.info("No candidate channels in the queue; creating a new channel.") channel = await self.create_dormant() if not channel: log.info("Couldn't create a candidate channel; waiting to get one from the queue.") + last_notification = await _message.notify_none_remaining( + self.bot.get_channel(constants.HelpChannels.notify_channel), + self.last_none_remaining_notification + ) + + if last_notification: + self.last_none_remaining_notification = last_notification + + channel = await self.wait_for_dormant_channel() # Blocks until a new channel is available - if (arrow.utcnow() - self.last_none_remaining_notification).seconds >= self.notify_interval_seconds: - await _message.notify_none_remaining(self.bot.get_channel(constants.HelpChannels.notify_channel)) - self.last_none_remaining_notification = arrow.utcnow() - self.bot.stats.incr("help.out_of_channel_alerts") + else: + last_notification = await _message.notify_running_low( + self.bot.get_channel(constants.HelpChannels.notify_channel), + self.channel_queue.qsize(), + self.last_running_low_notification + ) - channel = await self.wait_for_dormant_channel() + if last_notification: + self.last_running_low_notification = last_notification return channel diff --git a/bot/exts/help_channels/_message.py b/bot/exts/help_channels/_message.py index 7d70c9f00..097e648e0 100644 --- a/bot/exts/help_channels/_message.py +++ b/bot/exts/help_channels/_message.py @@ -1,6 +1,7 @@ import textwrap import typing as t +import arrow import discord from arrow import Arrow @@ -123,7 +124,7 @@ async def dm_on_open(message: discord.Message) -> None: ) -async def notify_none_remaining(channel: discord.TextChannel) -> None: +async def notify_none_remaining(channel: discord.TextChannel, last_notification: Arrow) -> t.Optional[Arrow]: """ Send a pinging message in `channel` notifying about there being no dormant channels remaining. @@ -133,16 +134,18 @@ async def notify_none_remaining(channel: discord.TextChannel) -> None: * `HelpChannels.notify_none_remaining_roles` - roles mentioned in notifications """ if not constants.HelpChannels.notify_none_remaining: - return + return None - log.trace("Notifying about lack of channels.") + if (arrow.utcnow() - last_notification).seconds < (constants.HelpChannels.notify_minutes * 60): + log.trace("Did not send none_remaining notification as it hasn't been enough time since the last one.") + return None - try: - log.trace("Sending notification message.") + log.trace("Notifying about lack of channels.") - mentions = " ".join(f"<@&{role}>" for role in constants.HelpChannels.notify_none_remaining_roles) - allowed_roles = [discord.Object(id_) for id_ in constants.HelpChannels.notify_none_remaining_roles] + mentions = " ".join(f"<@&{role}>" for role in constants.HelpChannels.notify_none_remaining_roles) + allowed_roles = [discord.Object(id_) for id_ in constants.HelpChannels.notify_none_remaining_roles] + try: await channel.send( f"{mentions} A new available help channel is needed but there " "are no more dormant ones. Consider freeing up some in-use channels manually by " @@ -152,9 +155,16 @@ async def notify_none_remaining(channel: discord.TextChannel) -> None: except Exception: # Handle it here cause this feature isn't critical for the functionality of the system. log.exception("Failed to send notification about lack of dormant channels!") + finally: + bot.instance.stats.incr("help.out_of_channel_alerts") + return arrow.utcnow() -async def notify_running_low(channel: discord.TextChannel, number_of_channels_left: int) -> None: +async def notify_running_low( + channel: discord.TextChannel, + number_of_channels_left: int, + last_notification: Arrow +) -> t.Optional[Arrow]: """ Send a non-pinging message in `channel` notifying about there being a low amount of dormant channels. @@ -166,14 +176,28 @@ async def notify_running_low(channel: discord.TextChannel, number_of_channels_le * `HelpChannels.notify_running_low_threshold` - minimum amount of channels to trigger running_low notifications """ if not constants.HelpChannels.notify_running_low: - return + return None - log.trace("Notifying about getting close to no dormant channels.") + if number_of_channels_left > constants.HelpChannels.notify_running_low_threshold: + log.trace("Did not send notify_running_low notification as the threshold was not met.") + return None - await channel.send( - f"There are only {number_of_channels_left} dormant channels left. " - "Consider participating in some help channels so that we don't run out." - ) + if (arrow.utcnow() - last_notification).seconds < (constants.HelpChannels.notify_minutes * 60): + log.trace("Did not send notify_running_low notification as it hasn't been enough time since the last one.") + return None + + log.trace("Notifying about getting close to no dormant channels.") + try: + await channel.send( + f"There are only {number_of_channels_left} dormant channels left. " + "Consider participating in some help channels so that we don't run out." + ) + except Exception: + # Handle it here cause this feature isn't critical for the functionality of the system. + log.exception("Failed to send notification about running low of dormant channels!") + finally: + bot.instance.stats.incr("help.running_low_alerts") + return arrow.utcnow() async def pin(message: discord.Message) -> None: |