From d96062226d3395787c63b7e78a972564db67c809 Mon Sep 17 00:00:00 2001 From: Michael H Date: Fri, 15 Feb 2019 19:15:33 -0500 Subject: [PATCH] [Audio] Remove players which no longer have a guild. (#2414) Cleanup players when the bot has one for a guild it leaves. Bumps Red-Lavalink to v0.2.2 --- redbot/cogs/audio/audio.py | 17 +++++++++++++++++ setup.py | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/redbot/cogs/audio/audio.py b/redbot/cogs/audio/audio.py index aa58e427f..eea8b70b5 100644 --- a/redbot/cogs/audio/audio.py +++ b/redbot/cogs/audio/audio.py @@ -2624,3 +2624,20 @@ class Audio(commands.Cog): self._cleaned_up = True __del__ = __unload + + async def on_guild_remove(self, guild: discord.Guild): + """ + This is to clean up players when + the bot either leaves or is removed from a guild + """ + channels = { + x # x is a voice_channel + for y in [g.voice_channels for g in self.bot.guilds] + for x in y # y is a list of voice channels + } # Yes, this is ugly. It's also the most performant and commented. + + zombie_players = {p for p in lavalink.player_manager.players if p.channel not in channels} + # Do not unroll to combine with next line. + # Can result in iterator changing size during context switching. + for zombie in zombie_players: + await zombie.destroy() diff --git a/setup.py b/setup.py index 5f9efabca..14c9eb09c 100644 --- a/setup.py +++ b/setup.py @@ -19,7 +19,7 @@ install_requires = [ "pyyaml==3.13", "raven==6.10.0", "raven-aiohttp==0.7.0", - "red-lavalink==0.2.1", + "red-lavalink==0.2.2", "schema==0.6.8", "websockets==7.0", "yarl==1.3.0",