Adding listener plugin support

pull/11/head
Sina Mashek 2017-11-28 07:48:29 +02:00
parent d4f0edda00
commit f043ca28e0
2 changed files with 55 additions and 15 deletions

View File

@ -10,14 +10,19 @@ irc.client.ServerConnection.buffer_class.errors = 'replace'
class Message: class Message:
def __init__(self, channel, nick, cmd, arg, botnick, ops): def __init__(self, channel, nick, botnick, ops, cmd=None, arg=None, text=None):
self.channel = channel self.channel = channel
self.nick = nick self.nick = nick
self.cmd = cmd
self.arg = arg
self.botnick = botnick self.botnick = botnick
self.ops = ops self.ops = ops
if cmd:
self.cmd = cmd
if arg:
self.arg = arg
if text:
self.text = text
if not (cmd or text):
print('Please pass Message a command or text!')
class Bot(irc.bot.SingleServerIRCBot): class Bot(irc.bot.SingleServerIRCBot):
def __init__(self, channels, nickname, server, **kwargs): def __init__(self, channels, nickname, server, **kwargs):
@ -31,7 +36,6 @@ class Bot(irc.bot.SingleServerIRCBot):
self.bot_nick = nickname self.bot_nick = nickname
self.load_plugins() self.load_plugins()
def set_kwargs(self, **kwargs): def set_kwargs(self, **kwargs):
kwarguments = { kwarguments = {
'port': 6667, 'port': 6667,
@ -63,11 +67,14 @@ class Bot(irc.bot.SingleServerIRCBot):
plugins.append(p) plugins.append(p)
except Exception as e: except Exception as e:
print(e) print(e)
# gather all commands # gather all commands and listeners
self.cmds = {} self.cmds = {}
self.lstnrs = {}
for plugin in plugins: for plugin in plugins:
for cmd in plugin.pinhook.plugin.cmds: for cmd in plugin.pinhook.plugin.cmds:
self.cmds[cmd['cmd']] = cmd['func'] self.cmds[cmd['cmd']] = cmd['func']
for lstnr in plugin.pinhook.plugin.lstnrs:
self.lstnrs[lstnr['lstn']] = lstnr['func']
def on_welcome(self, c, e): def on_welcome(self, c, e):
if self.ns_pass: if self.ns_pass:
@ -76,17 +83,36 @@ class Bot(irc.bot.SingleServerIRCBot):
c.join(channel) c.join(channel)
def on_pubmsg(self, c, e): def on_pubmsg(self, c, e):
self.process_command(c, e, e.arguments[0]) self.process_message(c, e)
def on_privmsg(self, c, e): def on_privmsg(self, c, e):
self.process_command(c, e, e.arguments[0]) self.process_message(c, e)
def process_command(self, c, e, text): def process_message(self, c, e):
nick = e.source.nick nick = e.source.nick
if e.target == self.bot_nick: if e.target == self.bot_nick:
chan = nick chan = nick
else: else:
chan = e.target chan = e.target
if e.arguments[0].startswith('!'):
self.process_command(c, e, nick, chan, e.arguments[0])
else:
output = None
for lstnr in self.lstnrs:
try:
output = self.lstnrs[lstnr](Message(
channel=chan,
text=e.arguments[0],
nick=nick,
botnick=self.bot_nick,
ops=self.ops
))
if output:
self.process_output(c, chan, output)
except Exception as e:
print(e)
def process_command(self, c, e, nick, chan, text):
cmd = text.split(' ')[0] cmd = text.split(' ')[0]
if len(text.split(' ')) > 1: if len(text.split(' ')) > 1:
arg = ''.join([i + ' ' for i in text.split(' ')[1:]]).strip() arg = ''.join([i + ' ' for i in text.split(' ')[1:]]).strip()
@ -120,10 +146,12 @@ class Bot(irc.bot.SingleServerIRCBot):
print(e) print(e)
if output: if output:
for msg in output.msg: self.process_output(c, chan, output)
if output.msg_type == 'message':
c.privmsg(chan, msg)
elif output.msg_type == 'action':
c.action(chan, msg)
time.sleep(.5)
def process_output(self, c, chan, output):
for msg in output.msg:
if output.msg_type == 'message':
c.privmsg(chan, msg)
elif output.msg_type == 'action':
c.action(chan, msg)
time.sleep(.5)

View File

@ -1,4 +1,6 @@
cmds = [] cmds = []
lstnrs = []
class Output: class Output:
def __init__(self, msg_type, msg): def __init__(self, msg_type, msg):
@ -21,9 +23,19 @@ def add_plugin(command, func):
cmds.append({'cmd': command, 'func': func}) cmds.append({'cmd': command, 'func': func})
def add_listener(name, func):
lstnrs.append({'lstn': name, 'func': func})
def register(command): def register(command):
def register_for_command(func): def register_for_command(func):
add_plugin(command, func) add_plugin(command, func)
return func return func
return register_for_command return register_for_command
def listener(name):
def register_as_listener(func):
add_listener(name, func)
return func
return register_as_listener