diff --git a/config.json b/config.json index 07569d2..a589a61 100644 --- a/config.json +++ b/config.json @@ -1,4 +1,7 @@ { "token": "YOUR_TOKEN_HERE", - "plugin_path": "./plugins" + "plugin_path": "./plugins", + "authed": [ + + ] } \ No newline at end of file diff --git a/plugin_manager.py b/plugin_manager.py index 710590a..730f2b8 100644 --- a/plugin_manager.py +++ b/plugin_manager.py @@ -7,12 +7,21 @@ class plugin_manager(): plugins = [name.split(".py")[0] for name in os.listdir(plugin_path) if name.endswith(".py")] self.plugins_loaded = [] 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")): - self.logger.write("plugin {plugin} is invalid: crucial attribute missing!") + logger.write(f"plugin {plugin} is invalid: crucial attribute missing!") + continue else: 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): for plugin in self.plugins_loaded: if event in plugin.hooks: - getattr(plugin, "run")(event, ctx, bot) \ No newline at end of file + try: + getattr(plugin, "run")(event, ctx, bot) + except Exception as e: + bot.logger.write(e) \ No newline at end of file diff --git a/plugins/authy.py b/plugins/authy.py new file mode 100644 index 0000000..0491e75 --- /dev/null +++ b/plugins/authy.py @@ -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']}") \ No newline at end of file diff --git a/plugins/dango.py b/plugins/dango.py index c398761..39c046e 100644 --- a/plugins/dango.py +++ b/plugins/dango.py @@ -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 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": # and if the message body matches... - bot.logger.write("d/a/ngo") + if ctx["content"] == "dango": # if the message body matches... bot.send_msg(ctx["channel_id"], "to all the motherfuckers that shed a tear") \ No newline at end of file diff --git a/plugins/hotload.py b/plugins/hotload.py new file mode 100644 index 0000000..f27eeb5 --- /dev/null +++ b/plugins/hotload.py @@ -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}") \ No newline at end of file