Discord.py : Different loop per guild

Viewed 22

Hello i'm creating custom autoplay functions that allow users per guild to play their songs and make autoplay within but the problem is when I try to clear the queue even though the autoplay is disabled is still the while loop inside the async function is still ongoing even though it is canceled.

What it should be doing is when the user call the autoplay command is a non-blocking isolated loop for that guild, then when the user called again the autoplay, then the looping function for that specific guild called by user before will stop or break.

Here's the main initiator of the loop

@commands.command(aliases=['auto'])
async def autoplay(self, ctx):
    base = self.bot.get_cog('Music_base')

    
    _guild_autoloop = base.guild_autoplay[ctx.guild.id]
    print(_guild_autoloop)
    if _guild_autoloop is False:
        base.guild_autoplay.update({ctx.guild.id : True})
        future = asyncio.ensure_future(self.looped_task(ctx))

    else:
        base.guild_autoplay.update({ctx.guild.id : False})
        asyncio.ensure_future(self.looped_task(ctx)).cancel()

Then the function that responsible for the looping

async def looped_task(self, ctx):
        base = self.bot.get_cog('Music_base')

        _guild_autoloop = base.guild_autoplay[ctx.guild.id]
 
        if _guild_autoloop == True:
            print(f'\n{"="*20}\nThis loop is current enabled by - {ctx.guild.id} ')
            while _guild_autoloop:
                player = self.bot.lavalink.player_manager.get(ctx.guild.id)
                # if _guild_autoloop == True:
                if len(player.queue) == 0:
                    songs  = autoplay.get_yt(player.current.uri)
                    for song in songs:
                        try:
                            await self.capture_track(ctx, song)
                        except Exception as e:
                            print(e)
                            continue
        
                await asyncio.sleep(4)
0 Answers
Related