Merge pull request #363 from miaowware/plugins

add support for plugins
This commit is contained in:
classabbyamp 2021-03-18 01:26:35 -04:00 committed by GitHub
commit 54960e8237
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 7 deletions

1
.gitignore vendored
View File

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

View File

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

View File

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

36
main.py
View File

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