hot reloading and authentication

master
cynic 4 years ago
parent 582145b479
commit bfa0e3da5c
  1. 5
      config.json
  2. 15
      plugin_manager.py
  3. 12
      plugins/authy.py
  4. 4
      plugins/dango.py
  5. 20
      plugins/hotload.py

@ -1,4 +1,7 @@
{ {
"token": "YOUR_TOKEN_HERE", "token": "YOUR_TOKEN_HERE",
"plugin_path": "./plugins" "plugin_path": "./plugins",
"authed": [
]
} }

@ -7,12 +7,21 @@ class plugin_manager():
plugins = [name.split(".py")[0] for name in os.listdir(plugin_path) if name.endswith(".py")] plugins = [name.split(".py")[0] for name in os.listdir(plugin_path) if name.endswith(".py")]
self.plugins_loaded = [] self.plugins_loaded = []
for plugin in plugins: for plugin in plugins:
module = __import__(plugin) try:
module = __import__(plugin)
except:
logger.write(f"error loading plugin {plugin}")
continue
if not(hasattr(module, "run")) or not(hasattr(module, "hooks")): if not(hasattr(module, "run")) or not(hasattr(module, "hooks")):
self.logger.write("plugin {plugin} is invalid: crucial attribute missing!") logger.write(f"plugin {plugin} is invalid: crucial attribute missing!")
continue
else: else:
self.plugins_loaded.append(module) self.plugins_loaded.append(module)
logger.write(f"loaded plugin {module.__name__}") # this name will be used for hotloading
def handle(self, event, ctx, bot): def handle(self, event, ctx, bot):
for plugin in self.plugins_loaded: for plugin in self.plugins_loaded:
if event in plugin.hooks: if event in plugin.hooks:
getattr(plugin, "run")(event, ctx, bot) try:
getattr(plugin, "run")(event, ctx, bot)
except Exception as e:
bot.logger.write(e)

@ -0,0 +1,12 @@
hooks = ["MESSAGE_CREATE"]
def run(event, ctx, bot):
if ctx["content"].startswith(":") and not(int(ctx["author"]["id"]) in bot.config["authed"]):
bot.send_msg(ctx["channel_id"], "you lack the proper authentication")
return
if ctx["content"] == ":ops":
bot.send_msg(ctx["channel_id"], ", ".join([str(ident) for ident in bot.config["authed"]]))
elif ctx["content"].startswith(":op"):
for mention in ctx["mentions"]:
bot.config["authed"].append(int(mention["id"]))
bot.send_msg(ctx["channel_id"], f"added {mention['username']} to the session authed!")
bot.logger.write(f"added auth {mention['id']} {mention['username']}")

@ -13,7 +13,5 @@ this particular example listens for a message that contains the string 'dango' a
hooks = ["MESSAGE_CREATE"] # run() will be called when client.dispatch() gets a MESSAGE_CREATE hooks = ["MESSAGE_CREATE"] # run() will be called when client.dispatch() gets a MESSAGE_CREATE
def run(event, ctx, bot): def run(event, ctx, bot):
if not(ctx["author"]["id"] == bot.user["id"]): # if the message author is not our bot... if ctx["content"] == "dango": # if the message body matches...
if ctx["content"] == "dango": # and if the message body matches...
bot.logger.write("d/a/ngo")
bot.send_msg(ctx["channel_id"], "to all the motherfuckers that shed a tear") bot.send_msg(ctx["channel_id"], "to all the motherfuckers that shed a tear")

@ -0,0 +1,20 @@
from importlib import reload
hooks = ["MESSAGE_CREATE"]
def run(event, ctx, bot):
if ctx["content"].startswith(":") and not(int(ctx["author"]["id"]) in bot.config["authed"]):
bot.send_msg(ctx["channel_id"], "you lack the proper authentication")
return
plugin_manager = bot.plugman
if ctx["content"].startswith(":reload"):
cmd = ctx["content"].split(" ")[1]
if not(cmd in [m.__name__ for m in plugin_manager.plugins_loaded]):
bot.send_msg(ctx["channel_id"], f"plugin {cmd} could not be found for reloading!")
else:
for m in plugin_manager.plugins_loaded:
if m.__name__ == cmd:
reload(m)
bot.send_msg(ctx["channel_id"], f"reloaded plugin {cmd}!")
bot.logger.write(f"reloaded {cmd}")
elif ctx["content"] ==":plugins":
plugins = ", ".join([m.__name__ for m in plugin_manager.plugins_loaded])
bot.send_msg(ctx["channel_id"], f"{plugins}")
Loading…
Cancel
Save