Handle failed send in all cases.
authorCMDR furrycat <elite@furrycat.net>
Thu, 25 Jul 2019 13:58:00 +0000 (14:58 +0100)
committerCMDR furrycat <elite@furrycat.net>
Thu, 25 Jul 2019 13:58:00 +0000 (14:58 +0100)
bot.py

diff --git a/bot.py b/bot.py
index 3ba0253..7dea3ba 100644 (file)
--- a/bot.py
+++ b/bot.py
@@ -507,18 +507,22 @@ def parse_attachment(attachment, filename = None):
   return parsed
 
 async def maybe_say(channel, text, *, attachment = None, embed = None, probability = 0.2, wake = True):
+  destination = 'private channel [{}]'.format(', '.join(user.name, user.discriminator) for user in channel.recipients if user.id != channel.me.id) if channel.is_private else 'channel {} on {}'.format(channel.name, channel.server.name)
   if random.random() <= probability:
-    await client.send_typing(channel)
-    message = None
-    if attachment is not None:
-      parsed = parse_attachment(attachment)
-      if parsed is not None:
-        message = await client.send_file(channel, parsed['fd'], filename = parsed['filename'], content = text)
-        parsed['fd'].close()
-      else:
-        log.warning("Missing or invalid attachment!")
-    if message is None:
-      message = await client.send_message(channel, text, embed = embed)
+    try:
+      message = None
+      await client.send_typing(channel)
+      if attachment is not None:
+        parsed = parse_attachment(attachment)
+        if parsed is not None:
+          message = await client.send_file(channel, parsed['fd'], filename = parsed['filename'], content = text)
+          parsed['fd'].close()
+        else:
+          log.warning("Missing or invalid attachment!")
+      if message is None:
+        message = await client.send_message(channel, text, embed = embed)
+    except Exception:
+      log.error('sending to {}: {}'.format(destination, traceback.format_exc().splitlines()[-1]))
     if wake:
       await wake_up()
     return message
@@ -538,7 +542,6 @@ async def say(channel, text, *, attachment = None, embed = None, wake = None):
   return message
 
 async def say_many(channel, lines, *, attachment = None, embed = None, maxlen = 2000):
-  destination = 'private channel [{}]'.format(', '.join(user.name, user.discriminator) for user in channel.recipients if user.id != channel.me.id) if channel.is_private else 'channel {} on {}'.format(channel.name, channel.server.name)
   while len(lines):
     message_len = 0
     n = 0
@@ -553,11 +556,7 @@ async def say_many(channel, lines, *, attachment = None, embed = None, maxlen =
     # We can't print too big a message.
     subset = lines[:n]
     message = '\n'.join(subset)
-    try:
-      result = await say(channel, message, attachment = attachment, embed = embed)
-    except Exception:
-      log.error('sending to {}: {}'.format(destination, traceback.format_exc().splitlines()[-1]))
-      result = None
+    result = await say(channel, message, attachment = attachment, embed = embed)
     if not result:
       break
     lines = lines[len(subset):]