diff options
author | 2020-04-20 14:16:05 -0700 | |
---|---|---|
committer | 2020-04-20 14:16:05 -0700 | |
commit | 8a3a8aab30d6d10553e99141d8b6b82e54284dd5 (patch) | |
tree | a4fc0ee6ddaefea9df8a87cecd16d0d7346f8a66 | |
parent | Merge pull request #875 from python-discord/bug/info/869/tag-encoding (diff) | |
parent | HelpChannels: add logging to answered check (diff) |
Merge pull request #896 from python-discord/answered-session-stats
Answered help session statistics
-rw-r--r-- | bot/cogs/help_channels.py | 45 |
1 files changed, 42 insertions, 3 deletions
diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index e73bbdae5..9d7328739 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -62,7 +62,8 @@ through our guide for [asking a good question]({ASKING_GUIDE_URL}). """ AVAILABLE_EMOJI = "✅" -IN_USE_EMOJI = "⌛" +IN_USE_ANSWERED_EMOJI = "⌛" +IN_USE_UNANSWERED_EMOJI = "⏳" NAME_SEPARATOR = "|" @@ -132,7 +133,14 @@ class HelpChannels(Scheduler, commands.Cog): self.init_task = self.bot.loop.create_task(self.init_cog()) # Stats - self.claim_times = {} + + # This dictionary maps a help channel to the time it was claimed + self.claim_times: t.Dict[int, datetime] = {} + + # This dictionary maps a help channel to whether it has had any + # activity other than the original claimant. True being no other + # activity and False being other activity. + self.unanswered: t.Dict[int, bool] = {} def cog_unload(self) -> None: """Cancel the init task and scheduled tasks when the cog unloads.""" @@ -506,6 +514,12 @@ class HelpChannels(Scheduler, commands.Cog): in_use_time = datetime.now() - claimed self.bot.stats.timing("help.in_use_time", in_use_time) + if channel.id in self.unanswered: + if self.unanswered[channel.id]: + self.bot.stats.incr("help.sessions.unanswered") + else: + self.bot.stats.incr("help.sessions.answered") + log.trace(f"Position of #{channel} ({channel.id}) is actually {channel.position}.") log.trace(f"Sending dormant message for #{channel} ({channel.id}).") @@ -521,7 +535,7 @@ class HelpChannels(Scheduler, commands.Cog): log.info(f"Moving #{channel} ({channel.id}) to the In Use category.") await channel.edit( - name=f"{IN_USE_EMOJI}{NAME_SEPARATOR}{self.get_clean_channel_name(channel)}", + name=f"{IN_USE_UNANSWERED_EMOJI}{NAME_SEPARATOR}{self.get_clean_channel_name(channel)}", category=self.in_use_category, sync_permissions=True, topic=IN_USE_TOPIC, @@ -580,6 +594,27 @@ class HelpChannels(Scheduler, commands.Cog): # 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!") + async def check_for_answer(self, message: discord.Message) -> None: + """Checks for whether new content in a help channel comes from non-claimants.""" + channel = message.channel + log.trace(f"Checking if #{channel} ({channel.id}) has been answered.") + + # Confirm the channel is an in use help channel + if self.is_in_category(channel, constants.Categories.help_in_use): + # Check if there is an entry in unanswered (does not persist across restarts) + if channel.id in self.unanswered: + claimant_id = self.help_channel_claimants[channel].id + + # Check the message did not come from the claimant + if claimant_id != message.author.id: + # Mark the channel as answered + self.unanswered[channel.id] = False + + # Change the emoji in the channel name to signify activity + log.trace(f"#{channel} ({channel.id}) has been answered; changing its emoji") + name = self.get_clean_channel_name(channel) + await channel.edit(name=f"{IN_USE_ANSWERED_EMOJI}{NAME_SEPARATOR}{name}") + @commands.Cog.listener() async def on_message(self, message: discord.Message) -> None: """Move an available channel to the In Use category and replace it with a dormant one.""" @@ -587,6 +622,9 @@ class HelpChannels(Scheduler, commands.Cog): return # Ignore messages sent by bots. channel = message.channel + + await self.check_for_answer(message) + if not self.is_in_category(channel, constants.Categories.help_available): return # Ignore messages outside the Available category. @@ -612,6 +650,7 @@ class HelpChannels(Scheduler, commands.Cog): self.bot.stats.incr("help.claimed") self.claim_times[channel.id] = datetime.now() + self.unanswered[channel.id] = True log.trace(f"Releasing on_message lock for {message.id}.") |