From d3d81582cd7005df16fe3f5306e6afbed75bdb74 Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Sat, 17 Jul 2021 20:10:11 +0100 Subject: Initial commit --- arthur/bot.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 arthur/bot.py (limited to 'arthur/bot.py') diff --git a/arthur/bot.py b/arthur/bot.py new file mode 100644 index 0000000..ebc4391 --- /dev/null +++ b/arthur/bot.py @@ -0,0 +1,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 {self.user}") + + # Start extension loading + + for path, extension in find_extensions(): + logger.info( + f"Loading extension {path.stem} " f"from {path.parent}" + ) + + try: + self.load_extension(extension) + except: # noqa: E722 + logger.exception( + f"Failed to load extension {path.stem} " + f"from {path.parent}", + ) + else: + logger.info( + f"Loaded extension {path.stem} " f"from {path.parent}" + ) -- cgit v1.2.3