aboutsummaryrefslogtreecommitdiffstats
path: root/botcore/utils/channel.py
blob: 17e70a2a33e82a363ab83bd271cb06b323332ca0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""Useful helper functions for interacting with various discord.py channel objects."""

import discord
from discord.ext.commands import Bot

from botcore.utils import logging

log = logging.get_logger(__name__)


def is_in_category(channel: discord.TextChannel, category_id: int) -> bool:
    """
    Return whether the given ``channel`` in the the category with the id ``category_id``.

    Args:
        channel: The channel to check.
        category_id: The category to check for.

    Returns:
        A bool depending on whether the channel is in the category.
    """
    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 the given ``channel_id`` from the bots cache, and return it.

    Args:
        bot: The :obj:`discord.ext.commands.Bot` instance to use for getting/fetching.
        channel_id: The channel to get/fetch.

    Raises:
        :exc:`discord.InvalidData`
            An unknown channel type was received from Discord.
        :exc:`discord.HTTPException`
            Retrieving the channel failed.
        :exc:`discord.NotFound`
            Invalid Channel ID.
        :exc:`discord.Forbidden`
            You do not have permission to fetch this channel.

    Returns:
        The channel from the ID.
    """
    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