问题描述
我目前正在创建我的第一个 discordjs 机器人,但遇到了一些问题。我现在想添加播放音乐的功能,但是当我在我的服务器中为我的机器人使用播放命令时,我总是收到错误:“UnhandledPromiseRejectionWarning:错误:找不到视频 ID”。 我已经用谷歌搜索了它,但没有答案对我有帮助...... 代码如下:
async function execute(message,serverQueue,args) {
const voiceChannel = message.member.voice.channel;
if (!voiceChannel)
return message.channel.send(
"You need to be in a voice channel to play music!"
);
const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
return message.channel.send(
"I need the permissions to join and speak in your voice channel!"
);
}
const songInfo = ytdl.getInfo(args);
const song = {
title: songInfo.videoDetails.title,url: songInfo.videoDetails.video_url,};
if (!serverQueue) {
const queueContruct = {
textChannel: message.channel,voiceChannel: voiceChannel,connection: null,songs: [],volume: 5,playing: true
};
queue.set(message.guild.id,queueContruct);
queueContruct.songs.push(song);
try {
var connection = voiceChannel.join();
queueContruct.connection = connection;
play(message.guild,queueContruct.songs[0]);
} catch (err) {
console.log(err);
queue.delete(message.guild.id);
return message.channel.send(err);
}
} else {
serverQueue.songs.push(song);
return message.channel.send(`${song.title} has been added to the queue!`);
}
}
function skip(message,serverQueue) {
if (!message.member.voice.channel)
return message.channel.send(
"You have to be in a voice channel to stop the music!"
);
if (!serverQueue)
return message.channel.send("There is no song that I Could skip!");
serverQueue.connection.dispatcher.end();
}
function stop(message,serverQueue) {
if (!message.member.voice.channel)
return message.channel.send(
"You have to be in a voice channel to stop the music!"
);
if (!serverQueue)
return message.channel.send("There is no song that I Could stop!");
serverQueue.songs = [];
serverQueue.connection.dispatcher.end();
}
function play(guild,song) {
const serverQueue = queue.get(guild.id);
if (!song) {
serverQueue.voiceChannel.leave();
queue.delete(guild.id);
return;
}
const dispatcher = serverQueue.connection
.play(ytdl(song.url))
.on("finish",() => {
serverQueue.songs.shift();
play(guild,serverQueue.songs[0]);
})
.on("error",error => console.error(error));
dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
serverQueue.textChannel.send(`Start playing: **${song.title}**`);
}
解决方法
该错误意味着您的代码中某处 Promise
被拒绝,但您没有为其指定错误处理。我不确定您的哪个函数调用会返回 Promise,但是当您需要处理错误时,您需要将 .catch()
调用添加到 Promise。