From 250df44778c5c8c60e5cf8801b2b07898ff46abc Mon Sep 17 00:00:00 2001 From: Benjamyn Love Date: Mon, 16 Mar 2020 17:18:33 +1100 Subject: [PATCH] Initial commit --- .gitignore | 3 ++ main.py | 103 +++++++++++++++++++++++++++++++++++++++++++++ plugin_example.py | 29 +++++++++++++ plugins/.gitignore | 1 + plugins/1.py | 31 ++++++++++++++ plugins/2.py | 19 +++++++++ plugins/3.py | 23 ++++++++++ plugins/4.py | 0 plugins/chat.py | 31 ++++++++++++++ 9 files changed, 240 insertions(+) create mode 100644 .gitignore create mode 100644 main.py create mode 100644 plugin_example.py create mode 100644 plugins/.gitignore create mode 100644 plugins/1.py create mode 100644 plugins/2.py create mode 100644 plugins/3.py create mode 100644 plugins/4.py create mode 100644 plugins/chat.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4914db9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +env/ +.vscode/ +__pycache__/ \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..bf4e9ae --- /dev/null +++ b/main.py @@ -0,0 +1,103 @@ +#!/usr/bin/python3 + +import importlib +from pathlib import Path +from pprint import pprint +import sys +import time +import random + +# List of loaded plugins +loadedPlugins = [] + +# Temp lists to store plugin info +tmpgenericHooks = [] +tmpmessageHooks = [] +tmpmentionHooks = [] + +# List of functions per hook + +genericHooks = [] +messageHooks = [] +mentionHooks = [] + +messages = [{"name":"1","message":"message1"}, {"name":"2","message":"message2"}, {"name":"3","message":"Hey @ben sup"}, {"name":"test","message":"Hello World"},{"name":"Ben","message":"@1 sup"}] + +def registerGenericHooks(): + # Run this to run the registered hooks in a plugin + for x in tmpgenericHooks: + for y in x[1]: + genericHooks.append(getattr(sys.modules[f"{x[0]}"], f"{y}")) + + +def registerMessageHooks(): + # Run this to run the registered hooks in a plugin + for x in tmpmessageHooks: + for y in x[1]: + messageHooks.append(getattr(sys.modules[f"{x[0]}"], f"{y}")) + + +def registerMentionHooks(): + # Run this to run the registered hooks in a plugin + for x in tmpmentionHooks: + for y in x[1]: + mentionHooks.append(getattr(sys.modules[f"{x[0]}"], f"{y}")) + + + +def buildHookList(): + # Buld Generic hook lists + for x in loadedPlugins: + try: + tmpgenericHooks.append(x.genericHooks()) + tmpmessageHooks.append(x.messageHooks()) + tmpmentionHooks.append(x.mentionHooks()) + except AttributeError as e: + print(f"{e}") + +# pprint(loadedPlugins) + +def loadPlugins(): + p = Path("plugins/") + files = [x for x in p.iterdir() if x.suffix == ".py"] + + for x in files: + mod = x.stem + loadedPlugins.append(importlib.import_module(f"plugins.{mod}")) + for x in loadedPlugins: + try: + x.pluginStart() + except AttributeError: + print(f"{x.__name__} failed to load as there is no pluginStart method") + +def runGenericHooks(): + for x in genericHooks: + x() + +def runMessageHooks(msg): + for x in messageHooks: + x(msg) + +def runMentionHooks(msg): + for x in mentionHooks: + x(msg) + +def init(): + loadPlugins() + buildHookList() + registerGenericHooks() + registerMessageHooks() + registerMentionHooks() + +# pprint(hooks) + +init() + +while True: + runGenericHooks() + msg = random.choice(messages) + runMessageHooks(msg) + if "@" in msg['message']: + print("running mentions") + runMentionHooks(msg) + time.sleep(1) \ No newline at end of file diff --git a/plugin_example.py b/plugin_example.py new file mode 100644 index 0000000..69cefa3 --- /dev/null +++ b/plugin_example.py @@ -0,0 +1,29 @@ +def pluginStart(): + print(f"Starting '{__name__}'") + +def genericHooks(): + print(f"Starting '{__name__}'") + funcList = ["exampleFunc"] + print(f"Registering hooks {funcList}") + return [__name__,funcList] + +def messageHooks(): + funcList = ["onMessage"] + print(f"Registering hooks {funcList}") + return [__name__,funcList] + +def mentionHooks(): + funcList = ["onMention"] + return [__name__,funcList] + +def exampleFunc(): + # Do code + pass + +def onMessage(msg): + # Do code + pass + +def onMention(msg): + # Do code + pass \ No newline at end of file diff --git a/plugins/.gitignore b/plugins/.gitignore new file mode 100644 index 0000000..ba0430d --- /dev/null +++ b/plugins/.gitignore @@ -0,0 +1 @@ +__pycache__/ \ No newline at end of file diff --git a/plugins/1.py b/plugins/1.py new file mode 100644 index 0000000..cb0971e --- /dev/null +++ b/plugins/1.py @@ -0,0 +1,31 @@ +import re + +def pluginStart(): + print(f"Starting '{__name__}'") + +def genericHooks(): + print(f"Starting '{__name__}'") + funcList = ["test1"] + print(f"Registering hooks {funcList}") + return [__name__,funcList] + +def messageHooks(): + funcList = ["onMessage"] + print(f"Registering hooks {funcList}") + return [__name__,funcList] + +def mentionHooks(): + funcList = ["onMention"] + return [__name__,funcList] + +def test1(): + internalFunc("test") + +def internalFunc(name): + print(f"Looks like this works running from: {name}") + +def onMessage(msg): + pass + +def onMention(msg): + pass \ No newline at end of file diff --git a/plugins/2.py b/plugins/2.py new file mode 100644 index 0000000..216f340 --- /dev/null +++ b/plugins/2.py @@ -0,0 +1,19 @@ +def pluginStart(): + print(f"Starting '{__name__}'") + + +def genericHooks(): + print(f"Starting '{__name__}'") + funcList = ["memes1"] + print(f"Registering hooks {funcList}") + return [__name__,funcList] + +def messageHooks(): + funcList = ["onMessage"] + return [__name__,funcList] + +def memes1(): + return "memes" + +def onMessage(msg): + print(f"Hey {msg['name']}, called from {__name__}") \ No newline at end of file diff --git a/plugins/3.py b/plugins/3.py new file mode 100644 index 0000000..07740dd --- /dev/null +++ b/plugins/3.py @@ -0,0 +1,23 @@ +import sys + +def pluginStart(): + print(f"Starting '{__name__}'") + +def genericHooks(): + print(f"Starting '{__name__}'") + funcList = ["test11"] + print(f"Registering hooks {funcList}") + return [__name__,funcList] + +def messageHooks(): + funcList = ["onMessage"] + print(f"Registering hooks {funcList}") + return [__name__,funcList] + +def test11(): + print("test11") + +def onMessage(msg): + if msg['name'] == "test": + print("Return test!") + return "Test was here" \ No newline at end of file diff --git a/plugins/4.py b/plugins/4.py new file mode 100644 index 0000000..e69de29 diff --git a/plugins/chat.py b/plugins/chat.py new file mode 100644 index 0000000..44b29fc --- /dev/null +++ b/plugins/chat.py @@ -0,0 +1,31 @@ +import re + +def pluginStart(): + print(f"Starting '{__name__}'") + +def genericHooks(): + print(f"Starting '{__name__}'") + funcList = [] + print(f"Registering hooks {funcList}") + return [__name__,funcList] + +def messageHooks(): + funcList = ["onMessage"] + print(f"Registering hooks {funcList}") + return [__name__,funcList] + +def mentionHooks(): + funcList = ["onMention"] + return [__name__,funcList] + +def exampleFunc(): + # Do code + pass + +def onMessage(msg): + print(f"From: {msg['name']}\nMessage: {msg['message']}") + +def onMention(msg): + mention = re.search(r"@.+? ", msg["message"]).group(0) + mention = mention.strip() + print(f"{mention.split('@')[1]} got mentioned") \ No newline at end of file