Discord JS音乐机器人无法播放音乐

时间:2020-06-25 05:00:33

标签: node.js discord.js ytdl

我有一个可以播放音乐的代码。但是,当我实际键入命令时,该机器人加入了VC,但没有播放任何内容,并且弹出了错误代码。 “(node:7888)UnhandledPromiseRejectionWarning:TypeError:无法设置未定义的属性“歌曲” 停下来” 我已经通过在命令行中输入“ npm install discord.js ffmpeg fluent-ffmpeg @ discordjs / opus ytdl-core --save”安装了所有必需的东西。有人知道导致错误的原因吗?

const Discord = require('discord.js');
const ytdl = require('ytdl-core');
const client = new Discord.Client();
const queue = new Map();

client.on("message", async message => {
 if (message.author.bot) return;
 if (!message.content.startsWith('!')) return;

 const serverQueue = queue.get(message.guild.id);

 if (message.content.startsWith(`!play`)) {
   execute(message, serverQueue);
   return;
 } else if (message.content.startsWith(`!skip`)) {
   skip(message, serverQueue);
   return;
 } else if (message.content.startsWith(`!stop`)) {
   stop(message, serverQueue);
   return;
 }
});

async function execute(message, serverQueue) {
 const args = message.content.split(" ");

 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 = await ytdl.getInfo(args[1]);
 const song = {
   title: songInfo.title,
   url: songInfo.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 = await 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!"
   );
 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}**`);
}

0 个答案:

没有答案