Discord.py 在嵌入消息中发送 youtube 缩略图

时间:2021-07-06 04:11:22

标签: python discord.py

我正在尝试学习如何制作音乐机器人,播放命令还可以,但是当嵌入消息中给出 url 时,youtube 缩略图不显示。我知道有什么功能可以让机器人将 youtube 视频缩略图显示到嵌入消息中。代码如下:

@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)

1 个答案:

答案 0 :(得分:0)

无论如何,我认为您正在寻找 .set_image() 方法。
您可以通过

获得指向 YouTube 视频缩略图的链接
?:

喜欢解释here
通过

检索youtube视频的ID
https://img.youtube.com/vi/<insert-youtube-video-id-here>/0.jpg

然后您可以将其设置为嵌入图像或缩略图。 在你的情况下,它应该是这样的

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

如果您觉得这张图片太大,您也可以通过以下方式将其设置为缩略图 @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)