diff options
author | 2022-02-21 12:58:10 +0000 | |
---|---|---|
committer | 2022-02-21 12:58:10 +0000 | |
commit | bcdb3a77690e1e224225627f085d86689353e1cb (patch) | |
tree | bb21c2fa12e06b9c84142ddd4f1f4260f078b185 /botcore/channel.py | |
parent | Modify Autodoc Formatting (diff) |
Port many utilities from bot
Diffstat (limited to 'botcore/channel.py')
-rw-r--r-- | botcore/channel.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/botcore/channel.py b/botcore/channel.py new file mode 100644 index 00000000..b19b4f08 --- /dev/null +++ b/botcore/channel.py @@ -0,0 +1,26 @@ +"""Utilities for interacting with discord channels.""" + +import discord +from discord.ext.commands import Bot + +from botcore 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 |