parent
663ca911ea
commit
582145b479
@ -1,3 +1,4 @@ |
|||||||
{ |
{ |
||||||
"token": "YOUR_TOKEN_HERE" |
"token": "YOUR_TOKEN_HERE", |
||||||
|
"plugin_path": "./plugins" |
||||||
} |
} |
@ -0,0 +1,18 @@ |
|||||||
|
import os |
||||||
|
import sys |
||||||
|
|
||||||
|
class plugin_manager(): |
||||||
|
def __init__(self, logger, plugin_path = "./plugins"): |
||||||
|
sys.path.append(plugin_path) # needed to __import__ scripts without a hassle, shouldn't cause an issue |
||||||
|
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) |
||||||
|
if not(hasattr(module, "run")) or not(hasattr(module, "hooks")): |
||||||
|
self.logger.write("plugin {plugin} is invalid: crucial attribute missing!") |
||||||
|
else: |
||||||
|
self.plugins_loaded.append(module) |
||||||
|
def handle(self, event, ctx, bot): |
||||||
|
for plugin in self.plugins_loaded: |
||||||
|
if event in plugin.hooks: |
||||||
|
getattr(plugin, "run")(event, ctx, bot) |
@ -0,0 +1,19 @@ |
|||||||
|
""" |
||||||
|
the "hello world" (if you will) of the plugin system. |
||||||
|
there are only two required parts: |
||||||
|
* 'hooks': a list of dispatch events to run() on |
||||||
|
* run(event, ctx, bot): the function to run on-event, where event is the trigger, |
||||||
|
ctx is the 'd'(data) key of the response, and 'bot' is the current bot |
||||||
|
instance. |
||||||
|
the rest is up to you. |
||||||
|
this particular example listens for a message that contains the string 'dango' and |
||||||
|
returns a response, similar to the traditional 'hello' -> 'hello, world!' |
||||||
|
test interaction |
||||||
|
""" |
||||||
|
|
||||||
|
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") |
||||||
|
bot.send_msg(ctx["channel_id"], "to all the motherfuckers that shed a tear") |
Loading…
Reference in new issue