[英]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.