Add listener plugin support (#11)
* Adding listener plugin support * Exposing number of nicks in channel to listeners * Expanded number of nicks to be nick list for all plugins * Change to check plugin command list * resolving conflict * Commands and listeners work properly.pull/16/head
parent
2d1472c739
commit
5b10d64eb8
|
@ -11,13 +11,20 @@ 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, nick_list=None):
|
||||||
self.channel = channel
|
self.channel = channel
|
||||||
self.nick = nick
|
self.nick = nick
|
||||||
self.cmd = cmd
|
self.nick_list = nick_list
|
||||||
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):
|
||||||
|
@ -32,7 +39,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,
|
||||||
|
@ -66,11 +72,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:
|
||||||
|
@ -79,13 +88,14 @@ 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_command(c, e)
|
||||||
|
|
||||||
def on_privmsg(self, c, e):
|
def on_privmsg(self, c, e):
|
||||||
self.process_command(c, e, e.arguments[0])
|
self.process_command(c, e)
|
||||||
|
|
||||||
def process_command(self, c, e, text):
|
def process_command(self, c, e):
|
||||||
nick = e.source.nick
|
nick = e.source.nick
|
||||||
|
text = e.arguments[0]
|
||||||
if e.target == self.bot_nick:
|
if e.target == self.bot_nick:
|
||||||
chan = nick
|
chan = nick
|
||||||
else:
|
else:
|
||||||
|
@ -114,19 +124,36 @@ class Bot(irc.bot.SingleServerIRCBot):
|
||||||
output = self.cmds[cmd](Message(
|
output = self.cmds[cmd](Message(
|
||||||
channel=chan,
|
channel=chan,
|
||||||
cmd=cmd,
|
cmd=cmd,
|
||||||
|
nick_list=list(self.channels[chan].users()),
|
||||||
nick=nick,
|
nick=nick,
|
||||||
arg=arg,
|
arg=arg,
|
||||||
botnick=self.bot_nick,
|
botnick=self.bot_nick,
|
||||||
ops=self.ops
|
ops=self.ops
|
||||||
))
|
))
|
||||||
|
if output:
|
||||||
|
self.process_output(c, chan, output)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
else:
|
||||||
|
for lstnr in self.lstnrs:
|
||||||
|
try:
|
||||||
|
output = self.lstnrs[lstnr](Message(
|
||||||
|
channel=chan,
|
||||||
|
text=text,
|
||||||
|
nick_list=list(self.channels[chan].users()),
|
||||||
|
nick=nick,
|
||||||
|
botnick=self.bot_nick,
|
||||||
|
ops=self.ops
|
||||||
|
))
|
||||||
|
if output:
|
||||||
|
self.process_output(c, chan, output)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
if output:
|
def process_output(self, c, chan, output):
|
||||||
for msg in output.msg:
|
for msg in output.msg:
|
||||||
if output.msg_type == 'message':
|
if output.msg_type == 'message':
|
||||||
c.privmsg(chan, msg)
|
c.privmsg(chan, msg)
|
||||||
elif output.msg_type == 'action':
|
elif output.msg_type == 'action':
|
||||||
c.action(chan, msg)
|
c.action(chan, msg)
|
||||||
time.sleep(.5)
|
time.sleep(.5)
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
cmds = []
|
cmds = []
|
||||||
|
lstnrs = []
|
||||||
|
|
||||||
|
|
||||||
class Output:
|
class Output:
|
||||||
def __init__(self, msg_type, msg):
|
def __init__(self, msg_type, msg):
|
||||||
|
@ -6,7 +8,10 @@ class Output:
|
||||||
self.msg = self.sanitize(msg)
|
self.msg = self.sanitize(msg)
|
||||||
|
|
||||||
def sanitize(self, msg):
|
def sanitize(self, msg):
|
||||||
|
try:
|
||||||
return msg.splitlines()
|
return msg.splitlines()
|
||||||
|
except AttributeError:
|
||||||
|
return msg
|
||||||
|
|
||||||
|
|
||||||
def action(msg):
|
def action(msg):
|
||||||
|
@ -25,9 +30,19 @@ def clear_plugins():
|
||||||
cmds.clear()
|
cmds.clear()
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
|
Loading…
Reference in New Issue