From ec6ab3e5d209953ebc7d019ebb7d52851e3daa90 Mon Sep 17 00:00:00 2001 From: fanyx Date: Sun, 15 Nov 2020 15:44:03 +0100 Subject: [PATCH] startup info and dynamic module reloading --- .gitignore | 1 + main.py | 15 +++------------ src/ext/admin.py | 24 ++++++++++++++++++++++++ src/ext/events.py | 21 +++++++++++++++++++++ src/ext/test.py | 14 ++++++++++++++ src/utils/__init__.py | 1 - src/utils/config.py | 16 +++------------- 7 files changed, 66 insertions(+), 26 deletions(-) create mode 100644 src/ext/admin.py create mode 100644 src/ext/events.py create mode 100644 src/ext/test.py diff --git a/.gitignore b/.gitignore index 5ceb386..499ca79 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ venv +__pycache__/ \ No newline at end of file diff --git a/main.py b/main.py index d3a4e6a..2a7d0c5 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,5 @@ -import logging import sys from os import listdir - from discord.ext.commands import Bot from src.utils.config import build_config @@ -14,19 +12,12 @@ bot = Bot(command_prefix="~ak ") TOKEN = config['AUTH']['token'] # load extensions -for file in os.listdir("src/ext"): +for file in listdir("src/ext"): if file.endswith(".py"): name = file[:-3] - try: - bot.load_extension(f"src.ext.{name}") - except NoEntryPointError: - logging.error(f"Extension {name} cannot be loaded. [No setup function]") - except ExtensionFailed: - logging.error(f"Extension {name} failed to load. [Execution error]") - - logging.info("Finished loading extensions.") + bot.load_extension(f"src.ext.{name}") if __name__ == "__main__": - logging.info("Starting bot...") + print("[INFO]: Starting bot...") # use token from config bot.run(TOKEN) \ No newline at end of file diff --git a/src/ext/admin.py b/src/ext/admin.py new file mode 100644 index 0000000..bff8b60 --- /dev/null +++ b/src/ext/admin.py @@ -0,0 +1,24 @@ +import discord +from discord.ext import commands + +class Admin(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @commands.group() + async def admin(self, ctx): + """ + manages bot administration + """ + pass + + @admin.command(name="reload") + async def admin_reload(self, ctx, module): + try: + self.bot.reload_extension(f"src.ext.{module}") + await ctx.send(f"Reload successful: [{module}]") + except: + await ctx.send(f"Reload failed: [{module}]") + +def setup(bot): + bot.add_cog(Admin(bot)) \ No newline at end of file diff --git a/src/ext/events.py b/src/ext/events.py new file mode 100644 index 0000000..0434217 --- /dev/null +++ b/src/ext/events.py @@ -0,0 +1,21 @@ +import discord +from discord.ext import commands +from peewee import * + +class Events(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @commands.Cog.listener() + async def on_ready(self): + """ + Defines all startup checks + + - migrations + - activity + - presence + """ + print(f"[INFO]: Startup complete | {self.bot.user}") + +def setup(bot): + bot.add_cog(Events(bot)) \ No newline at end of file diff --git a/src/ext/test.py b/src/ext/test.py new file mode 100644 index 0000000..197e2ca --- /dev/null +++ b/src/ext/test.py @@ -0,0 +1,14 @@ +import discord +from discord.ext import commands + +class Test(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @commands.command() + @commands.guild_only() + async def ping(self, ctx = commands.Context): + await ctx.send("pong") + +def setup(bot): + bot.add_cog(Test(bot)) \ No newline at end of file diff --git a/src/utils/__init__.py b/src/utils/__init__.py index 8b13789..e69de29 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -1 +0,0 @@ - diff --git a/src/utils/config.py b/src/utils/config.py index 1803121..47afc4f 100644 --- a/src/utils/config.py +++ b/src/utils/config.py @@ -1,23 +1,13 @@ -import os -import logging +from os import getenv from configparser import ConfigParser - def build_config(): - config_path = os.getenv("AKWAH_CONFIG_PATH") + config_path = getenv("AKWAH_CONFIG_PATH") if config_path == None: config_path = "config.ini" - - try: - config_file = open(config_path) - except FileNotFoundError: - logging.critical("Unable to locate the configuration file.") - sys.exit(1) config = ConfigParser() - config.read_file(config_file) - logging.info("Finished building configuration.") - + config.read_file(open(config_path)) return config