yt-search SearchError yt_search.errors.SearchError: badRequest

yt-search SearchError yt_search.errors.SearchError: badRequest

我正在 discord.py 制作一个 discord 机器人,我快完成了。我正在尝试这样做,以便您只需输入视频的标题,它就会搜索它。所以我尝试使用 yt-search 库,因为它看起来很容易使用。但是当我尝试 运行 我得到的机器人时 this error 请告诉我下一步应该做什么。

这是我的代码:

import discord
from discord.ext import commands
import youtube_dl
import yt_search
import os

client = commands.Bot(command_prefix="?")


@client.event
async def on_ready():
    print("Bot is ready.")

    game = discord.Game("Music")
    await client.change_presence(activity=game)


@client.command()
async def play(ctx, url: str = None):
    if url is None:
        await ctx.send("Put YouTube video's link after the 'play' command.")
        return

    song_there = os.path.isfile("song.mp3")
    try:
        if song_there:
            os.remove("song.mp3")
    except PermissionError:
        await ctx.send("Wait for the currently playing music to end or use the 'stop' command.")

    channel = discord.utils.get(ctx.guild.voice_channels, name="music lounge")
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    if not voice is None:
        if not voice.is_connected():
            voice = await channel.connect()
    else:
        voice = await channel.connect()

    ydl_opts = {
        'format': "bestaudio",
        'postprocessors': [{
            'key': "FFmpegExtractAudio",
            'preferredcodec': "mp3",
            'preferredquality': "192"
        }]
    }
    try:
        with youtube_dl.YoutubeDL(ydl_opts) as ydl:
            await ctx.send("Downloading music...")
            ydl.download([url])
    except:
        yt = yt_search.build("API Key")
        search_result = yt.search(url, sMax=10, sType=["video"])
        print(search_result.title)
        print(search_result.videoId)
        print(search_result.channelTitle)

    for file in os.listdir("./"):
        if file.endswith(".mp3"):
            os.rename(file, "song.mp3")

    voice.play(discord.FFmpegPCMAudio("song.mp3"))


@ client.command()
async def leave(ctx):
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    if not voice is None:
        if voice.is_connected():
            await voice.disconnect()
        else:
            await ctx.send("The bot is not connected to a voice channel.")


@ client.command()
async def pause(ctx):
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    if not voice is None:
        if voice.is_playing():
            voice.pause()
        else:
            await ctx.send("Currently no audio is playing.")
    else:
        await ctx.send("The bot is not connected to a voice channel.")


@ client.command()
async def resume(ctx):
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    if not voice is None:
        if voice.is_paused():
            voice.resume()
        else:
            await ctx.send("The audio is not paused.")
    else:
        await ctx.send("The bot is not connected to a voice channel.")


@ client.command()
async def stop(ctx):
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    if not voice is None:
        if voice.is_playing():
            voice.stop()
        else:
            await ctx.send("Currently no audio is playing.")
    else:
        await ctx.send("The bot is not connected to a voice channel.")


client.run('TOKEN')

您可以更轻松地搜索 Youtube 视频,例如使用这个短代码:

with youtube_dl.YoutubeDL(ytdl_format_options) as ydl:
        urlprev = ydl.extract_info(f"ytsearch:url", download=True)