Add plugin loading logic
This commit is contained in:
parent
8a1d8a57bd
commit
4d6048f2a4
20
bot.py
20
bot.py
|
@ -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()
|
||||
|
|
|
@ -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
|
|
@ -0,0 +1,5 @@
|
|||
import plugin
|
||||
|
||||
@plugin.command("test","Just a test")
|
||||
def test(bot,channel,nick,*args):
|
||||
print(bot,channel,nick,*args)
|
Loading…
Reference in New Issue