Discord music bot, when I skip a track, the bot skips 2 tracks at once

Viewed 41

Situation and problem: I play a song and create a list of songs that will be played. After that I use > skip, I get into the play_music I get to the self.vc.play and there I start after=, when the previous song ended due to the fact that I > skip. (It should be noted that before I came to the self.vc.play I uploaded the track again, it follows minus one song) Continuing with after=, I get to the play_next, where I will load the song again and as a result I lost 1 song. Hence the question, how to write code so that songs can normally > skip?

And here's a simple example that I get

class Music(commands.Cog):

    def __init__(self, bot):
        self.bot = bot
        self.client = Client(token_yandex_music).init()
        # all the music related stuff
        self.is_playing = False
        self.is_paused = False

        # 2d array containing [song, channel]
        self.music_queue = []
        self.vc = None

    def play_next(self):
        if len(self.music_queue) > 0:
            self.vc.stop()
            self.music_queue[0][0]['source'].download('song.mp3')
            self.music_queue.pop(0)
            self.vc.play(discord.FFmpegPCMAudio('song.mp3'), after=lambda e: self.play_next())
            self.is_playing = True
        else:
            self.is_playing = False

    async def play_music(self, ctx):
        if len(self.music_queue) > 0:
            self.is_playing = True

            self.music_queue[0][0]['source'].download('song.mp3')
            # try to connect to voice channel if you are not already connected
            if self.vc is None or not self.vc.is_connected():
                self.vc = await self.music_queue[0][1].connect()
                # in case we fail to connect
                if self.vc is None:
                    await ctx.send("Вы не подключены к гс каналу")
                    return
            else:
                await self.vc.move_to(self.music_queue[0][1])
            # remove the first element as you are currently playing it
            self.music_queue.pop(0)
            self.vc.play(discord.FFmpegPCMAudio('song.mp3'), after=lambda e: self.play_next())
        else:
            self.is_playing = False

    @commands.command(name="play", aliases=["p", "playing"], help="Plays a selected song from ym")
    async def play(self, ctx, *args):
        query = " ".join(args)

        voice_channel = ctx.author.voice.channel
        if voice_channel is None:
            # you need to be connected so that the bot knows where to go
            await ctx.send('Вы не подключены к гс каналу')
        elif self.is_paused:
            self.vc.resume()
        else:
            song = await self.search_yt(ctx, query)
            if type(True) == type(song):
                await ctx.send(
                    "Не удалось загрузить песню")
            else:
                await ctx.send("Песня добавлена в плейлист")
                self.music_queue.append([song, voice_channel])
                if not self.is_playing:
                    await self.play_music(ctx)

    @commands.command(name="skip", aliases=["s"], help="Skips the current song being played")
    async def skip(self, ctx):
        if self.vc is not None and self.vc:
            self.vc.stop()
            # try to play next in the queue if it exists
            await self.play_music(ctx)
0 Answers
Related