0

Im trying to learn on how to make a music bot, the play command is alr fine but the youtube thumbnail pic doesnt show when the url is given in embed message. Idk whats the function to make the bot show the youtube video thumbnail into the embed message. Heres the code :

@client.command()
async def play(ctx, url : str):
    song_there = os.path.isfile("song.mp3")
    try:
        if song_there:
            os.remove("song.mp3")
    except PermissionError:
        await ctx.send("Wait for the current playing music to end or use %leave <:_Paimon6:827074349450133524>")
        return

    voiceChannel = discord.utils.get(ctx.guild.voice_channels, name='Private')
    await voiceChannel.connect()
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    em6 = discord.Embed(title = "Downloading Music", description = f'{url}\n\nPlease wait for paimon to setup the music you provide.\nMusic provided by {ctx.author.mention} <:_Paimon6:827074349450133524>',color = ctx.author.color)
    await ctx.send(embed = em6, delete_after = 2)

    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([url])
    for file in os.listdir("./"):
        if file.endswith(".mp3"):
            os.rename(file, "song.mp3")
    voice.play(discord.FFmpegPCMAudio("song.mp3"))
    em1 = discord.Embed(title = "Now Listening", description = f'{url}\n\nPlease use %leave first to change music.\nMusic provided by {ctx.author.mention} <:_Paimon6:827074349450133524>',color = ctx.author.color)
    await ctx.send(embed = em1)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Exd Craft
  • 52
  • 6
  • 3
    You cannot manually enable link previews, you will have to send the link as a separate message for the preview to be enabled, or manually grab the thumbnail and set it in your embed – Ceres Jul 06 '21 at 05:09

1 Answers1

0

Anyways, I think you're looking for the .set_image() method.
You can get a link to the thumbnail of a youtube video via

https://img.youtube.com/vi/<insert-youtube-video-id-here>/0.jpg

Like explained here.
Retrieve the id of the youtube video via

videoID = url.split("watch?v=")[1].split("&")[0]

And then you can set it as the embed image or thumbnail. In your case it should look like this

@client.command()
async def play(ctx, url : str):
    song_there = os.path.isfile("song.mp3")
    try:
        if song_there:
            os.remove("song.mp3")
    except PermissionError:
        await ctx.send("Wait for the current playing music to end or use %leave <:_Paimon6:827074349450133524>")
        return

    voiceChannel = discord.utils.get(ctx.guild.voice_channels, name='Private')
    await voiceChannel.connect()
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    em6 = discord.Embed(title = "Downloading Music", description = f'{url}\n\nPlease wait for paimon to setup the music you provide.\nMusic provided by {ctx.author.mention} <:_Paimon6:827074349450133524>',color = ctx.author.color)
    await ctx.send(embed = em6, delete_after = 2)

    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([url])
    for file in os.listdir("./"):
        if file.endswith(".mp3"):
            os.rename(file, "song.mp3")
    voice.play(discord.FFmpegPCMAudio("song.mp3"))
    em1 = discord.Embed(title = "Now Listening", description = f'{url}\n\nPlease use %leave first to change music.\nMusic provided by {ctx.author.mention} <:_Paimon6:827074349450133524>',color = ctx.author.color)

    # get id
    videoID = url.split("watch?v=")[1].split("&")[0]

    em1.set_image(url = "https://img.youtube.com/vi/{videoID}/0.jpg".format(videoID = videoID))
    await ctx.send(embed = em1)

If you find this image too big, you could also set it as a thumbnail via embed.set_thumbnail(url)

itzFlubby
  • 2,269
  • 1
  • 9
  • 31