Discord Music Bot错误discord.ext.commands.errors.CommandInvokeError:命令引发了异常:KeyError:'videoId'

时间:2020-05-25 14:46:45

标签: python discord discord.py discord.py-rewrite

我试图制作一个不和谐的音乐机器人,并使用YouTube的Data API,我试图通过仅输入歌曲名称而不是URL来获取歌曲,但我遇到了一些有趣的问题。

“使用单词获取歌曲”功能最初存在一个重大缺陷,它只能接收其中包含1个单词的消息。这是整个序列的代码。

@client.command(pass_context = True, aliases = ['p'])
async def play(ctx, song_name: str):
    song_pack = rq.get("https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q={}&key={}".format(final_song_name, YOUTUBE_API)).json()
    song_url = "https://www.youtube.com/watch?v={}".format(song_pack['items'][0]['id']['videoId'])

    def check_queue():
        Queue_infile = os.path.isdir("./Queue")
        if Queue_infile is True:
            DIR = os.path.abspath(os.path.realpath("Queue"))
            length = len(os.listdir(DIR))
            still_q = length - 1

            try:
                first_file = os.listdir(DIR)[0]
            except:
                print("No more queued song(s)\n")
                queues.clear()
                return

            main_location = os.path.dirname(os.path.realpath(__file__))
            song_path = os.path.abspath(os.path.realpath("Queue") + "\\" + first_file)

            if length != 0:
                print("Song done, playing next queued\n")
                print(f"Songs still in queue: {still_q}")
                song_there = os.path.isfile("song.mp3")

                if song_there:
                    os.remove("song.mp3")
                shutil.move(song_path, main_location)

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

                voice.play(discord.FFmpegPCMAudio("song.mp3"), after = lambda e: check_queue())
                voice.source = discord.PCMVolumeTransformer(voice.source)
                voice.source.volume = 0.7

            else:
                queues.clear()
                return

        else:
            queues.clear()
            print("No songs were queued before the ending of the last song\n")

    song_there = os.path.isfile("song.mp3")
    try:
        if song_there:
            os.remove("song.mp3")
            queues.clear()
            print('Removed old song file')
    except PermissionError:
        print("Trying to delete song file, but, it is being played.")
        await ctx.send("ERROR: Music Playing")
        return


    Queue_infile = os.path.isdir("./Queue")
    try:
        Queue_folder = "./Queue"
        if Queue_infile is True:
            print("Removed old Queue folder")
            shutil.rmtree(Queue_folder)
    except:
        print("No old queue folder")

    await ctx.send("**Searching for** `{}` :mag_right:".format(song_name))

    voice = get(client.voice_clients, guild = ctx.guild)

    ydl_opts = {
    'format': 'bestaudio/best',
    'quiet': True,
    'postprocessors':[{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192',
        }],
    }

    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        print("Downloading audio now\n")
        ydl.download([song_url])

    for file in os.listdir("./"):
        if file.endswith(".mp3"):
            name = file
            print(f"Renamed File: {file}\n")
            os.rename(file, "song.mp3")

    voice.play(discord.FFmpegPCMAudio("song.mp3"), after = lambda e: check_queue())
    voice.source = discord.PCMVolumeTransformer(voice.source)
    voice.source.volume = 0.7

    nname = name[:-16]
    await ctx.send(f"**Playing** :musical_note: : `{nname}`")
    print("Playing\n")

但是当我尝试通过在第四行添加适当缩进来修复该缺陷时

    final_song_name = song_name.replace(" ", "_")

我得到了错误。

这是YouTube API中的JSON示例。

{
  "kind": "youtube#searchListResponse",
  "etag": "OvvXoVaLuxf9xsl2hEiI-iG1Hs4",
  "nextPageToken": "CAEQAA",
  "regionCode": "IN",
  "pageInfo": {
    "totalResults": 3994,
    "resultsPerPage": 1
  },
  "items": [
    {
      "kind": "youtube#searchResult",
      "etag": "6ooIFJrvuNFu7tLWPM_CbRXyp4s",
      "id": {
        "kind": "youtube#video",
        "videoId": "TfdHSJxVPCk"
      },
      "snippet": {
        "publishedAt": "2013-02-05T00:17:42Z",
        "channelId": "UCJLX0B8m1KrS6oe3xageAbQ",
        "title": "Archie - Famous (Original Mix) [Monstercat Release]",
        "description": "Archie - Famous (Original Mix) [Monstercat Release] Hope you enjoy & don't forget to subscribe! :D.",
        "thumbnails": {
          "default": {
            "url": "https://i.ytimg.com/vi/TfdHSJxVPCk/default.jpg",
            "width": 120,
            "height": 90
          },
          "medium": {
            "url": "https://i.ytimg.com/vi/TfdHSJxVPCk/mqdefault.jpg",
            "width": 320,
            "height": 180
          },
          "high": {
            "url": "https://i.ytimg.com/vi/TfdHSJxVPCk/hqdefault.jpg",
            "width": 480,
            "height": 360
          }
        },
        "channelTitle": "Domsn House",
        "liveBroadcastContent": "none",
        "publishTime": "2013-02-05T00:17:42Z"
      }
    }
  ]
}

我认为我所做的是正确的。我什至用以下代码制作了一个新程序

song_name = str(input('Enter Song Name:'))
final_song_name = song_name.replace(" ", "_")
song_pack = rq.get("https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q={}&key={}".format(final_song_name, YOUTUBE_API)).json()
song_url = "https://www.youtube.com/watch?v={}".format(song_pack['items'][0]['id']['videoId'])
print(song_url)

它工作正常,输入了很多单词后就获取了我的网址。 这是我的第一个问题。请原谅我的无能或愚蠢。

此外, rq =请求模块

0 个答案:

没有答案