Purr and meow in voice channels.
authorCMDR furrycat <elite@furrycat.net>
Fri, 30 Sep 2016 07:37:08 +0000 (08:37 +0100)
committerCMDR furrycat <elite@furrycat.net>
Fri, 30 Sep 2016 07:37:08 +0000 (08:37 +0100)
bot.py
meow.wav [new file with mode: 0644]
purr.wav [new file with mode: 0644]

diff --git a/bot.py b/bot.py
index e6b768f..f23eda4 100755 (executable)
--- a/bot.py
+++ b/bot.py
@@ -44,6 +44,7 @@ log = logging.getLogger('discord')
 
 idletime = multiprocessing.Value('i', 7200)
 rsstime = multiprocessing.Value('i', 600)
+playing = multiprocessing.Value('b', False)
 feeds = [
   { 'channel': 230340587474255872, 'description': 'GalNet', 'url': 'http://proxy.gonegeeky.com/edproxy/', 'date': False },
   { 'channel': 230340639458459649, 'description': 'Dev tracker', 'url': 'https://miggy.org/games/elite-dangerous/devtracker/ed-dev-posts.rss', 'summary': True },
@@ -52,8 +53,8 @@ feeds = [
 ]
 
 greetings = [
-  { 'channel': 222685317885329408, 'message': 'Welcome to Delta Squadron, take a look at #delta_rules and make yourself at home in the public lobby. If interested in joining please PM a member of the High Command or Council for assistance' },
-  { 'channel': 225217541922881538, 'message': 'Welcome to Delta Squadron, take a look at #delta_rules and make yourself at home in the public lobby. If interested in joining please PM a member of the High Command or Council for assistance' }
+  { 'channel': 222685317885329408, 'message': 'Welcome to Catbot' },
+  { 'channel': 225217541922881538, 'message': 'Welcome to Delta Squadron, take a look at #delta_rules and make yourself at home in the public lobby. If interested in joining please PM a member of the High Command or Council for assistance', 'voice': 222103336483028993, 'sound': 'meow.wav' }
 ]
 
 def open_url(url):
@@ -114,6 +115,8 @@ def set_avatar(url):
 @asyncio.coroutine
 def set_idle(idle):
   yield from client.change_status(idle = idle)
+  for voice in list(client.voice_clients):
+    yield from voice.disconnect()
 
 @asyncio.coroutine
 def wake_up(force = False):
@@ -363,9 +366,11 @@ def do_commands(message, command, raw, non_admin):
     for response in ['om nom nom', '^-^', 'meow!', 'lick']:
       said = yield from maybe_say(message.channel, response)
       if said:
+        yield from maybe_play_sound(voice_channel_for_channel(message.channel), 'purr.wav', join = False)
         return
 
   yield from maybe_say(message.channel, 'purr', wake = False)
+  yield from maybe_play_sound(voice_channel_for_channel(message.channel), 'purr.wav', join = False)
 
 @asyncio.coroutine
 def do_edi(message, command, raw, p):
@@ -420,7 +425,7 @@ def set_unassigned(member):
   yield from set_role(member, 'Unassigned')
 
 @asyncio.coroutine
-def greet(channel, member):
+def greet(channel, member, sound = True):
   for greeting in greetings:
     if greeting['channel'] != int(channel.id):
       continue
@@ -429,6 +434,8 @@ def greet(channel, member):
       log.info('Not sending greeting {}'.format(message))
     else:
       yield from say(channel, message)
+      if sound:
+        yield from play_greeting(greeting)
 
 @asyncio.coroutine
 def set_rss_permissions(feed):
@@ -588,6 +595,67 @@ def should_greet_member_in(channel, member, role_name = 'Unassigned', minimum =
     return False
   return True
 
+def voice_channel_for_channel(channel):
+  for voice in client.voice_clients:
+    if voice.server != channel.server:
+      continue
+    if not voice.is_connected:
+      continue
+    return voice.channel
+
+@asyncio.coroutine
+def maybe_play_sound(channel, filename, *, probability = 0.05, join = True):
+  if not channel or not filename:
+    log.warning('Missing channel and/or filename for play_sound()')
+    return False
+
+  if random.random() > probability:
+    log.debug("Didn't bother to play {}".format(filename))
+    return False
+
+  while playing.value:
+    yield from asyncio.sleep(1)
+
+  playing.value = True
+  voice = client.voice_client_in(channel.server)
+  if voice is None or voice.channel != channel:
+    if not join:
+      log.info('Not joining voice channel {} just to play {}'.format(channel, filename))
+      return False
+    log.debug('Moving to voice channel {}'.format(channel))
+    if voice is not None:
+      yield from client.move_to(channel)
+    else:
+      yield from client.join_voice_channel(channel)
+    voice = client.voice_client_in(channel.server)
+    if voice.channel != channel:
+      log.error("Can't move to voice channel {}".format(channel))
+      playing.value = False
+      return False
+  try:
+    log.info('Playing {} in channel {}'.format(filename, channel))
+    player = voice.create_ffmpeg_player(filename)
+    player.start()
+    while not player.is_done():
+      yield from asyncio.sleep(1)
+    playing.value = False
+  except:
+    log.error("Error creating ffpmeg player for {}".format(filename))
+    playing.value = False
+    return False
+
+@asyncio.coroutine
+def play_sound(channel, filename, *, join = True):
+  result = yield from maybe_play_sound(channel, filename, probability = 1.0, join = join)
+  return result
+
+@asyncio.coroutine
+def play_greeting(greeting):
+  if 'voice' not in greeting:
+    return
+  yield from play_sound(client.get_channel(str(greeting['voice'])), greeting['sound'])
+
+
 @client.event
 @asyncio.coroutine
 def on_ready():
@@ -598,11 +666,13 @@ def on_ready():
   print('Greeting in {}'.format([channel.name for channel in channels]))
   for channel in channels:
     log.debug('Greeting in {}'.format(channel.name))
+    sound = True
     for member in channel.server.members:
       result = should_greet_member_in(channel, member)
       if not result:
         continue
-      yield from greet(channel, member)
+      yield from greet(channel, member, sound)
+      sound = False
   for member in client.get_all_members():
     log.debug('Maybe unassigning {}'.format(member.name))
     yield from set_unassigned(member)
diff --git a/meow.wav b/meow.wav
new file mode 100644 (file)
index 0000000..13607c1
Binary files /dev/null and b/meow.wav differ
diff --git a/purr.wav b/purr.wav
new file mode 100644 (file)
index 0000000..8714f10
Binary files /dev/null and b/purr.wav differ