From 060bad105dc2569fc485adb03b985aa2ab5d367e Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Tue, 22 Feb 2022 23:47:09 +0000 Subject: Move new utilities to the util namespace --- botcore/utils/channel.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 botcore/utils/channel.py (limited to 'botcore/utils/channel.py') diff --git a/botcore/utils/channel.py b/botcore/utils/channel.py new file mode 100644 index 00000000..7e0fc387 --- /dev/null +++ b/botcore/utils/channel.py @@ -0,0 +1,26 @@ +"""Utilities for interacting with discord channels.""" + +import discord +from discord.ext.commands import Bot + +from botcore.utils import loggers + +log = loggers.get_logger(__name__) + + +def is_in_category(channel: discord.TextChannel, category_id: int) -> bool: + """Return True if `channel` is within a category with `category_id`.""" + return getattr(channel, "category_id", None) == category_id + + +async def get_or_fetch_channel(bot: Bot, channel_id: int) -> discord.abc.GuildChannel: + """Attempt to get or fetch a channel and return it.""" + log.trace(f"Getting the channel {channel_id}.") + + channel = bot.get_channel(channel_id) + if not channel: + log.debug(f"Channel {channel_id} is not in cache; fetching from API.") + channel = await bot.fetch_channel(channel_id) + + log.trace(f"Channel #{channel} ({channel_id}) retrieved.") + return channel -- cgit v1.2.3