From: zonidjan Date: Wed, 4 Oct 2017 23:18:01 +0000 (-0500) Subject: add wantchan - fixes #13 - will consume channel if available, but not require it X-Git-Url: https://jfr.im/git/erebus.git/commitdiff_plain/827ec8f0367a9482714aeddc6f7e7eef56dee417?ds=sidebyside add wantchan - fixes #13 - will consume channel if available, but not require it --- diff --git a/bot.py b/bot.py index d1eed6c..060c172 100644 --- a/bot.py +++ b/bot.py @@ -270,37 +270,41 @@ class Bot(object): if msg == "VERSION": self.msg(user, "\001VERSION Erebus v%d.%d - http://github.com/zonidjan/erebus" % (self.parent.APIVERSION, self.parent.RELEASE)) return - if len(pieces) > 1: - chanword = pieces[1] - if chanword.startswith('#'): - chanparam = self.parent.channel(chanword) if target != self.nick: # message was sent to a channel - chan = self.parent.channel(target) try: if msg.startswith('*'): # message may be addressed to bot by "*BOTNICK" trigger? if pieces[0][1:].lower() == self.nick.lower(): pieces.pop(0) # command actually starts with next word msg = ' '.join(pieces) # command actually starts with next word - elif not triggerused: - if self.parent.haschanhook(target.lower()): - for callback in self.parent.getchanhook(target.lower()): - try: - cbret = callback(self, user, chan, *pieces) - except NotImplementedError: - self.msg(user, "Command not implemented.") - except: - self.msg(user, "Command failed. Code: CBEXC%09.3f" % (time.time() % 100000)) - self.__debug_cbexception("chanhook", user=user, target=target, msg=msg) - return # not to bot, don't process! + triggerused = True except IndexError: return # "message" is empty + if len(pieces) > 1: + chanword = pieces[1] + if chanword.startswith('#'): + chanparam = self.parent.channel(chanword) + + if target != self.nick: # message was sent to a channel + chan = self.parent.channel(target) + if not triggerused: + if self.parent.haschanhook(target.lower()): + for callback in self.parent.getchanhook(target.lower()): + try: + cbret = callback(self, user, chan, *pieces) + except NotImplementedError: + self.msg(user, "Command not implemented.") + except: + self.msg(user, "Command failed. Code: CBEXC%09.3f" % (time.time() % 100000)) + self.__debug_cbexception("chanhook", user=user, target=target, msg=msg) + return # not to bot, don't process! + cmd = pieces[0].lower() rancmd = False if self.parent.hashook(cmd): for callback in self.parent.gethook(cmd): - if chanparam is not None and callback.needchan: + if chanparam is not None and (callback.needchan or callback.wantchan): chan = chanparam pieces.pop(1) if chan is None and callback.needchan: diff --git a/modlib.py b/modlib.py index 7225ac7..ff63a07 100644 --- a/modlib.py +++ b/modlib.py @@ -100,7 +100,8 @@ class modlib(object): return func return realhook - def hook(self, cmd=None, needchan=True, glevel=ANYONE, clevel=PUBLIC): + def hook(self, cmd=None, needchan=True, glevel=ANYONE, clevel=PUBLIC, wantchan=None): + if wantchan is None: wantchan = needchan _cmd = cmd #save this since it gets wiped out... def realhook(func): cmd = _cmd #...and restore it @@ -110,6 +111,7 @@ class modlib(object): cmd = (cmd,) func.needchan = needchan + func.wantchan = wantchan func.reqglevel = glevel func.reqclevel = clevel func.cmd = cmd diff --git a/modules/control.py b/modules/control.py index 34018fc..bd2460c 100644 --- a/modules/control.py +++ b/modules/control.py @@ -99,7 +99,7 @@ def _whois(user, chan, showglevel=True, showclevel=True): fmt += " (not a channel user)" return fmt % fillers -@lib.hook(needchan=False) +@lib.hook(needchan=False, wantchan=True) @lib.help("", "shows who someone is") @lib.argsEQ(1) def whois(bot, user, chan, realtarget, *args):