我正在使用Discord.js和ytdl开发Discord机器人。但是,每当我键入?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.
这些代码都是从我制作的另一个机器人中复制的,所以我不确定为什么突然出现错误。
这是代码:
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: []
});
答案 0 :(得分:0)
args[1]
在运行代码时未定义,这意味着从不提供。这也会导致url
也未定义,然后由于validateURL()
需要一个字符串而从ytdl抛出错误。
在继续执行命令之前,请确保存在必需的参数。如果不是,则返回错误消息。例如...
if (!args[1]) {
return message.channel.send('You must provide a URL!')
.catch(console.error);
}