Support voice only announcements.
authorCMDR furrycat <elite@furrycat.net>
Wed, 5 Oct 2016 11:22:13 +0000 (12:22 +0100)
committerCMDR furrycat <elite@furrycat.net>
Wed, 5 Oct 2016 11:22:13 +0000 (12:22 +0100)
bot.py

diff --git a/bot.py b/bot.py
index 274b2dc..e5b6d86 100755 (executable)
--- a/bot.py
+++ b/bot.py
@@ -821,7 +821,8 @@ def show_announcement(message, id):
         text += ' tell @{}'.format(mention)
       else:
         text += ' tell <@{}>'.format(mention)
-  text += ' in <#{}>'.format(announcement['channel_id'])
+  if announcement['channel_id'] != announcement['voice_id']:
+    text += ' in <#{}>'.format(announcement['channel_id'])
   if announcement['voice_id']:
     text += ' voice <#{}>'.format(announcement['voice_id'])
     if announcement['sound']:
@@ -829,9 +830,11 @@ def show_announcement(message, id):
   if announcement['probability'] < 1.0:
     text += ' probability {}'.format(announcement['probability'])
 
-  text += ':'
+  if announcement['message']:
+    text += ':'
   lines.append(text)
-  lines.append(announcement['message'])
+  if announcement['message']:
+    lines.append(announcement['message'])
 
   yield from say(message.channel, '\n'.join(lines))
 
@@ -878,33 +881,46 @@ def announce(announcement):
     log.warning("Can't get channel for announcement {}".format(announcement['message']))
     return
 
-  # @everyone mention mangles the digest
-  text = announcement['message']
-  mention = announcement['mention']
-  if mention:
-    if mention in ['everyone', 'here']:
-      text = '@{}\n{}'.format(mention, text)
-    else:
-      text = '<@{}>\n{}'.format(mention, text)
+  voice_only = announcement['voice_id'] == announcement['channel_id']
+  if not voice_only:
+    # @everyone mention mangles the digest
+    text = announcement['message']
+    mention = announcement['mention']
+    if mention:
+      if mention in ['everyone', 'here']:
+        text = '@{}\n{}'.format(mention, text)
+      else:
+        text = '<@{}>\n{}'.format(mention, text)
+
+    cutoff = datetime.datetime.utcnow() - datetime.timedelta(0, announcement['interval'])
+    logs = yield from client.logs_from(channel, after = cutoff)
+    digest = announcement['digest']
+    for message in logs:
+      raw = re.sub(r'^(<?@\S+\s)+', '', message.content).strip()
+      if hashlib.sha224(raw.encode('utf-8')).hexdigest() == digest:
+        log.debug('Saw previously posted announcement with digest {}'.format(digest))
+        return
 
-  cutoff = datetime.datetime.utcnow() - datetime.timedelta(0, announcement['interval'])
-  logs = yield from client.logs_from(channel, after = cutoff)
-  digest = announcement['digest']
-  for message in logs:
-    raw = re.sub(r'^(<?@\S+\s)+', '', message.content).strip()
-    if hashlib.sha224(raw.encode('utf-8')).hexdigest() == digest:
-      log.debug('Saw previously posted announcement with digest {}'.format(digest))
-      return
+  if announcement['voice_id']:
+    filename = announcement['sound']
+    if not filename:
+      filename = 'meow.wav'
 
   try:
     if dryrun:
-      log.info('Dryrun: Not posting to {}: {}'.format(channel, text))
+      if voice_only:
+        log.info('Dryrun: Not playing announcement {} in {}'.format(filename, channel))
+      else:
+        log.info('Dryrun: Not posting to {}: {}'.format(channel, text))
     else:
       announce = random.random() <= announcement['probability']
       if announce:
-        log.info('Posting to {}: {}'.format(channel, text))
-        message = yield from say(channel, text)
-        digest = hashlib.sha224(message.content.encode('utf-8')).hexdigest()
+        if voice_only:
+          digest = None
+        else:
+          log.info('Posting to {}: {}'.format(channel, text))
+          message = yield from say(channel, text)
+          digest = hashlib.sha224(message.content.encode('utf-8')).hexdigest()
       else:
         log.info("Didn't bother with announcement {}".format(announcement['id']))
       # Set last_spoke even if we chose not to announce.
@@ -912,9 +928,7 @@ def announce(announcement):
       if not announce:
         return
       if announcement['voice_id']:
-        filename = announcement['sound']
-        if not filename:
-          filename = 'meow.wav'
+        log.info('Playing announcement {} in {}'.format(filename, channel))
         yield from play_sound(client.get_channel(str(announcement['voice_id'])), filename)
   except:
     pass