0

I want to use key-words in searching song instead of url in youtube-dl. e.x .play take my horse But I found examples when the song is being downloaded to PC, I don't wanna do it, just do the same like now but search with key-words. I'm new in discord.py and youtube-dl for me it dark forest. Help me pls !

music.py - there is a my code now, with url search.

import discord
from discord.ext import commands
import youtube_dl


class Music(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.command()
    async def join(self, ctx):
        if ctx.author.voice is None:
            await ctx.send("Вы не находитесь в голосовом канале !")
        voice_channel = ctx.author.voice.channel
        if ctx.voice_client is None:
            await voice_channel.connect()
        else:
            await ctx.voice_client.move_to(voice_channel)

    @commands.command()
    async def disconnect(self, ctx):
        await ctx.voice_client.disconnect()

    @commands.command()
    async def play(self, ctx, url):
        try:
            if ctx.author.voice is None:
                await ctx.send("Вы не находитесь в голосовом канале !")
            voice_channel = ctx.author.voice.channel
            if ctx.voice_client is None:
                await voice_channel.connect()
            else:
                await ctx.voice_client.move_to(voice_channel)
            ctx.voice_client.stop()
            ffmpeg_options = {
                'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
                'options': '-vn'
            }
            ydl_options = {
                'format': 'bestaudio'
            }
            vc = ctx.voice_client

            with youtube_dl.YoutubeDL(ydl_options) as ydl:
                info = ydl.extract_info(url, download=False)
                url2 = info['formats'][0]['url']
                source = await discord.FFmpegOpusAudio.from_probe(url2, **ffmpeg_options)
                vc.play(source)
        except discord.ClientException:
            await ctx.send("Already Playing Song")

    @commands.command()
    async def pause(self, ctx):
        ctx.voice_client.pause()
        embed = discord.Embed(
            title="",
            description="***Приостановлено.***",
            color=0x2f3136
        )
        await ctx.send(embed=embed)

    @commands.command()
    async def resume(self, ctx):
        ctx.voice_client.resume()
        embed = discord.Embed(
            title="",
            description="***Возобновлено.***",
            color=0x2f3136
        )
        await ctx.send(embed=embed)


def setup(client):
    client.add_cog(Music(client))
  • You need to somehow search YouTube then get the link to the first video. there are a lot of libraries that can do this, personally, I use pytube (although that's not asynchronous) – Wasi Master Sep 17 '21 at 03:18
  • Witalik, I recommend you take small steps at a time. This example combines two things (discord and youtube-dl) you are not very familiar with. Try to get the youtube-dl thing working first in a separate script. If you succeed, try to integrate that in your discord application. If not, you can write a question here focused on the youtube-dl part. That will also make your question more useful for others, and others will more likely be able to help you answer your question. Read how to create a [mre] and then [edit] your question. Good luck. – wovano Sep 17 '21 at 06:18

1 Answers1

1

youtube_dl already implements the use of key-words:

with YoutubeDL(ydl_options) as ydl:
    videos = ydl.extract_info("ytsearch:your key-words", download=False)

⚠️ Using key-words returns a list of videos

If you want both url and key-words, you could check if url is a valid link or not. Here's a Stack Overflow question that has various ways of doing it.

MrSpaar
  • 3,979
  • 2
  • 10
  • 25