Youtube api Youtube视频搜索出错(总常量不匹配)

Youtube api Youtube视频搜索出错(总常量不匹配),youtube-api,bots,discord,discord.js,youtube-javascript-api,Youtube Api,Bots,Discord,Discord.js,Youtube Javascript Api,首先,我仍然不熟悉所有这些,但我仍然决定尝试一下,所以请耐心等待 我在这里使用的包是discord.js Commando和discord youtube api。在添加播放功能之前,我决定看看是否可以正确使用搜索功能。但每次我试图搜索某个东西时,结果都是毫无意义的(甚至与我试图搜索的视频没有任何关系),它只给我一个结果(watch?v=-yDd2D5OHyc),其他什么都没有 class SearchCommand extends Commando.Command { construc

首先,我仍然不熟悉所有这些,但我仍然决定尝试一下,所以请耐心等待

我在这里使用的包是discord.js Commando和discord youtube api。在添加播放功能之前,我决定看看是否可以正确使用搜索功能。但每次我试图搜索某个东西时,结果都是毫无意义的(甚至与我试图搜索的视频没有任何关系),它只给我一个结果(watch?v=-yDd2D5OHyc),其他什么都没有

class SearchCommand extends Commando.Command {
    constructor(client){
        super(client,{
            name: 'search',
            group:'music',
            memberName:'search',
            description: 'Search a Youtube video',
            args: [
                {
                    key: 'text',
                    prompt: 'Input the video name?',
                    type: 'string'
                }
            ]
        });
    }

    async run (message, args, {text})
    {
        message.channel.send(args)
        message.channel.send(text)
        var video = await youtube.searchVideos(args.toString().replace(/,/g,' '));
        message.channel.send(video.url);
        message.channel.send(video.thumbnail);
        message.channel.send(video.length);

    }
}

module.exports = SearchCommand;
…它只给了我一个结果

这是根据的
searchVideos()
方法的预期行为


[T] 他的结果完全是胡说八道

将用逗号连接数组的元素。这可能会导致意外的结果

您可以将
args
数组与合并在一起,并使用空格作为分隔符,而不是逗号。考虑这个例子…

const text='鲨鱼宝宝10小时';
常量args=text.split(+/+/g);
console.log(args.toString());//您当前的搜索查询

console.log(args.join(“”));//预期的搜索查询
感谢slothiful为我的问题提供了答案。 虽然您的解决方案对问题是正确的,但发生了另一个错误

(node:14492) UnhandledPromiseRejectionWarning: TypeError: Parameter "url" must be a string, not object
但是,提出的解决方案并没有错。只是不完整。 幸运的是,我已经找到了新错误的解决方案,结合您的解决方案,我可以使它像这样工作

async run(message, {text})
    {
        const args = text.split(/ +/g);
        const video = await youtube.searchVideos(args);
        console.log(args.toString());
        console.log(args.join(' '));
        console.log(video.url)
        const streamOptions = { seek: 0, volume: 1 };
        if (message.member.voiceChannel){
        message.member.voiceChannel.join()
        .then(
            connection => { 
            const stream = yt(video.url, { filter : 'audioandvideo', quality : 'highestaudio', lang : 'en'});
            const dispatcher = connection.playStream(stream, streamOptions);
            })
        }
        else {
            message.channel.send("Please join a voice channel before I can play it for you !")
        }