]> jfr.im git - erebus.git/commitdiff
add `bot.reply` function and hook return shortcut
authorzonidjan <redacted>
Sun, 12 Aug 2018 09:31:05 +0000 (04:31 -0500)
committerzonidjan <redacted>
Sun, 12 Aug 2018 09:31:05 +0000 (04:31 -0500)
bot.reply(chan, user, reply_msg) will now send a reply.
This is meant to be used directly with the chan and user objects passed to a hook.
This is bot.msg(user, reply_msg) when chan is None (i.e. when used via PM or when wantchan=False);
or bot.msg(chan, "user: reply_msg") when chan is not None (i.e. when used in channel or via PM with wantchan=True and channel provided)

Returning a string (Py2: str or unicode) from a hook will now result in a bot.reply.

bot.py

diff --git a/bot.py b/bot.py
index 610c6cda694cd7d8d9eaae0ed4c3ff080240ceb1..d56be6f94e9bcf50229766494c0f9dc23a605919 100644 (file)
--- a/bot.py
+++ b/bot.py
@@ -11,13 +11,19 @@ MAXLEN = 400 # arbitrary max length of a command generated by Bot.msg functions
 
 if sys.version_info.major < 3:
        timerbase = threading._Timer
+       stringbase = basestring
 else:
        timerbase = threading.Timer
+       stringbase = str
 class MyTimer(timerbase):
        def __init__(self, *args, **kwargs):
                timerbase.__init__(self, *args, **kwargs)
                self.daemon = True
 
+if sys.version_info.major < 3:
+       stringbase = basestring
+else:
+       stringbase = str
 
 #bots = {'erebus': bot.Bot(nick='Erebus', user='erebus', bind='', server='irc.quakenet.org', port=6667, realname='Erebus')}
 class Bot(object):
@@ -329,6 +335,8 @@ class Bot(object):
                                                cbret = callback(self, user, chan, target, *pieces[1:])
                                                if cbret is NotImplemented:
                                                        raise NotImplementedError
+                                               elif isinstance(cbret, stringbase):
+                                                       self.reply(chan, user, cbret)
                                        except NotImplementedError:
                                                self.msg(user, "Command not implemented.")
                                        except Exception:
@@ -353,6 +361,13 @@ class Bot(object):
 #                      print "%09.3f %s [!] %s" % (time.time() % 100000, self.nick, "!!! NOMSG")
                        __import__('traceback').print_stack()
 
+
+       def reply(self, chan, user, msg):
+               if chan is not None:
+                       self.msg(chan, "%s: %s" % (user, msg))
+               else:
+                       self.msg(user, msg)
+
        def msg(self, target, msg):
                if self.parent.cfg.getboolean('erebus', 'nofakelag'): return self.fastmsg(target, msg)
                cmd = self._formatmsg(target, msg)