Skip to content

Commit

Permalink
feat: load cogs automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
ooliver1 committed Apr 27, 2022
1 parent 75aa8fc commit dca07b7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion botbase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from .checks import *


__version__ = "1.13.5"
__version__ = "1.14.0"


getLogger(__name__).addHandler(NullHandler())
37 changes: 34 additions & 3 deletions botbase/botbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
from importlib import import_module
from logging import CRITICAL, INFO, Formatter, getLogger
from logging.handlers import RotatingFileHandler
from os import listdir
from os.path import isfile
from pathlib import Path
from random import choice
from sys import modules
from textwrap import dedent
from typing import TYPE_CHECKING

import jishaku
from aiohttp import ClientSession
from asyncpg import create_pool
from nextcord import Embed, Interaction, Member, Thread, User, abc
Expand Down Expand Up @@ -52,7 +55,22 @@ class BotBase(Bot):
session: ClientSession
blacklist: Optional[Blacklist]

def __init__(self, *args, config_module: str = "config", **kwargs) -> None:
@staticmethod
def get_config(config_module: str) -> tuple[str, str]:
file = modules["__main__"].__file__

if file is None:
raise RuntimeError("how")

path = Path(file)

if path.parts[-1] == "__main__.py":
mod = path.parts[-2]
return f"{mod}.{config_module}", mod
else:
return config_module, ""

def __init__(self, *args, config_module: str = "default", **kwargs) -> None:
pre = kwargs.pop("command_prefix", self.get_pre)
saf = kwargs.pop("strip_after_prefix", True)
ca = kwargs.pop("case_insensitive", True)
Expand Down Expand Up @@ -88,7 +106,10 @@ def __init__(self, *args, config_module: str = "config", **kwargs) -> None:

self.loop.set_exception_handler(self.asyncio_handler)

config = import_module(config_module.rstrip(".py"))
cfg, mod = self.get_config(config_module.rstrip(".py"))
config = import_module(cfg)

self.mod = mod

self.db_enabled: bool
self.db_args: tuple[Any, ...]
Expand Down Expand Up @@ -183,6 +204,16 @@ async def startup(self) -> None:
def run(self, *args, **kwargs) -> None:
self.loop.create_task(self.startup())

cog_dir = f"{self.mod}/cogs" if self.mod else "./cogs"
cogs_mod = f"{self.mod}.cogs" if self.mod else "cogs"

for filename in listdir(cog_dir):
if filename.endswith(".py"):
self.load_extension(f"{cogs_mod}.{filename[:-3]}")
else:
if isfile(filename):
print(f"Unable to load {filename[:-3]}")

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

async def close(self, *args, **kwargs) -> None:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ooliver-botbase"
version = "1.13.5"
version = "1.14.0"
description = "A personal nextcord bot base package for bots."
authors = ["ooliver1 <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit dca07b7

Please sign in to comment.