add plugin capability
parent
a55e1ac495
commit
604b2f878a
|
@ -1,15 +1,32 @@
|
||||||
|
import imp
|
||||||
|
import os
|
||||||
|
|
||||||
import irc.bot
|
import irc.bot
|
||||||
|
|
||||||
irc.client.ServerConnection.buffer_class.errors = 'replace'
|
irc.client.ServerConnection.buffer_class.errors = 'replace'
|
||||||
|
|
||||||
|
|
||||||
class Bot(irc.bot.SingleServerIRCBot):
|
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)
|
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
|
||||||
self.chanlist = channels
|
self.chanlist = channels
|
||||||
self.bot_nick = nickname
|
self.bot_nick = nickname
|
||||||
self.ops = ops
|
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):
|
def on_welcome(self, c, e):
|
||||||
for channel in self.chanlist:
|
for channel in self.chanlist:
|
||||||
c.join(channel)
|
c.join(channel)
|
||||||
|
@ -31,11 +48,19 @@ class Bot(irc.bot.SingleServerIRCBot):
|
||||||
arg = ''.join([i + ' ' for i in text.split(' ')[1:]]).strip()
|
arg = ''.join([i + ' ' for i in text.split(' ')[1:]]).strip()
|
||||||
else:
|
else:
|
||||||
arg = ''
|
arg = ''
|
||||||
output = ()
|
output = None
|
||||||
if cmd == '!join' and nick in self.ops:
|
if cmd == '!join' and nick in self.ops:
|
||||||
c.join(arg)
|
c.join(arg)
|
||||||
c.privmsg(chan, '{}: joined {}'.format(nick, arg))
|
c.privmsg(chan, '{}: joined {}'.format(nick, arg))
|
||||||
elif cmd == '!quit' and nick in self.ops:
|
elif cmd == '!quit' and nick in self.ops:
|
||||||
c.quit("See y'all later!")
|
c.quit("See y'all later!")
|
||||||
quit()
|
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)
|
||||||
|
|
||||||
|
|
|
@ -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})
|
||||||
|
|
Loading…
Reference in New Issue