add plugin capability

pull/1/head
Mallory Hancock 2017-09-27 14:55:00 -07:00
parent a55e1ac495
commit 604b2f878a
2 changed files with 46 additions and 2 deletions

View File

@ -1,15 +1,32 @@
import imp
import os
import irc.bot
irc.client.ServerConnection.buffer_class.errors = 'replace'
class Bot(irc.bot.SingleServerIRCBot):
def __init__(self, channels, nickname, server, port=6667, ops=[]):
def __init__(self, channels, nickname, server, port=6667, ops=[], plugin_dir='plugins'):
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
self.chanlist = channels
self.bot_nick = nickname
self.ops = ops
# load all plugins
plugins = []
for m in os.listdir(plugin_dir):
if m.endswith('.py'):
name = m[:-3]
fp, pathname, description = imp.find_module(name, [plugin_dir])
plugins.append(imp.load_module(name, fp, pathname, description))
# gather all commands
self.cmds = {}
for plugin in plugins:
for cmd in plugin.pinhook.plugin.cmds:
self.cmds[cmd['cmd']] = cmd['func']
def on_welcome(self, c, e):
for channel in self.chanlist:
c.join(channel)
@ -31,11 +48,19 @@ class Bot(irc.bot.SingleServerIRCBot):
arg = ''.join([i + ' ' for i in text.split(' ')[1:]]).strip()
else:
arg = ''
output = ()
output = None
if cmd == '!join' and nick in self.ops:
c.join(arg)
c.privmsg(chan, '{}: joined {}'.format(nick, arg))
elif cmd == '!quit' and nick in self.ops:
c.quit("See y'all later!")
quit()
elif cmd in self.cmds:
output = self.cmds[cmd](nick=nick, arg=arg)
if output:
if output.msg_type == 'message':
c.privmsg(chan, output.msg)
elif output.msg_type == 'action':
c.action(chan, output.msg)

19
pinhook/plugin.py 100644
View File

@ -0,0 +1,19 @@
cmds = []
class Output:
def __init__(self, msg_type, msg):
self.msg_type = msg_type
self.msg = msg
def action(msg):
return Output('action', msg)
def message(msg):
return Output('message', msg)
def add_plugin(command, func):
cmds.append({'cmd': command, 'func': func})