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:
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.nick = nick
self.cmd = cmd
self.arg = arg
self.botnick = botnick
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):
def __init__(self, channels, nickname, server, **kwargs):
@ -31,7 +36,6 @@ class Bot(irc.bot.SingleServerIRCBot):
self.bot_nick = nickname
self.load_plugins()
def set_kwargs(self, **kwargs):
kwarguments = {
'port': 6667,
@ -63,11 +67,14 @@ class Bot(irc.bot.SingleServerIRCBot):
plugins.append(p)
except Exception as e:
print(e)
# gather all commands
# gather all commands and listeners
self.cmds = {}
self.lstnrs = {}
for plugin in plugins:
for cmd in plugin.pinhook.plugin.cmds:
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):
if self.ns_pass:
@ -76,17 +83,36 @@ class Bot(irc.bot.SingleServerIRCBot):
c.join(channel)
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):
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
if e.target == self.bot_nick:
chan = nick
else:
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]
if len(text.split(' ')) > 1:
arg = ''.join([i + ' ' for i in text.split(' ')[1:]]).strip()
@ -120,10 +146,12 @@ class Bot(irc.bot.SingleServerIRCBot):
print(e)
if 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)
self.process_output(c, chan, output)
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 = []
lstnrs = []
class Output:
def __init__(self, msg_type, msg):
@ -21,9 +23,19 @@ def add_plugin(command, func):
cmds.append({'cmd': command, 'func': func})
def add_listener(name, func):
lstnrs.append({'lstn': name, 'func': func})
def register(command):
def register_for_command(func):
add_plugin(command, func)
return func
return register_for_command
def listener(name):
def register_as_listener(func):
add_listener(name, func)
return func
return register_as_listener