diff options
| author | 2020-10-18 18:56:08 -0700 | |
|---|---|---|
| committer | 2020-11-20 15:57:08 -0800 | |
| commit | 44fe885de67135dd16a44539fc97d8e7fc543400 (patch) | |
| tree | 1f9133efab24dee5f521aace6fd5f74b7f0af72e | |
| parent | Help channels: move message functions to message module (diff) | |
Help channels: move time functions to channel module
| -rw-r--r-- | bot/exts/help_channels/_channel.py | 36 | ||||
| -rw-r--r-- | bot/exts/help_channels/_cog.py | 34 |
2 files changed, 39 insertions, 31 deletions
diff --git a/bot/exts/help_channels/_channel.py b/bot/exts/help_channels/_channel.py index 047f41e89..93c0c7fc9 100644 --- a/bot/exts/help_channels/_channel.py +++ b/bot/exts/help_channels/_channel.py @@ -1,15 +1,22 @@ import logging import typing as t +from datetime import datetime, timedelta import discord +from async_rediscache import RedisCache from bot import constants +from bot.exts.help_channels import _message log = logging.getLogger(__name__) MAX_CHANNELS_PER_CATEGORY = 50 EXCLUDED_CHANNELS = (constants.Channels.how_to_get_help, constants.Channels.cooldown) +# This dictionary maps a help channel to the time it was claimed +# RedisCache[discord.TextChannel.id, UtcPosixTimestamp] +_claim_times = RedisCache(namespace="HelpChannels.claim_times") + def get_category_channels(category: discord.CategoryChannel) -> t.Iterable[discord.TextChannel]: """Yield the text channels of the `category` in an unsorted manner.""" @@ -21,6 +28,35 @@ def get_category_channels(category: discord.CategoryChannel) -> t.Iterable[disco yield channel +async def get_idle_time(channel: discord.TextChannel) -> t.Optional[int]: + """ + Return the time elapsed, in seconds, since the last message sent in the `channel`. + + Return None if the channel has no messages. + """ + log.trace(f"Getting the idle time for #{channel} ({channel.id}).") + + msg = await _message.get_last_message(channel) + if not msg: + log.debug(f"No idle time available; #{channel} ({channel.id}) has no messages.") + return None + + idle_time = (datetime.utcnow() - msg.created_at).seconds + + log.trace(f"#{channel} ({channel.id}) has been idle for {idle_time} seconds.") + return idle_time + + +async def get_in_use_time(channel_id: int) -> t.Optional[timedelta]: + """Return the duration `channel_id` has been in use. Return None if it's not in use.""" + log.trace(f"Calculating in use time for channel {channel_id}.") + + claimed_timestamp = await _claim_times.get(channel_id) + if claimed_timestamp: + claimed = datetime.utcfromtimestamp(claimed_timestamp) + return datetime.utcnow() - claimed + + def is_excluded_channel(channel: discord.abc.GuildChannel) -> bool: """Check if a channel should be excluded from the help channel system.""" return not isinstance(channel, discord.TextChannel) or channel.id in EXCLUDED_CHANNELS diff --git a/bot/exts/help_channels/_cog.py b/bot/exts/help_channels/_cog.py index 174c40096..390528fde 100644 --- a/bot/exts/help_channels/_cog.py +++ b/bot/exts/help_channels/_cog.py @@ -2,7 +2,7 @@ import asyncio import logging import random import typing as t -from datetime import datetime, timedelta, timezone +from datetime import datetime, timezone import discord import discord.abc @@ -201,34 +201,6 @@ class HelpChannels(commands.Cog): return channel - async def get_in_use_time(self, channel_id: int) -> t.Optional[timedelta]: - """Return the duration `channel_id` has been in use. Return None if it's not in use.""" - log.trace(f"Calculating in use time for channel {channel_id}.") - - claimed_timestamp = await self.claim_times.get(channel_id) - if claimed_timestamp: - claimed = datetime.utcfromtimestamp(claimed_timestamp) - return datetime.utcnow() - claimed - - @staticmethod - async def get_idle_time(channel: discord.TextChannel) -> t.Optional[int]: - """ - Return the time elapsed, in seconds, since the last message sent in the `channel`. - - Return None if the channel has no messages. - """ - log.trace(f"Getting the idle time for #{channel} ({channel.id}).") - - msg = await _message.get_last_message(channel) - if not msg: - log.debug(f"No idle time available; #{channel} ({channel.id}) has no messages.") - return None - - idle_time = (datetime.utcnow() - msg.created_at).seconds - - log.trace(f"#{channel} ({channel.id}) has been idle for {idle_time} seconds.") - return idle_time - async def init_available(self) -> None: """Initialise the Available category with channels.""" log.trace("Initialising the Available category with channels.") @@ -329,7 +301,7 @@ class HelpChannels(commands.Cog): else: idle_seconds = constants.HelpChannels.deleted_idle_minutes * 60 - time_elapsed = await self.get_idle_time(channel) + time_elapsed = await _channel.get_idle_time(channel) if time_elapsed is None or time_elapsed >= idle_seconds: log.info( @@ -424,7 +396,7 @@ class HelpChannels(commands.Cog): self.bot.stats.incr(f"help.dormant_calls.{caller}") - in_use_time = await self.get_in_use_time(channel.id) + in_use_time = await _channel.get_in_use_time(channel.id) if in_use_time: self.bot.stats.timing("help.in_use_time", in_use_time) |