aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar MarkKoz <[email protected]>2020-10-20 12:31:40 -0700
committerGravatar MarkKoz <[email protected]>2020-11-20 15:57:09 -0800
commit21f6a9a5f799c88f746b2bbda250ec1a6bb8f845 (patch)
treeebd98f6b379ceb44e9c305c33e4cc26d30e41005
parentHelp channels: wait for cog to be ready in deleted msg listener (diff)
Help channels: move all caches to a separate module
Some need to be shared among modules, so it became redundant to redefine them in each module.
-rw-r--r--bot/exts/help_channels/_caches.py19
-rw-r--r--bot/exts/help_channels/_channel.py9
-rw-r--r--bot/exts/help_channels/_cog.py23
-rw-r--r--bot/exts/help_channels/_cooldown.py9
-rw-r--r--bot/exts/help_channels/_message.py26
5 files changed, 36 insertions, 50 deletions
diff --git a/bot/exts/help_channels/_caches.py b/bot/exts/help_channels/_caches.py
new file mode 100644
index 000000000..4cea385b7
--- /dev/null
+++ b/bot/exts/help_channels/_caches.py
@@ -0,0 +1,19 @@
+from async_rediscache import RedisCache
+
+# This dictionary maps a help channel to the time it was claimed
+# RedisCache[discord.TextChannel.id, UtcPosixTimestamp]
+claim_times = RedisCache(namespace="HelpChannels.claim_times")
+
+# This cache tracks which channels are claimed by which members.
+# RedisCache[discord.TextChannel.id, t.Union[discord.User.id, discord.Member.id]]
+claimants = RedisCache(namespace="HelpChannels.help_channel_claimants")
+
+# This cache maps a help channel to original question message in same channel.
+# RedisCache[discord.TextChannel.id, discord.Message.id]
+question_messages = RedisCache(namespace="HelpChannels.question_messages")
+
+# This cache 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.
+# RedisCache[discord.TextChannel.id, bool]
+unanswered = RedisCache(namespace="HelpChannels.unanswered")
diff --git a/bot/exts/help_channels/_channel.py b/bot/exts/help_channels/_channel.py
index 93c0c7fc9..d6d6f1245 100644
--- a/bot/exts/help_channels/_channel.py
+++ b/bot/exts/help_channels/_channel.py
@@ -3,20 +3,15 @@ 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
+from bot.exts.help_channels import _caches, _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."""
@@ -51,7 +46,7 @@ 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)
+ claimed_timestamp = await _caches.claim_times.get(channel_id)
if claimed_timestamp:
claimed = datetime.utcfromtimestamp(claimed_timestamp)
return datetime.utcnow() - claimed
diff --git a/bot/exts/help_channels/_cog.py b/bot/exts/help_channels/_cog.py
index 29570bab3..a17213323 100644
--- a/bot/exts/help_channels/_cog.py
+++ b/bot/exts/help_channels/_cog.py
@@ -6,12 +6,11 @@ from datetime import datetime, timezone
import discord
import discord.abc
-from async_rediscache import RedisCache
from discord.ext import commands
from bot import constants
from bot.bot import Bot
-from bot.exts.help_channels import _channel, _cooldown, _message
+from bot.exts.help_channels import _caches, _channel, _cooldown, _message
from bot.exts.help_channels._name import create_name_queue
from bot.utils import channel as channel_utils
from bot.utils.scheduling import Scheduler
@@ -58,14 +57,6 @@ class HelpChannels(commands.Cog):
Help channels are named after the chemical elements in `bot/resources/elements.json`.
"""
- # This cache tracks which channels are claimed by which members.
- # RedisCache[discord.TextChannel.id, t.Union[discord.User.id, discord.Member.id]]
- help_channel_claimants = RedisCache()
-
- # This dictionary maps a help channel to the time it was claimed
- # RedisCache[discord.TextChannel.id, UtcPosixTimestamp]
- claim_times = RedisCache()
-
def __init__(self, bot: Bot):
self.bot = bot
self.scheduler = Scheduler(self.__class__.__name__)
@@ -136,7 +127,7 @@ class HelpChannels(commands.Cog):
async def dormant_check(self, ctx: commands.Context) -> bool:
"""Return True if the user is the help channel claimant or passes the role check."""
- if await self.help_channel_claimants.get(ctx.channel.id) == ctx.author.id:
+ if await _caches.claimants.get(ctx.channel.id) == ctx.author.id:
log.trace(f"{ctx.author} is the help channel claimant, passing the check for dormant.")
self.bot.stats.incr("help.dormant_invoke.claimant")
return True
@@ -378,7 +369,7 @@ class HelpChannels(commands.Cog):
"""
log.info(f"Moving #{channel} ({channel.id}) to the Dormant category.")
- await self.help_channel_claimants.delete(channel.id)
+ await _caches.claimants.delete(channel.id)
await self.move_to_bottom_position(
channel=channel,
category_id=constants.Categories.help_dormant,
@@ -390,7 +381,7 @@ class HelpChannels(commands.Cog):
if in_use_time:
self.bot.stats.timing("help.in_use_time", in_use_time)
- unanswered = await _message.unanswered.get(channel.id)
+ unanswered = await _caches.unanswered.get(channel.id)
if unanswered:
self.bot.stats.incr("help.sessions.unanswered")
elif unanswered is not None:
@@ -458,15 +449,15 @@ class HelpChannels(commands.Cog):
await _message.pin(message)
# Add user with channel for dormant check.
- await self.help_channel_claimants.set(channel.id, message.author.id)
+ await _caches.claimants.set(channel.id, message.author.id)
self.bot.stats.incr("help.claimed")
# Must use a timezone-aware datetime to ensure a correct POSIX timestamp.
timestamp = datetime.now(timezone.utc).timestamp()
- await self.claim_times.set(channel.id, timestamp)
+ await _caches.claim_times.set(channel.id, timestamp)
- await _message.unanswered.set(channel.id, True)
+ await _caches.unanswered.set(channel.id, True)
log.trace(f"Releasing on_message lock for {message.id}.")
diff --git a/bot/exts/help_channels/_cooldown.py b/bot/exts/help_channels/_cooldown.py
index c4fd4b662..c5c39297f 100644
--- a/bot/exts/help_channels/_cooldown.py
+++ b/bot/exts/help_channels/_cooldown.py
@@ -2,20 +2,15 @@ import logging
from typing import Callable, Coroutine
import discord
-from async_rediscache import RedisCache
import bot
from bot import constants
-from bot.exts.help_channels import _channel
+from bot.exts.help_channels import _caches, _channel
from bot.utils.scheduling import Scheduler
log = logging.getLogger(__name__)
CoroutineFunc = Callable[..., Coroutine]
-# This cache tracks which channels are claimed by which members.
-# RedisCache[discord.TextChannel.id, t.Union[discord.User.id, discord.Member.id]]
-_help_channel_claimants = RedisCache(namespace="HelpChannels.help_channel_claimants")
-
async def add_cooldown_role(member: discord.Member) -> None:
"""Add the help cooldown role to `member`."""
@@ -29,7 +24,7 @@ async def check_cooldowns(scheduler: Scheduler) -> None:
guild = bot.instance.get_guild(constants.Guild.id)
cooldown = constants.HelpChannels.claim_minutes * 60
- for channel_id, member_id in await _help_channel_claimants.items():
+ for channel_id, member_id in await _caches.claimants.items():
member = guild.get_member(member_id)
if not member:
continue # Member probably left the guild.
diff --git a/bot/exts/help_channels/_message.py b/bot/exts/help_channels/_message.py
index eaf8b0ab5..8b058d5aa 100644
--- a/bot/exts/help_channels/_message.py
+++ b/bot/exts/help_channels/_message.py
@@ -3,10 +3,10 @@ import typing as t
from datetime import datetime
import discord
-from async_rediscache import RedisCache
import bot
from bot import constants
+from bot.exts.help_channels import _caches
from bot.utils.channel import is_in_category
log = logging.getLogger(__name__)
@@ -40,20 +40,6 @@ question to maximize your chance of getting a good answer. If you're not sure ho
through our guide for **[asking a good question]({ASKING_GUIDE_URL})**.
"""
-# This cache 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.
-# RedisCache[discord.TextChannel.id, bool]
-unanswered = RedisCache(namespace="HelpChannels.unanswered")
-
-# This cache tracks which channels are claimed by which members.
-# RedisCache[discord.TextChannel.id, t.Union[discord.User.id, discord.Member.id]]
-_help_channel_claimants = RedisCache(namespace="HelpChannels.help_channel_claimants")
-
-# This cache maps a help channel to original question message in same channel.
-# RedisCache[discord.TextChannel.id, discord.Message.id]
-_question_messages = RedisCache(namespace="HelpChannels.question_messages")
-
async def check_for_answer(message: discord.Message) -> None:
"""Checks for whether new content in a help channel comes from non-claimants."""
@@ -64,8 +50,8 @@ async def check_for_answer(message: discord.Message) -> None:
log.trace(f"Checking if #{channel} ({channel.id}) has been answered.")
# Check if there is an entry in unanswered
- if await unanswered.contains(channel.id):
- claimant_id = await _help_channel_claimants.get(channel.id)
+ if await _caches.unanswered.contains(channel.id):
+ claimant_id = await _caches.claimants.get(channel.id)
if not claimant_id:
# The mapping for this channel doesn't exist, we can't do anything.
return
@@ -73,7 +59,7 @@ async def check_for_answer(message: discord.Message) -> None:
# Check the message did not come from the claimant
if claimant_id != message.author.id:
# Mark the channel as answered
- await unanswered.set(channel.id, False)
+ await _caches.unanswered.set(channel.id, False)
async def get_last_message(channel: discord.TextChannel) -> t.Optional[discord.Message]:
@@ -154,7 +140,7 @@ async def notify(channel: discord.TextChannel, last_notification: t.Optional[dat
async def pin(message: discord.Message) -> None:
"""Pin an initial question `message` and store it in a cache."""
if await _pin_wrapper(message.id, message.channel, pin=True):
- await _question_messages.set(message.channel.id, message.id)
+ await _caches.question_messages.set(message.channel.id, message.id)
async def send_available_message(channel: discord.TextChannel) -> None:
@@ -180,7 +166,7 @@ async def send_available_message(channel: discord.TextChannel) -> None:
async def unpin(channel: discord.TextChannel) -> None:
"""Unpin the initial question message sent in `channel`."""
- msg_id = await _question_messages.pop(channel.id)
+ msg_id = await _caches.question_messages.pop(channel.id)
if msg_id is None:
log.debug(f"#{channel} ({channel.id}) doesn't have a message pinned.")
else: