async function play(msg) {
let mg = getMusicGuild(msg.guild.id);
let voiceChannel = msg.guild.voiceConnection;
if (!voiceChannel) voiceChannel = await msg.member.voiceChannel.join();
msg.guild.me.setDeaf(true);
let sq = mg.shiftQueue();
mg.setDispatcher(voiceChannel.play(ytdl(sq.youtube.link,{filter:"audioonly"}),{passes:4}));
mg.setPlayingTitle(sq.youtube.title);
mg.getDispatcher().on('end',reason => {
setTimeout(()=>{
let sqa = mg.queue[0];
mg.setPlaying(false);
if (!sqa) {
console.log("music ended");
voiceChannel.disconnect();
} else {
play(msg).catch(console.error);
}
},500);
});
mg.setPlaying(true);
msg.channel.send(new MessageEmbed()
.setColor(0x0ea5d3)
.setAuthor(msg.author.username, msg.author.avatarURL)
.setDescription(sq.youtube.description)
.setTimestamp(new Date())
.setTitle(sq.youtube.title)
.setImage(sq.youtube.thumbnails.high.url)
.setURL(sq.youtube.link)
);
}
module.exports = {
play: play,
searchAddToQueue: searchAddToQueue,
getMusicGuild: getMusicGuild
};
这是允许我的机器人加入语音频道和播放音乐的代码。但是,当我执行允许所有这些工作的命令时,会出现此错误:
(node:2668) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: voiceChannel.play is not a function
正如你所知,我坚持这一点。我在代码中做错了什么?
答案 0 :(得分:1)
我可以看到你正在做一个音乐机器人,就个人而言,这对音乐来说似乎并不高效。如果你想拥有一个效果更好的高级音乐机器人,并且有很多难以实现的功能,I recommend this.如果你仍然希望使用自己的代码使音乐功能发挥作用,我建议你做一个与此类似的功能:
function play(connection, message){
var server = servers[message.guild.id];
server.dispatcher = connection.playStream(ytdl(server.queue[0], {filter:
"audioonly"}));
server.queue.shift();
server.dispatcher.on("end", function() {
if(server.queue[0]) play(connection, message);
else connection.disconnect();
});
}
请注意,这是使用FFmpeg
和opusscript