aboutsummaryrefslogtreecommitdiffstats
path: root/arthur/bot.py
blob: ebc43917ad6e3b4c064a1408556b057b1c6ea68d (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
55
56
57
58
59
60
61
"""Module containing the core bot base for King Arthur."""
from typing import Any

from discord.ext import commands
from discord.ext.commands import Bot
from discord_components import DiscordComponents
from kubernetes_asyncio import config

from arthur import logger
from arthur.config import CONFIG
from arthur.extensions import find_extensions


class KingArthur(Bot):
    """Base bot class for King Arthur."""

    def __init__(self, *args: list[Any], **kwargs: dict[str, Any]) -> None:
        config = {"command_prefix": CONFIG.prefix}

        kwargs.update(config)

        super().__init__(*args, **kwargs)

        self.add_check(self._is_devops)

    @staticmethod
    async def _is_devops(ctx: commands.Context) -> bool:
        """Check all commands are executed by authorised personnel."""
        if not ctx.guild:
            return False

        return CONFIG.devops_role in [r.id for r in ctx.author.roles]

    async def on_ready(self) -> None:
        """Initialise bot once connected and authorised with Discord."""
        # Initialise components (e.g. buttons, selections)
        DiscordComponents(self)

        # Authenticate with Kubernetes
        await config.load_kube_config()

        logger.info(f"Logged in <red>{self.user}</>")

        # Start extension loading

        for path, extension in find_extensions():
            logger.info(
                f"Loading extension <magenta>{path.stem}</> " f"from <magenta>{path.parent}</>"
            )

            try:
                self.load_extension(extension)
            except:  # noqa: E722
                logger.exception(
                    f"Failed to load extension <magenta>{path.stem}</> "
                    f"from <magenta>{path.parent}</>",
                )
            else:
                logger.info(
                    f"Loaded extension <magenta>{path.stem}</> " f"from <magenta>{path.parent}</>"
                )