Split long messages solely by length not line count.
authorCMDR furrycat <elite@furrycat.net>
Tue, 15 Nov 2016 12:14:30 +0000 (12:14 +0000)
committerCMDR furrycat <elite@furrycat.net>
Tue, 15 Nov 2016 12:14:30 +0000 (12:14 +0000)
bot.py

diff --git a/bot.py b/bot.py
index 067b7f9..9be7651 100644 (file)
--- a/bot.py
+++ b/bot.py
@@ -285,16 +285,22 @@ def say(channel, text, *, attachment = None, wake = None):
   return message
 
 @asyncio.coroutine
-def say_many(channel, lines, n = 25):
+def say_many(channel, lines, *, maxlen = 2000):
   while len(lines):
+    message_len = 0
+    n = 0
+    for i in range(0, len(lines)):
+      message_len += len(lines[i])
+      if message_len > maxlen:
+        break
+      n += 1
     # We can't print too big a message.
     subset = lines[:n]
     message = '\n'.join(subset)
-    if len(message) > 2000 and n > 1:
-      n -= 1
-      continue
+    result = yield from say(channel, message)
+    if not result:
+      break
     lines = lines[len(subset):]
-    yield from say(channel, message)
 
 def voice_channel_for_channel(channel):
   for voice in client.voice_clients: