简体   繁体   English

ytdl:“url”参数必须是字符串类型。 接收类型未定义

[英]ytdl: The "url" argument must be of type string. Received type undefined

I'm working on a Discord bot using Discord.js and ytdl.我正在使用 Discord.js 和 ytdl 开发一个 Discord 机器人。 However, whenever I type ?play and my play command is executed, this error is thrown:但是,每当我输入?play并执行我的播放命令时,就会抛出此错误:

 (node:15840) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received type undefined at URL.parse(url.js:154:11) at Object.urlParse [as parse] (url.js:148:13) at Object.exports.getURLVideoID (C:\\Users\\MauSc\\Desktop\\[MENU DOCS] Bot\\node_modules\\ytdl-core\\lib\\util.js:273:22) at Function.exports.validateURL(C:\\Users\\MauSc\\Desktop\\[MENU DOCS] Bot\\node_modules\\ytdl-core\\lib\\util.js:328:20) at Object.module.exports.run (C:\\Users\\MauSc\\Desktop\\[MENU DOCS] Bot\\commands\\play.js:18:22) at Client.bot.on (C:\\Users\\MauSc\\Desktop\\[Menu DOCS] Bot\\index.js:51:38) at Client.emit (events.js:198:13) at MessageCreateHandler.handle (C:\\Users\\MauSc\\Desktop\\[MENU DOCS] Bot\\node_modules\\discord.js\\src\\client\\websocket\\packets\\handlers\\MessageCreate.js:9:34) at WebSocketPacketManager.handle (C:\\Users\\MauSc\\Desktop\\[MENU DOCS] Bot\\node_modules\\discord.js\\src\\client\\websocket\\packets\\WebSocketPacketManager.js:105:65) at WebSocketConnection.onPacket (C:\\Users\\MauSc\\Desktop\\[MENU DOCS] Bot\\node_modules\\discord.js\\src\\client\\websocket\\WebSocketConnection.js:333:35) (node:15840) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:15840) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

The code is all copied from another bot I made, so I'm not sure why the error suddenly appears.代码都是从我制作的另一个机器人复制的,所以我不确定为什么会突然出现错误。

This is the code:这是代码:

const Discord = require("discord.js");
const ytdl = require('ytdl-core');
const streamOptions ={
    seek: 0,
    volume: 1
}

const bot = new Discord.Client({disableEveryone: true});

var musicUrls = [];

bot.on("message", async message => {

module.exports.run = async (bot, message, args) => {
             let url = args[1];
             let voiceChannel = message.guild.channels.find(channel => channel.name === 'Musik Bot');

             if(ytdl.validateURL(url))
             {
                console.log("Valid URL");
                var flag = musicUrls.some(element => element === url);
                if(!flag)
                {
                    musicUrls.push(url);
                    if(voiceChannel != null)
                    {

                        if(voiceChannel.connection)
                        {
                            console.log("Connection exists");
                            const embed = new Discord.RichEmbed();
                            embed.setAuthor(bot.user.username, bot.user.displayAvatarURL);
                            embed.setDescription("Du hast erfolgreich einen Track zu der Playlist hinzugefügt!");
                            message.channel.send(embed);
                        }
                        else {
                            try {
                                const voiceConnection = await voiceChannel.join();
                                await playSong(message.channel, voiceConnection, voiceChannel);
                            }
                            catch(ex)
                            {
                                console.log(ex);
                             }
                         }
                    }

                 }
             }
}
     async function playSong(messageChannel, voiceConnection, voiceChannel)
{
    const stream = ytdl(musicUrls[0], { filter : 'audioonly'});
    const dispatcher = voiceConnection.playStream(stream, streamOptions);

    dispatcher.on('end', () => {
        musicUrls.shift();

        if(musicUrls.length == 0)
            voiceChannel.leave();
        else
        {
            setTimeout(() => {
                 playSong(messageChannel, voiceConnection, voiceChannel);
            }, 1000);
        }
    });
    try {
        if(!message.content.startsWith(prefix)) return;
        let commandfile = bot.commands.get(cmd.slice(prefix.length)) || bot.commands.get(bot.aliases.get(cmd.slice(prefix.length)))
        console.log(commandfile);
        if (commandfile) commandfile.run(bot, message, args)
    } catch (error) {
        console.error(error);
    }
}},


module.exports.config = {
    name: "play",
    aliases: []
});

args[1] is undefined when your code is run, which means it was never provided. args[1]在您的代码运行时未定义,这意味着它从未被提供。 This causes url to also be undefined, and your error is then thrown from ytdl because validateURL() expected a string.这会导致url也未定义,然后从 ytdl 抛出您的错误,因为validateURL()需要一个字符串。

Before continuing with the command, ensure that the required argument is present.在继续执行该命令之前,请确保存在所需的参数。 If not, return an error message.如果不是,则返回错误消息。 For example...例如...

if (!args[1]) {
  return message.channel.send('You must provide a URL!')
    .catch(console.error);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 axios “url”参数必须是字符串类型。 收到类型未定义错误 - axios The "url" argument must be of type string. Received type undefined error 等待 dTypeError [ERR_INVALID_ARG_TYPE]:“id”参数必须是字符串类型。 收到未定义 - Waiting for the dTypeError [ERR_INVALID_ARG_TYPE]: The “id” argument must be of type string. Received undefined 错误:“路径”参数必须是字符串类型。 收到未定义。 firebase deploy --only 功能 - Error: The “path” argument must be of type string. Received undefined. firebase deploy --only functions (hashlips_art_engine-1.1.2_patch_v5) “路径”参数必须是字符串类型。 收到未定义 - (hashlips_art_engine-1.1.2_patch_v5) The "path" argument must be of type string. Received undefined 图片上传错误:TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。 接收类型未定义 - Image Upload Error: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined TypeError [ERR_INVALID_ARG_TYPE]:“路径”参数必须是字符串类型。 收到未定义和代码:'ERR_INVALID_ARG_TYPE' - TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined and code: 'ERR_INVALID_ARG_TYPE' 尝试使用gh-pages部署我的React应用程序,但收到此错误消息:“file”参数必须是string类型。收到的类型未定义 - Trying to deploy my React app with gh-pages but got this error message : The “file” argument must be of type string. Received type undefined [ERR_INVALID_ARG_TYPE]:“id”参数必须是字符串类型。 接收类型对象 - [ERR_INVALID_ARG_TYPE]: The "id" argument must be of type string. Received type object 类型错误 [ERR_INVALID_ARG_TYPE]:“文件”参数必须是字符串类型。 接收类型对象 - TypeError [ERR_INVALID_ARG_TYPE]: The "file" argument must be of type string. Received type object ERR_INVALID_ARG_TYPE - “from” 参数必须是字符串类型。 接收到一个 Array 实例 - ERR_INVALID_ARG_TYPE - The “from” argument must be of type string. Received an instance of Array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM