首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >yt-search SearchError yt_search.errors.SearchError: badRequest

yt-search SearchError yt_search.errors.SearchError: badRequest
EN

Stack Overflow用户
提问于 2021-05-09 19:35:10
回答 1查看 29关注 0票数 0

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

下面是我的代码:

代码语言:javascript
复制
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')
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-09 20:25:10

你可以更容易地搜索Youtube视频,例如,使用以下简短代码:

代码语言:javascript
复制
with youtube_dl.YoutubeDL(ytdl_format_options) as ydl:
        urlprev = ydl.extract_info(f"ytsearch:url", download=True)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67457233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档