ircbot: reply() method that supports recursion

undecided if i'll bother to bring recursion back, but if it works out,
provide this method to either reply or give text to the thing recursing.
either way this is a bit clearer than using privmsg() directly, usually
This commit is contained in:
Brian S. Stephan 2015-05-15 17:35:03 -05:00
parent 94656b71d2
commit 07ca50dfb6
3 changed files with 45 additions and 10 deletions

View File

@ -492,6 +492,41 @@ class IRCBot(irc.client.SimpleIRCClient):
else:
self.connection.send_raw("PRIVMSG {0:s} :{1:s}".format(target, text))
def reply(self, event, replystr, stop=False):
"""Reply over IRC to replypath or return a string with the reply.
The primary utility for this is to properly handle recursion. The
recursion code will set up a couple hints that this method
picks up on and will appropriately send an IRC event or return a
string.
Unless you know what you are doing, the modules you write should use
this method rather than send a privmsg reply, as failing to call this
method will certainly have recursion do odd things with your module.
Args:
event incoming event
replystr the message to reply with
stop whether or not to let other handlers see this
Returns:
The replystr if the event is inside recursion, or, potentially,
"NO MORE" to stop other event handlers from acting.
"""
replypath = ircbotlib.reply_destination_for_event(event)
if replystr is not None:
recursing = getattr(event, '_recursing', False)
if recursing:
return replystr
else:
replies = replystr.split('\n')
for reply in replies:
self.privmsg(replypath, reply)
if stop:
return "NO MORE"
def start(self):
"""Start the bot."""

View File

@ -53,7 +53,7 @@ class ChannelManagement(Plugin):
chan_mod, c = IrcChannel.objects.get_or_create(name=channel)
log.debug(u"joining channel %s", channel)
self.connection.join(channel)
self.bot.privmsg(dest, "Joined channel {0:s}.".format(channel))
self.bot.reply(event, "Joined channel {0:s}.".format(channel))
def handle_part(self, connection, event, match):
"""Handle the join command."""
@ -66,7 +66,7 @@ class ChannelManagement(Plugin):
chan_mod, c = IrcChannel.objects.get_or_create(name=channel)
log.debug(u"parting channel %s", channel)
self.connection.part(channel)
self.bot.privmsg(dest, "Parted channel {0:s}.".format(channel))
self.bot.reply(event, "Parted channel {0:s}.".format(channel))
def handle_quit(self, connection, event, match):
"""Handle the join command."""

View File

@ -55,18 +55,18 @@ class Markov(Plugin):
# the speaker
topics = [x for x in addressed_re.match(what).group(1).split(' ') if len(x) >= 3]
self.bot.privmsg(target, u"{0:s}: {1:s}"
u"".format(nick, u" ".join(markovlib.generate_line(context,
topics=topics,
max_sentences=1))))
self.bot.reply(event, u"{0:s}: {1:s}"
u"".format(nick, u" ".join(markovlib.generate_line(context,
topics=topics,
max_sentences=1))))
else:
# i wasn't addressed directly, so just respond
topics = [x for x in what.split(' ') if len(x) >= 3]
self.bot.privmsg(target, u"{0:s}"
u"".format(u" ".join(markovlib.generate_line(context,
topics=topics,
max_sentences=1))))
self.bot.reply(event, u"{0:s}"
u"".format(u" ".join(markovlib.generate_line(context,
topics=topics,
max_sentences=1))))
plugin = Markov