diff --git a/cogs/audio.py b/cogs/audio.py index 8b4da12e5..7b07f4a70 100644 --- a/cogs/audio.py +++ b/cogs/audio.py @@ -240,6 +240,13 @@ class Audio: "VOTE_THRESHOLD"] self.cache_path = "data/audio/cache" self.local_playlist_path = "data/audio/localtracks" + self._old_game = False + + async def _add_song_status(self, song): + if self._old_game is False: + self._old_game = list(self.bot.servers)[0].me.game + await self.bot.change_status(discord.Game(name=song.title)) + log.debug('Bot status changed to song title: ' + song.title) def _add_to_queue(self, server, url): if server.id not in self.queue: @@ -454,6 +461,16 @@ class Audio: # TODO: _enable_controls() + # returns list of active voice channels + # assuming list does not change during the execution of this function + # if that happens, blame asyncio. + def _get_active_voice_clients(self): + avcs = [] + for vc in self.bot.voice_clients: + if hasattr(vc, 'audio_player') and not vc.audio_player.is_done(): + avcs.append(vc) + return avcs + def _get_queue(self, server, limit): if server.id not in self.queue: return [] @@ -804,6 +821,12 @@ class Audio: if server.id in self.queue: del self.queue[server.id] + async def _remove_song_status(self): + if self._old_game is not False: + await self.bot.change_status(self._old_game) + log.debug('Bot status returned to ' + str(self._old_game)) + self._old_game = False + def _save_playlist(self, server, name, playlist): sid = server.id try: @@ -876,6 +899,7 @@ class Audio: self._setup_queue(server) self._stop_player(server) self._stop_downloader(server) + self.bot.loop.create_task(self._update_bot_status()) async def _stop_and_disconnect(self, server): self._stop(server) @@ -896,6 +920,23 @@ class Audio: if hasattr(voice_client, 'audio_player'): voice_client.audio_player.stop() + # no return. they can check themselves. + async def _update_bot_status(self): + if self.settings["TITLE_STATUS"]: + song = None + try: + active_servers = self._get_active_voice_clients() + except: + log.debug("voice_clients changed while trying to update bot's song status") + return + if len(active_servers) == 1: + server = active_servers[0].server + song = self.queue[server.id]["NOW_PLAYING"] + if song: + await self._add_song_status(song) + else: + await self._remove_song_status() + def _valid_playlist_name(self, name): for l in name: if l.isdigit() or l.isalpha() or l == "_": @@ -956,6 +997,19 @@ class Audio: await self.bot.say("Player toggled. You're now using ffmpeg.") self.save_settings() + @audioset.command(name="status") + @checks.is_owner() # cause effect is cross-server + async def audioset_status(self): + """Enables/disables songs' titles as status""" + self.settings["TITLE_STATUS"] = not self.settings["TITLE_STATUS"] + if self.settings["TITLE_STATUS"]: + await self.bot.say("If only one server is playing music, songs' titles will now show up as status") + # not updating on disable if we say disable means don't mess with it. + await self._update_bot_status() + else: + await self.bot.say("Songs' titles will no longer show up as status") + self.save_settings() + @audioset.command(pass_context=True, name="volume", no_pm=True) @checks.mod_or_permissions(manage_messages=True) async def audioset_volume(self, ctx, percent: int): @@ -1718,6 +1772,8 @@ class Audio: song = None self.queue[server.id]["NOW_PLAYING"] = song log.debug("set now_playing for sid {}".format(server.id)) + self.bot.loop.create_task(self._update_bot_status()) + elif server.id in self.downloaders: # We're playing but we might be able to download a new song curr_dl = self.downloaders.get(server.id)