Sandy
Sandy

Reputation: 287

How do I make a progress bar that shows how far along the music has been played in discord.py

I have made a discord music bot using python. I would like to know how to get the timestamp of how far along the track has been played.

Here is the now_playing command

@commands.command(aliases=['np', 'now'], description='Shows the current track being played')
async def nowplaying(self, ctx):
    # This command does not prevent users from outside the voice channel from accessing the command since it is view-only
    vc = discord.utils.get(self.bot.voice_clients, guild=ctx.guild)

    # Basic responses to false calls
    if vc is None:
        await send_error_embed(ctx, description='The Player is not connected to the voice channel')
        return

    if not vc.is_playing():
        await send_error_embed(ctx, description='No audio is being played')
        return

    else:
        # Response embed
        video = pafy.new(url=self.now_playing_url[ctx.guild.id])
        embed = discord.Embed(description='Now Playing', colour=discord.Colour.dark_teal())
        if video.author == video.username:
            embed.add_field(name='Track Name:',
                            value=f'[{self.now_playing[ctx.guild.id]}]({self.now_playing_url[ctx.guild.id]}) by [{video.author}](https://youtube.com/c/{video.author})')
        else:
            embed.add_field(name='Track Name:',
                            value=f'[{self.now_playing[ctx.guild.id]}]({self.now_playing_url[ctx.guild.id]}) by [{video.author}](https://youtube.com/channel/{video.username})')
        embed.set_author(name=self.bot.user.name, icon_url=self.bot.user.avatar)
        embed.set_thumbnail(url=video.bigthumbhd)
        embed.set_footer(text=f'Duration: {video.duration}, 🎥: {video.viewcount}, 👍: {video.likes}')
        await ctx.send(embed=embed)

Upvotes: 0

Views: 1747

Answers (1)

hardik singh
hardik singh

Reputation: 84

you can get the exact time at which the track started playing. you can store this info in maybe a database. then when the user requests for the track which is currently playing, you can get the exact time at which the user sent the command and deduct the two time values to get how much time has passed since the track started playing. for the database system, you can use something light like pickle and for time, you can use the time library.

now we have how far are we into the song and the total length of the song. you can now convert these values into seconds and then get the percentage of the song played. now to display the porgress bar you can have 10 emoji blocks in total and display that percentage in these blocks(you will have to round them off to the nearest factor of 10). for example let's say we are 1 minute in of a 3-minute song, then it would be 60/180 or 33.33%. we can approximate this to 30% and display 30% of the 10 emoji blocks, which would be only 3 blocks out of the 10

you can change the emojis according to the theme of your bot

Upvotes: 1

Related Questions