add support for plugins

This commit is contained in:
Abigail G 2021-03-17 21:49:24 -04:00
parent 3a5ce9f1c0
commit 79545403d8
No known key found for this signature in database
GPG Key ID: 6BE0755918A4C7F5
4 changed files with 41 additions and 7 deletions

1
.gitignore vendored
View File

@ -6,6 +6,7 @@ cty.zip
/botenv/ /botenv/
/devenv/ /devenv/
/data/ /data/
/data/plugins/
/docker-compose.yml /docker-compose.yml

View File

@ -32,7 +32,7 @@ help:
# Main install target # Main install target
.PHONY: install .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 # Virual environment setup
$(BOTENV)/success: $(BOTENV)/success:
@ -55,6 +55,11 @@ data:
@echo "\033[34;1m--> Creating ./data ...\033[0m" @echo "\033[34;1m--> Creating ./data ...\033[0m"
@mkdir -p data @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 # Copying templates
data/options.py data/keys.py: ./data data/options.py data/keys.py: ./data
@echo "\033[34;1m--> Copying template for ./$@ ...\033[0m" @echo "\033[34;1m--> Copying template for ./$@ ...\033[0m"
@ -72,7 +77,7 @@ clean:
### Dev targets ### ### Dev targets ###
.PHONY: dev-install .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 # Installing dev requirements
$(BOTENV)/dev_req_done: dev-requirements.txt $(BOTENV)/success $(BOTENV)/dev_req_done: dev-requirements.txt $(BOTENV)/success

View File

@ -215,7 +215,7 @@ class BaseCog(commands.Cog):
[miaowware/qrm-resources](https://github.com/miaowware/qrm-resources/issues).""" [miaowware/qrm-resources](https://github.com/miaowware/qrm-resources/issues)."""
await ctx.send(embed=embed) await ctx.send(embed=embed)
@commands.command(name="donate") @commands.command(name="donate", aliases=["tip"])
async def _donate(self, ctx: commands.Context): async def _donate(self, ctx: commands.Context):
"""Shows ways to help support development of the bot via donations.""" """Shows ways to help support development of the bot via donations."""
embed = cmn.embed_factory(ctx) embed = cmn.embed_factory(ctx)

36
main.py
View File

@ -15,6 +15,7 @@ import sys
import traceback import traceback
from datetime import datetime, time from datetime import datetime, time
from types import SimpleNamespace from types import SimpleNamespace
from pathlib import Path
import pytz 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) 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. 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) 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.""" """Lists loaded extensions."""
embed = cmn.embed_factory(ctx) embed = cmn.embed_factory(ctx)
embed.title = "Loaded Extensions" 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) await ctx.send(embed=embed)
@_extctl.command(name="load", aliases=["ld"]) @_extctl.command(name="load", aliases=["ld"])
async def _extctl_load(ctx: commands.Context, extension: str): async def _extctl_load(ctx: commands.Context, extension: str):
"""Loads an extension.""" """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) 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) pika = bot.get_emoji(opt.pika)
if pika: if pika:
await cmn.add_react(ctx.message, 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) await cmn.add_react(ctx.message, cmn.emojis.check_mark)
@_extctl.command(name="unload", aliases=["ul"]) @_extctl.command(name="unload", aliases=["ul"])
async def _extctl_unload(ctx: commands.Context, extension: str): async def _extctl_unload(ctx: commands.Context, extension: str):
"""Unloads an extension.""" """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) await cmn.add_react(ctx.message, cmn.emojis.check_mark)
@ -246,6 +270,10 @@ async def _ensure_activity_fixed():
for ext in opt.exts: for ext in opt.exts:
bot.load_extension(ext_dir + "." + ext) 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: try:
bot.run(keys.discord_token) bot.run(keys.discord_token)