Skip to content

Commit

Permalink
Allow bot to utilize multiple destinations at one time
Browse files Browse the repository at this point in the history
  • Loading branch information
deathbybandaid committed Mar 30, 2019
1 parent 5197450 commit 2503057
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions sopel/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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] = []
Expand All @@ -326,26 +334,29 @@ 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:
self.sending.release()
# 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
Expand All @@ -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.
Expand All @@ -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):
Expand Down

0 comments on commit 2503057

Please sign in to comment.