aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--bot/exts/help_channels/_channel.py22
-rw-r--r--bot/exts/help_channels/_cog.py22
-rw-r--r--bot/exts/help_channels/_stats.py8
3 files changed, 32 insertions, 20 deletions
diff --git a/bot/exts/help_channels/_channel.py b/bot/exts/help_channels/_channel.py
index 8af059830..b8db337fc 100644
--- a/bot/exts/help_channels/_channel.py
+++ b/bot/exts/help_channels/_channel.py
@@ -1,6 +1,7 @@
import logging
import typing as t
from datetime import timedelta
+from enum import Enum
import arrow
import discord
@@ -17,6 +18,17 @@ MAX_CHANNELS_PER_CATEGORY = 50
EXCLUDED_CHANNELS = (constants.Channels.cooldown,)
+class ClosingReason(Enum):
+ """All possible closing reasons for help channels."""
+
+ COMMAND = "command"
+ LATEST_MESSSAGE = "auto.latest_message"
+ CLAIMANT_TIMEOUT = "auto.claimant_timeout"
+ OTHER_TIMEOUT = "auto.other_timeout"
+ DELETED = "auto.deleted"
+ CLEANUP = "auto.deleted"
+
+
def get_category_channels(category: discord.CategoryChannel) -> t.Iterable[discord.TextChannel]:
"""Yield the text channels of the `category` in an unsorted manner."""
log.trace(f"Getting text channels in the category '{category}' ({category.id}).")
@@ -27,7 +39,7 @@ def get_category_channels(category: discord.CategoryChannel) -> t.Iterable[disco
yield channel
-async def get_closing_time(channel: discord.TextChannel, init_done: bool) -> t.Tuple[Arrow, str]:
+async def get_closing_time(channel: discord.TextChannel, init_done: bool) -> t.Tuple[Arrow, ClosingReason]:
"""
Return the time at which the given help `channel` should be closed along with the reason.
@@ -59,11 +71,11 @@ async def get_closing_time(channel: discord.TextChannel, init_done: bool) -> t.T
msg = await _message.get_last_message(channel)
if not msg:
log.debug(f"No idle time available; #{channel} ({channel.id}) has no messages, closing now.")
- return Arrow.min, "deleted"
+ return Arrow.min, ClosingReason.DELETED
# Use the greatest offset to avoid the possibility of prematurely closing the channel.
time = Arrow.fromdatetime(msg.created_at) + timedelta(minutes=idle_minutes_claimant)
- return time, "latest_message"
+ return time, ClosingReason.LATEST_MESSSAGE
claimant_time = Arrow.utcfromtimestamp(claimant_time)
others_time = await _caches.non_claimant_last_message_times.get(channel.id)
@@ -82,10 +94,10 @@ async def get_closing_time(channel: discord.TextChannel, init_done: bool) -> t.T
# Use the time which is the furthest into the future.
if claimant_time >= others_time:
closing_time = claimant_time
- reason = "claimant_timeout"
+ reason = ClosingReason.CLAIMANT_TIMEOUT
else:
closing_time = others_time
- reason = "others_timeout"
+ reason = ClosingReason.OTHER_TIMEOUT
log.trace(f"#{channel} ({channel.id}) should be closed at {closing_time} due to {reason}.")
return closing_time, reason
diff --git a/bot/exts/help_channels/_cog.py b/bot/exts/help_channels/_cog.py
index 658e50201..18457f6a5 100644
--- a/bot/exts/help_channels/_cog.py
+++ b/bot/exts/help_channels/_cog.py
@@ -192,7 +192,7 @@ class HelpChannels(commands.Cog):
# Don't use a discord.py check because the check needs to fail silently.
if await self.close_check(ctx):
log.info(f"Close command invoked by {ctx.author} in #{ctx.channel}.")
- await self.unclaim_channel(ctx.channel, closed_on="command")
+ await self.unclaim_channel(ctx.channel, closed_on=_channel.ClosingReason.COMMAND)
async def get_available_candidate(self) -> discord.TextChannel:
"""
@@ -238,7 +238,7 @@ class HelpChannels(commands.Cog):
elif missing < 0:
log.trace(f"Moving {abs(missing)} superfluous available channels over to the Dormant category.")
for channel in channels[:abs(missing)]:
- await self.unclaim_channel(channel, closed_on="cleanup")
+ await self.unclaim_channel(channel, closed_on=_channel.ClosingReason.CLEANUP)
async def init_categories(self) -> None:
"""Get the help category objects. Remove the cog if retrieval fails."""
@@ -305,7 +305,7 @@ class HelpChannels(commands.Cog):
if closing_time < (arrow.utcnow() + timedelta(seconds=1)):
log.info(
f"#{channel} ({channel.id}) is idle past {closing_time} "
- f"and will be made dormant. Reason: {closed_on}"
+ f"and will be made dormant. Reason: {closed_on.value}"
)
await self.unclaim_channel(channel, closed_on=closed_on)
@@ -358,7 +358,7 @@ class HelpChannels(commands.Cog):
_stats.report_counts()
@lock.lock_arg(f"{NAMESPACE}.unclaim", "channel")
- async def unclaim_channel(self, channel: discord.TextChannel, *, closed_on: str) -> None:
+ async def unclaim_channel(self, channel: discord.TextChannel, *, closed_on: _channel.ClosingReason) -> None:
"""
Unclaim an in-use help `channel` to make it dormant.
@@ -366,10 +366,7 @@ class HelpChannels(commands.Cog):
Remove the cooldown role from the channel claimant if they have no other channels claimed.
Cancel the scheduled cooldown role removal task.
- `closed_on` is the reason that the channel was closed for. Possible values for this are:
- "cleanup", "command", "claimant_timeout", "others_timeout", "deleted".
- All values, except for "command", get prefixed with "auto." within `_stats.report_complete_session()`
- before being added to the bot's stats.
+ `closed_on` is the reason that the channel was closed. See _channel.ClosingReason for possible values.
"""
claimant_id = await _caches.claimants.get(channel.id)
_unclaim_channel = self._unclaim_channel
@@ -382,7 +379,12 @@ class HelpChannels(commands.Cog):
return await _unclaim_channel(channel, claimant_id, closed_on)
- async def _unclaim_channel(self, channel: discord.TextChannel, claimant_id: int, closed_on: str) -> None:
+ async def _unclaim_channel(
+ self,
+ channel: discord.TextChannel,
+ claimant_id: int,
+ closed_on: _channel.ClosingReason
+ ) -> None:
"""Actual implementation of `unclaim_channel`. See that for full documentation."""
await _caches.claimants.delete(channel.id)
@@ -403,7 +405,7 @@ class HelpChannels(commands.Cog):
# Cancel the task that makes the channel dormant only if called by the close command.
# In other cases, the task is either already done or not-existent.
- if closed_on == "command":
+ if closed_on == _channel.ClosingReason.COMMAND:
self.scheduler.cancel(channel.id)
async def move_to_in_use(self, channel: discord.TextChannel) -> None:
diff --git a/bot/exts/help_channels/_stats.py b/bot/exts/help_channels/_stats.py
index 123604945..eb34e75e1 100644
--- a/bot/exts/help_channels/_stats.py
+++ b/bot/exts/help_channels/_stats.py
@@ -22,15 +22,13 @@ def report_counts() -> None:
log.warning(f"Couldn't find category {name!r} to track channel count stats.")
-async def report_complete_session(channel_id: int, closed_on: str) -> None:
+async def report_complete_session(channel_id: int, closed_on: _channel.ClosingReason) -> None:
"""
Report stats for a completed help session channel `channel_id`.
- Set `is_auto` to True if the channel was automatically closed or False if manually closed.
+ `closed_on` is the reason why the channel was closed. See `_channel.ClosingReason` for possible reasons.
"""
- if closed_on != "command":
- closed_on = f"auto.{closed_on}"
- bot.instance.stats.incr(f"help.dormant_calls.{closed_on}")
+ bot.instance.stats.incr(f"help.dormant_calls.{closed_on.value}")
in_use_time = await _channel.get_in_use_time(channel_id)
if in_use_time: