startup info and dynamic module reloading

This commit is contained in:
fanyx 2020-11-15 15:44:03 +01:00
parent 0d999b0e3d
commit ec6ab3e5d2
7 changed files with 66 additions and 26 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
venv venv
__pycache__/

15
main.py
View File

@ -1,7 +1,5 @@
import logging
import sys import sys
from os import listdir from os import listdir
from discord.ext.commands import Bot from discord.ext.commands import Bot
from src.utils.config import build_config from src.utils.config import build_config
@ -14,19 +12,12 @@ bot = Bot(command_prefix="~ak ")
TOKEN = config['AUTH']['token'] TOKEN = config['AUTH']['token']
# load extensions # load extensions
for file in os.listdir("src/ext"): for file in listdir("src/ext"):
if file.endswith(".py"): if file.endswith(".py"):
name = file[:-3] name = file[:-3]
try: bot.load_extension(f"src.ext.{name}")
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.")
if __name__ == "__main__": if __name__ == "__main__":
logging.info("Starting bot...") print("[INFO]: Starting bot...")
# use token from config # use token from config
bot.run(TOKEN) bot.run(TOKEN)

24
src/ext/admin.py Normal file
View File

@ -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))

21
src/ext/events.py Normal file
View File

@ -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))

14
src/ext/test.py Normal file
View File

@ -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))

View File

@ -1 +0,0 @@

View File

@ -1,23 +1,13 @@
import os from os import getenv
import logging
from configparser import ConfigParser from configparser import ConfigParser
def build_config(): def build_config():
config_path = os.getenv("AKWAH_CONFIG_PATH") config_path = getenv("AKWAH_CONFIG_PATH")
if config_path == None: if config_path == None:
config_path = "config.ini" 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 = ConfigParser()
config.read_file(config_file) config.read_file(open(config_path))
logging.info("Finished building configuration.")
return config return config