这是我具有的机器人音乐功能的代码
// MORE CODE FOR MUSIC FEATURES IM PLAYING WITH
var servers = {};
function play(connection, message) {
var server = servers[message.guild.id];
const dispatcher = connection.play(ytdl(server.queue[0], { filter:
"audioonly" }));
server.dispatcher = connection.play(ytdl(server.queue[0], { filter:
"audioonly" })).then(()=> {
playing = true;
});
dispatcher.on('error', console.error);
server.dispatcher.setVolume(0.2);
server.dispatcher.on("end", function () {
server.queue.shift();
if (server.queue[0]) play(connection, message);
else connection.disconnect().then(()=> {
playing = false;
});
});
}
Crashbot.on('message', async message =>{
let args = message.content.substring(PREFIX.length).split(" ");
switch (args[0]) {
case "play":
if (!args[1]) {
message.reply(`You need to put a link after ${PREFIX}play to add a song to the queue`)
return;
}
if (!message.member.voice.channel) {
message.channel.send("You must be in a voice channel to use this command")
return;
}
if (!servers[message.guild.id]) servers[message.guild.id] = {
queue: []
}
var server = servers[message.guild.id];
if (playing) {
server.queue.push(args[1]);
return;
}
if (!message.guild.voiceConnection) message.member.voice.channel.join().then(function (connection) {
play(connection, message);
});
break;
case "skip":
var server = servers[message.guild.id];
if (server.dispatcher) server.dispatcher.end();
break;
case "stop":
var server = servers[message.guild.id];
if (message.guild.voiceConnection) message.guild.voiceConnection.disconnect().then(()=> {
playing = false;
});
break;
}
})
,这就是我尝试使用音乐功能时不断遇到的错误。我已经仔细阅读了几次代码,无法弄清是什么原因造成的(我对编码很陌生,所以我对自己的编码不太了解,所以可能与它有很大关系)>
(node:20560) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received undefined
at validateString (internal/validators.js:120:11)
at Url.parse (url.js:159:3)
at Object.urlParse [as parse] (url.js:154:13)
at Object.exports.getURLVideoID (E:\Crashbot\node_modules\ytdl-core\lib\util.js:252:22)
at Object.exports.getVideoID (E:\Crashbot\node_modules\ytdl-core\lib\util.js:285:20)
at Function.exports.<computed> [as getInfo] (E:\Crashbot\node_modules\ytdl-core\lib\info.js:320:19)
at ytdl (E:\Crashbot\node_modules\ytdl-core\lib\index.js:17:8)
at play (E:\Crashbot\index.js:435:40)
at E:\Crashbot\index.js:485:17
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:20560) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:20560) [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.