Add plugin loading logic

This commit is contained in:
Robert Miles 2018-12-09 12:53:44 -05:00
parent 8a1d8a57bd
commit 4d6048f2a4
3 changed files with 29 additions and 5 deletions

20
bot.py
View File

@ -1,4 +1,4 @@
import teambot, sys, traceback, requests, random, notes, words, markovuniverse
import teambot, sys, traceback, requests, random, notes, words, markovuniverse, imp, plugin
from titlecase import titlecase
BOTOP = "~minerobber@127.0.0.1"
@ -9,6 +9,12 @@ NOTEBOOK = notes.DatedNotebook()
NOTEBOOK.load("messages.json")
class MinerbotPhoenix(teambot.Handler):
def load_module(self,modname):
try:
fp, pathname, desc = imp.find_module(modname,["plugins"])
imp.load_module(modname,fp,pathname,desc)
except:
print("Unable to load plugin {}".format(modname))
def on_pubmsg(self,channel,nick,message):
if NOTEBOOK.checkNotes(nick):
for note in NOTEBOOK.readNotes(nick):
@ -19,9 +25,10 @@ class MinerbotPhoenix(teambot.Handler):
cmd = args.pop(0).lower()
self.cmd = cmd
# print(cmd,args,hasattr(self,"on_{}".format(cmd)))
if hasattr(self,"on_{}".format(cmd)):
if cmd in plugin.cmds:
try:
getattr(self,"on_{}".format(cmd))(channel,nick,*args)
# getattr(self,"on_{}".format(cmd))(channel,nick,*args)
plugin.cmds[cmd](self,channel,nick,*args)
except ValueError:
self.say(channel,"Usage: {}{} {}".format(PREFIX,cmd,HELP.get(cmd,"")))
except Exception as e:
@ -95,8 +102,11 @@ class MinerbotPhoenix(teambot.Handler):
self.say(channel,nick+": https://tildegit.org/khuxkm/minerbot2")
if __name__=="__main__":
words.loadDict("american-english-small")
words.loadDict("words")
# print(list(filter(lambda x: x.startswith("on_"),dir(MinerbotPhoenix))))
channels = "#tildetown #bots #mafia #counting-meta".split()
channels = "#khuxkm-bots".split()
bot = teambot.TeamBot(channels,"minerbot","localhost",chandler=MinerbotPhoenix)
plugin.cmds["admin"] = MinerbotPhoenix.on_admin
plugin.cmds["rollcall"] = MinerbotPhoenix.on_rollcall
bot.handler.load_module("test")
bot.start()

9
plugin.py Normal file
View File

@ -0,0 +1,9 @@
cmds = {}
help = {}
def command(name,helptext="No help available for this command."):
def _register_cmd(func):
cmds[name]=func
help[name]=helptext
return func
return _register_cmd

5
plugins/test.py Normal file
View File

@ -0,0 +1,5 @@
import plugin
@plugin.command("test","Just a test")
def test(bot,channel,nick,*args):
print(bot,channel,nick,*args)