vendredi 15 décembre 2017

If-statement in audio_player_task() in my code causing music queue to stop playing

So, the audio_player_task() in my code has been modified to the 'repeat' command for a music loop (line 122).

However, it seems that this modification (the if self.repeat: statement) has caused the queue to stop working after a single song has finished; (the queue no longer plays songs that appear later in the queue). What I ask for is an explanation of what is causing the queue to stop working on a single song and a description/example of a solution.

My Code:

class Song:
    def __init__(self, path, message, player, video_info):
        self.path = path
        self.message = message
        self.requester = message.author
        self.channel = message.channel
        self.voice_channel = message.author.voice.voice_channel
        self.server = message.server
        self.player = player
        self.title = video_info[0]
        self.duration = video_info[1]
        self.thumbnail = video_info[2]
        self.uploader = video_info[3]
        self.web_url = video_info[4]
        if self.duration:
            m, s = divmod(video_info[1], 60)
            h, m = divmod(m, 60)
            self.duration = "%02d:%02d:%02d" % (h, m, s)

    def embed(self):
        song_info = discord.Embed(
            colour=discord.Colour.green()
        )
        duration = self.duration
        song_info.add_field(name='Uploaded by', value=self.uploader)
        song_info.add_field(name='Requested by', value=self.requester.display_name)
        song_info.add_field(name='Duration', value=str(duration))
        song_info.set_author(name=self.title, url=self.web_url)
        song_info.set_thumbnail(url=self.thumbnail)
        return song_info

    def on_song_playing(self):
        return ":notes: **Now playing** {} (`{}`)".format(self.title, self.duration)


class Queue:
    def __init__(self, bot, voice_client):
        self.bot = bot
        self.repeat = False
        self.voice_client = voice_client
        self.play_next_song = asyncio.Event()
        self.current = None
        self.skip_votes = set()
        self.songs = []
        self.song_name_list = []
        self.audio_player = self.bot.loop.create_task(self.audio_player_task())

    def _set_repeat(self):
        if not self.repeat:
            self.repeat = True
            return True
        elif self.repeat:
            self.repeat = False
            return False
        else:
            return

    def is_playing(self):
        if self.voice_client is None or self.current is None:
            return False

        player = self.current.player
        return not player.is_done()

    @property
    def player(self):
        return self.current.player

    def toggle_next(self):
        self.bot.loop.call_soon_threadsafe(self.play_next_song.set)

    def skip(self):
        self.skip_votes.clear()
        if self.is_playing():
            self.player.stop()

    def _get(self):
        song = self.songs[0]
        del self.songs[0]

        return song

    async def audio_player_task(self):
        while True:
            if self.repeat:
                player = self.voice_client.create_ffmpeg_player(self.current.path, after=self.toggle_next)
                video_info = [self.current.title, self.current.duration, self.current.thumbnail, self.current.uploader, self.current.web_url]
                print(video_info)
                song = Song(self.current.path, self.current.message, player, video_info)
                self.songs.insert(0, song)
                self.song_name_list.insert(0, self.current.title)
                print(self.songs)

            print(self.repeat)

            self.play_next_song.clear()
            self.current = self._get()
            self.song_name_list.remove(self.current)
            self.skip_votes.clear()
            await self.bot.send_message(self.current.channel, self.current.on_song_playing())
            self.current.player.start()
            await self.play_next_song.wait()

Note: I think I've narrowed the problem down to line 128, which attempts to create a Song() object, but cannot and does not raise an Exception. (This is only when self.repeat is True).

Aucun commentaire:

Enregistrer un commentaire