From 79545403d8bdc473b7b1261b4f6a4ccc5199873c Mon Sep 17 00:00:00 2001 From: Abigail G Date: Wed, 17 Mar 2021 21:49:24 -0400 Subject: [PATCH] add support for plugins --- .gitignore | 1 + Makefile | 9 +++++++-- exts/base.py | 2 +- main.py | 36 ++++++++++++++++++++++++++++++++---- 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 81cefc1..5e411d9 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ cty.zip /botenv/ /devenv/ /data/ +/data/plugins/ /docker-compose.yml diff --git a/Makefile b/Makefile index 62be00f..93e2c75 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ help: # Main install target .PHONY: install -install: $(BOTENV)/req_done data/options.py data/keys.py +install: $(BOTENV)/req_done data/options.py data/keys.py data/plugins # Virual environment setup $(BOTENV)/success: @@ -55,6 +55,11 @@ data: @echo "\033[34;1m--> Creating ./data ...\033[0m" @mkdir -p data +# Creating the ./data/plugins subdirectory +data/plugins: ./data + @echo "\033[34;1m--> Creating ./data/plugins ...\033[0m" + @mkdir -p data/plugins + # Copying templates data/options.py data/keys.py: ./data @echo "\033[34;1m--> Copying template for ./$@ ...\033[0m" @@ -72,7 +77,7 @@ clean: ### Dev targets ### .PHONY: dev-install -dev-install: $(BOTENV)/dev_req_done data/options.py data/keys.py +dev-install: $(BOTENV)/dev_req_done data/options.py data/keys.py data/plugins # Installing dev requirements $(BOTENV)/dev_req_done: dev-requirements.txt $(BOTENV)/success diff --git a/exts/base.py b/exts/base.py index 2204676..3a24a71 100644 --- a/exts/base.py +++ b/exts/base.py @@ -215,7 +215,7 @@ class BaseCog(commands.Cog): [miaowware/qrm-resources](https://github.com/miaowware/qrm-resources/issues).""" await ctx.send(embed=embed) - @commands.command(name="donate") + @commands.command(name="donate", aliases=["tip"]) async def _donate(self, ctx: commands.Context): """Shows ways to help support development of the bot via donations.""" embed = cmn.embed_factory(ctx) diff --git a/main.py b/main.py index fcb0fc2..394bf98 100644 --- a/main.py +++ b/main.py @@ -15,6 +15,7 @@ import sys import traceback from datetime import datetime, time from types import SimpleNamespace +from pathlib import Path import pytz @@ -34,6 +35,7 @@ import data.options as opt exit_code = 1 # The default exit code. ?shutdown and ?restart will change it accordingly (fail-safe) ext_dir = "exts" # The name of the directory where extensions are located. +plugin_dir = "data.plugins" # The name of the directory where plugins are located. debug_mode = opt.debug # Separate assignement in-case we define an override (ternary operator goes here) @@ -108,14 +110,24 @@ async def _extctl_list(ctx: commands.Context): """Lists loaded extensions.""" embed = cmn.embed_factory(ctx) embed.title = "Loaded Extensions" - embed.description = "\n".join(["‣ " + x.split(".")[1] for x in bot.extensions.keys()]) + embed.description = "\n".join( + ["‣ " + x.split(".")[-1] for x in bot.extensions.keys() if not x.startswith(plugin_dir)] + ) + if plugins := ["‣ " + x.split(".")[-1] for x in bot.extensions.keys() if x.startswith(plugin_dir)]: + embed.add_field(name="Loaded Plugins", value="\n".join(plugins)) await ctx.send(embed=embed) @_extctl.command(name="load", aliases=["ld"]) async def _extctl_load(ctx: commands.Context, extension: str): """Loads an extension.""" - bot.load_extension(ext_dir + "." + extension) + try: + bot.load_extension(ext_dir + "." + extension) + except commands.ExtensionNotFound as e: + try: + bot.load_extension(plugin_dir + "." + extension) + except commands.ExtensionNotFound: + raise e await cmn.add_react(ctx.message, cmn.emojis.check_mark) @@ -126,14 +138,26 @@ async def _extctl_reload(ctx: commands.Context, extension: str): pika = bot.get_emoji(opt.pika) if pika: await cmn.add_react(ctx.message, pika) - bot.reload_extension(ext_dir + "." + extension) + try: + bot.reload_extension(ext_dir + "." + extension) + except commands.ExtensionNotLoaded as e: + try: + bot.reload_extension(plugin_dir + "." + extension) + except commands.ExtensionNotLoaded: + raise e await cmn.add_react(ctx.message, cmn.emojis.check_mark) @_extctl.command(name="unload", aliases=["ul"]) async def _extctl_unload(ctx: commands.Context, extension: str): """Unloads an extension.""" - bot.unload_extension(ext_dir + "." + extension) + try: + bot.unload_extension(ext_dir + "." + extension) + except commands.ExtensionNotLoaded as e: + try: + bot.unload_extension(plugin_dir + "." + extension) + except commands.ExtensionNotLoaded: + raise e await cmn.add_react(ctx.message, cmn.emojis.check_mark) @@ -246,6 +270,10 @@ async def _ensure_activity_fixed(): for ext in opt.exts: bot.load_extension(ext_dir + "." + ext) +# load all py files in plugin_dir +for plugin in (f.stem for f in Path(plugin_dir.replace(".", "/")).glob("*.py")): + bot.load_extension(plugin_dir + "." + plugin) + try: bot.run(keys.discord_token)