Voice state moving and grooving.
authorjweigele <jweigele@local>
Sat, 19 Jun 2021 00:37:19 +0000 (17:37 -0700)
committerjweigele <jweigele@local>
Sat, 19 Jun 2021 00:37:19 +0000 (17:37 -0700)
Dockerfile
grahbot.py

index ed72f14cfa29242a02c63e7fb7fea1771d7e49f4..eda2cc51687d476e2d5998da21dd3ff48e569a11 100644 (file)
@@ -8,6 +8,7 @@ WORKDIR /usr/src/app
 RUN apt-get update
 RUN apt-get install -y --no-install-recommends python3-pip ffmpeg
 RUN pip3 install discord.py asynqp PyNaCl
+RUN pip3 install aioprometheus
 
 # Run the specified command within the container.
 CMD [ "/usr/src/app/grahbot.py" ]
index 1b7d9387cba3cd8745ed06974ec76ea83e356f15..7ec8221f0406d7b83a4f097001b3e1ad5ce0b953 100755 (executable)
@@ -47,7 +47,7 @@ class GrahState(object):
 
 
     def set_voice(self, guild, voice):
-        print('Setting voice for guild {} to {}'.format(guild, voice))
+        log.debug('Setting voice for guild {} to {}'.format(guild, voice))
         self.set_voice_by_id(guild.id, voice)
 
     def set_voice_by_id(self, guild_id, voice):
@@ -117,9 +117,6 @@ class HornClient(object):
         log.debug('Declaring exchange')
         exchange = await channel.declare_exchange(self.rabbit_config['exchange'], 'topic', passive=True)
 
-        # we have 10 users. Set up a queue for each of them
-        # use different channels to avoid any interference
-        # during message consumption, just in case.
         self.channel = await connection.open_channel()
         log.debug('Declaring queue')
         self.queue = await self.channel.declare_queue('horn_listener')
@@ -128,7 +125,6 @@ class HornClient(object):
         await self.queue.consume(self.process_msg, no_ack=True)
         log.debug('Consumed a thing')
 
-        # deliver 10 messages to each user
         while (True):
             await asyncio.sleep(1)
             #msg = asynqp.Message("omg here is the time {}".format(datetime.datetime.now()))
@@ -301,13 +297,14 @@ class GrahDiscordBot(discord.Client, HornClient):
 
 
     async def msg_join_voice_channel(self, channel_name, guild):
-        log.debug('Called join voice channel')
+        log.debug('Called join voice channel, trying to join channel "{}" in guild {}'.format(channel_name, guild))
         for vc in self.voice_channels:
             if vc.name == channel_name and (not guild or vc.guild == guild):
                 log.debug('Found a voice channel to join {} {}'.format(vc, type(vc)))
-                if self.state.get_voice(guild):
+                if self.state.get_voice(guild) is not None:
                     if vc != self.state.get_voice(guild).channel:
-                        self.state.set_voice(guild, await self.state.get_voice(guild).move_to(vc))
+                        log.debug('Already have a client {}, awaiting the move to {}'.format(self.state.get_voice(guild), vc))
+                        await self.state.get_voice(guild).move_to(vc)
                 else:
                     self.state.set_voice(guild, await vc.connect())
                 log.info('Joined voice channel {} {} (id: {})'.format(guild, vc.name, vc.id))
@@ -359,11 +356,13 @@ class GrahDiscordBot(discord.Client, HornClient):
 
 
     def member_guild_list(self, member):
+        retval = []
         for x in self.guilds:
             for member in x.members:
-                print('Guild {} Member {}'.format(x, member))
-            #print(x.members)
-        return [x for x in self.guilds if member in x.members]
+                log.debug('Guild {} Member {}'.format(x, member))
+            if member.id in [y.id for y in x.members]:
+                retval.append(x)
+        return retval
 
     def contextual_guild(self, message):
         channel = message.channel
@@ -394,18 +393,21 @@ class GrahDiscordBot(discord.Client, HornClient):
                 log.debug('Setting member {} to guild {}'.format(message.channel.recipient, guild.name))
                 self.member_guilds[message.channel.recipient] = [guild]
                 self.state.set_user_guild(message.channel.recipient, guild)
-                break
+                return
+            else:
+                log.debug('Guild {} does not match {}'.format(guild.name, guild_name))
+        log.debug('No guild found for {}'.format(guild))
 
     async def command_sleep(self, message, guild):
         await asyncio.sleep(5)
         await message.channel.send('Done sleeping')
 
     async def command_join(self, message, guild):
-        log.info('Was instructed to join {} guild {}'.format(message.content.split(' ')[1], str(guild)))
-        await self.msg_join_voice_channel(message.content.split(' ')[1], guild=guild)
+        log.info('Was instructed to join {} guild {}'.format(' '.join(message.content.split(' ')[1:]), str(guild)))
+        await self.msg_join_voice_channel(' '.join(message.content.split(' ')[1:]), guild=guild)
 
     async def command_play(self, message, guild):
-        log.info('Was instructed to play {}'.format(message.content.split(' ')[1]))
+        log.info('Was instructed to play {}'.format(' '.join(message.content.split(' ')[1:])))
         await self.msg_play_sound(name=' '.join(message.content.split(' ')[1:]), guild=guild)
 
     async def command_list(self, message, guild):
@@ -460,7 +462,7 @@ class GrahDiscordBot(discord.Client, HornClient):
             await message.channel.send('Too many guilds!!!\n{}'.format('\n'.join([str(x) for x in guilds])))
             return
         elif len(guilds) == 1:
-            print('Guild is {}'.format(guilds[0]))
+            log.debug('Guild is {}'.format(guilds[0]))
             guild = guilds[0]
 
         func = self.get_command_func(potential_command)
@@ -475,8 +477,18 @@ class GrahDiscordBot(discord.Client, HornClient):
         except Exception as e:
             await self.oh_no(message.channel, e)
 
+    async def on_voice_state_update(self, member, before, after):
+        if self.user.id == member.id:
+            log.debug('Something happened to me in voice! Member {} before {} after {}'.format(member, before, after))
+            if before.channel is not None and after.channel is not None and before.channel != after.channel:
+                log.debug('Before != after and channels differ, setting the VC appropriately')
+                # try to get the already established VC and resave
+                self.state.set_voice(after.channel.guild, self.state.get_voice(after.channel.guild))
+
+
 if __name__ == '__main__':
     config = json.load(open('{}/config.json'.format(PERSISTENT_DIR), 'r'))
     intents = discord.Intents.default()
     intents.members = True
+    intents.voice_states = True
     bot = GrahDiscordBot(config, intents=intents)