From 2b63ccba96d8651f2ff7d43a5f10f95f2b154268 Mon Sep 17 00:00:00 2001 From: Chris Lovering Date: Sun, 21 Aug 2022 22:12:24 +0100 Subject: Use BotBase from bot-core This includes: - Moving to an async main function to start the bot - Moving all extension loading (barring jishaku) to bot core - Adding dunder init files to all packages, required by bot-core ext loaders --- arthur/bot.py | 50 ++++++++++++++++---------------------------------- 1 file changed, 16 insertions(+), 34 deletions(-) (limited to 'arthur/bot.py') diff --git a/arthur/bot.py b/arthur/bot.py index 2bbb69c..2220db6 100644 --- a/arthur/bot.py +++ b/arthur/bot.py @@ -2,27 +2,20 @@ from pathlib import Path from typing import Any, Union +from botcore import BotBase +from botcore.utils import scheduling from discord import Interaction, Member, User from discord.ext import commands -from discord.ext.commands import Bot from kubernetes_asyncio import config -from arthur import logger +from arthur import exts, logger from arthur.config import CONFIG -from arthur.extensions import find_extensions -class KingArthur(Bot): +class KingArthur(BotBase): """Base bot class for King Arthur.""" def __init__(self, *args: list[Any], **kwargs: dict[str, Any]) -> None: - config = { - "command_prefix": commands.when_mentioned_or(*CONFIG.prefixes), - "case_insensitive": True, - } - - kwargs.update(config) - super().__init__(*args, **kwargs) self.add_check(self._is_devops) @@ -31,7 +24,10 @@ class KingArthur(Bot): def _is_devops(ctx: Union[commands.Context, Interaction]) -> bool: """Check all commands are executed by authorised personnel.""" if isinstance(ctx, Interaction): - return CONFIG.devops_role in [r.id for r in ctx.author.roles] + if isinstance(ctx.user, Member): + return CONFIG.devops_role in [r.id for r in ctx.user.roles] + else: + return False if ctx.command.name == "ed": return True @@ -41,37 +37,23 @@ class KingArthur(Bot): 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.""" + async def setup_hook(self) -> None: + """Async initialisation method for discord.py.""" + await super().setup_hook() + # Authenticate with Kubernetes if (Path.home() / ".kube/config").exists(): await config.load_kube_config() else: config.load_incluster_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}" - ) + # This is not awaited to avoid a deadlock with any cogs that have + # wait_until_guild_available in their cog_load method. + scheduling.create_task(self.load_extensions(exts)) logger.info("Loading jishaku") - self.load_extension("jishaku") + await self.load_extension("jishaku") logger.info("Loaded jishaku") async def is_owner(self, user: Union[User, Member]) -> bool: -- cgit v1.2.3