aboutsummaryrefslogtreecommitdiffstats
path: root/arthur/bot.py
diff options
context:
space:
mode:
authorGravatar Joe Banks <[email protected]>2021-07-17 20:10:11 +0100
committerGravatar Joe Banks <[email protected]>2021-07-17 20:10:11 +0100
commitd3d81582cd7005df16fe3f5306e6afbed75bdb74 (patch)
tree323a7441f6c4d32833fee922208121497c77a9de /arthur/bot.py
Initial commit
Diffstat (limited to 'arthur/bot.py')
-rw-r--r--arthur/bot.py61
1 files changed, 61 insertions, 0 deletions
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 <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}</>"
+ )