From 250305725e0846b0f58339f5df51916e8e5993d7 Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Thu, 28 Mar 2019 12:29:50 -0400 Subject: [PATCH] Allow bot to utilize multiple destinations at one time --- sopel/bot.py | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/sopel/bot.py b/sopel/bot.py index 7e0fefc866..a55be5eb47 100644 --- a/sopel/bot.py +++ b/sopel/bot.py @@ -267,14 +267,18 @@ def join(self, channel, password=None): else: self.write(['JOIN', channel, password]) - def msg(self, recipient, text, max_messages=1): + def msg(self, recipients, text, max_messages=1): # Deprecated, but way too much of a pain to remove. - self.say(text, recipient, max_messages) - def say(self, text, recipient, max_messages=1): - """Send ``text`` as a PRIVMSG to ``recipient``. + if not isinstance(recipients, list): + recipients = [recipients] + recipients = ','.join(str(x) for x in recipients) + self.say(text, recipients, max_messages) - In the context of a triggered callable, the ``recipient`` defaults to + def say(self, text, recipients, max_messages=1): + """Send ``text`` as a PRIVMSG to ``recipients``. + + In the context of a triggered callable, the ``recipients`` defaults to the channel (or nickname, if a private message) from which the message was received. @@ -297,13 +301,17 @@ def say(self, text, recipient, max_messages=1): # Manage multi-line only when needed text, excess = tools.get_sendable_message(text) + if not isinstance(recipients, list): + recipients = [recipients] + recipients = ','.join(str(x) for x in recipients) + try: self.sending.acquire() # No messages within the last 3 seconds? Go ahead! # Otherwise, wait so it's been at least 0.8 seconds + penalty - recipient_id = Identifier(recipient) + recipient_id = Identifier(recipients) if recipient_id not in self.stack: self.stack[recipient_id] = [] @@ -326,7 +334,7 @@ def say(self, text, recipient, max_messages=1): # If we said '...' 3 times, discard message return - self.write(('PRIVMSG', recipient), text) + self.write(('PRIVMSG', recipients), text) self.stack[recipient_id].append((time.time(), self.safe(text))) self.stack[recipient_id] = self.stack[recipient_id][-10:] finally: @@ -334,18 +342,21 @@ def say(self, text, recipient, max_messages=1): # Now that we've sent the first part, we need to send the rest. Doing # this recursively seems easier to me than iteratively if excess: - self.msg(recipient, excess, max_messages - 1) + self.msg(recipients, excess, max_messages - 1) - def notice(self, text, dest): + def notice(self, text, dests): """Send an IRC NOTICE to a user or a channel. Within the context of a triggered callable, ``dest`` will default to the channel (or nickname, if a private message), in which the trigger happened. """ - self.write(('NOTICE', dest), text) + if not isinstance(dests, list): + dests = [dests] + dests = ','.join(str(x) for x in dests) + self.write(('NOTICE', dests), text) - def action(self, text, dest): + def action(self, text, dests): """Send ``text`` as a CTCP ACTION PRIVMSG to ``dest``. The same loop detection and length restrictions apply as with @@ -355,9 +366,12 @@ def action(self, text, dest): the channel (or nickname, if a private message), in which the trigger happened. """ - self.say('\001ACTION {}\001'.format(text), dest) + if not isinstance(dests, list): + dests = [dests] + dests = ','.join(str(x) for x in dests) + self.say('\001ACTION {}\001'.format(text), dests) - def reply(self, text, dest, reply_to, notice=False): + def reply(self, text, dests, reply_to, notice=False): """Prepend ``reply_to`` to ``text``, and send as a PRIVMSG to ``dest``. If ``notice`` is ``True``, send a NOTICE rather than a PRIVMSG. @@ -371,10 +385,13 @@ def reply(self, text, dest, reply_to, notice=False): happened. """ text = '%s: %s' % (reply_to, text) + if not isinstance(dests, list): + dests = [dests] + dests = ','.join(str(x) for x in dests) if notice: - self.notice(text, dest) + self.notice(text, dests) else: - self.say(text, dest) + self.say(text, dests) class SopelWrapper(object): def __init__(self, sopel, trigger):