Not long ago I'm working on a bot for Discord and I want to add the functionality that the bot (like Rythm) search both by URL provided by the user or by keywords, that is, enter the name of the song like for example "Thriller Michael Jackson" and that the bot searches through YouTube for the first link that matches the search. For now I have only managed to play music by entering a specific URL, but entering a codeword does not occur to me.
I am working in python with the following libraries:
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
import youtube_dl
from ytsearch import youtube
With the tools provided by the library of "ytsearch" the only thing I can get is to show the name of the videos in the form of a list, but I do not see the tool to return a URL from there. If someone could bring me light on the matter I would be very grateful.
Here I leave the fragment of code where the bot would play the audio of the video:
@bot.command(pass_context=True)
async def play(ctx,url):
channel = ctx.message.author.voice_channel
server = ctx.message.server
await bot.join_voice_channel(channel)
voice_client = bot.voice_client_in(server)
player = await voice_client.create_ytdl_player(url)
players[server.id] = player
player.start()
print("Me he metido en el canal {} para poner música".format(channel))
This is how I have it structured right now, the idea would be if you provide "URL" with both a code word and a link, reproduce the desired video. The solution should be added on top of "player" in order to not alter the behavior of the code
PS: sorry for the poor writing