使用ytdl-core和node-opus的Discord JS Music Bot无法播放音乐

时间:2018-06-23 16:35:50

标签: javascript node.js discord.js

我正在尝试使用discord.js,node-opus,ytdl-core和ffmpeg库制作一个不和谐的音乐机器人。

问题是,每当执行命令!play [youtube-url]时,它都会完美加载歌曲,并获得其名称并输出now playing : ${song},这意味着代码中没有错误。但是,一旦漫游器加入我的voiceChannel,它就会迅速断开连接。

它首先输出Now Playing : ${Song-name},然后在加入后很快执行结束函数并输出{Finished Playing : ${Song-name}

任何人都可以帮助我解决此播放问题。它从不播放歌曲。

这是我的完整代码:

const Discord = require('discord.js');
const config = require('../config.json');
const ytdl = require('ytdl-core');

module.exports.run = async (bot, message, args) =>{
  if(!message.member.voiceChannel)
    return message.channel.send("You must be in a voice channel for me to start playing music");
  if(message.guild.me.voiceChannel)
return message.channel.send("Im busy at a different voice channel on the server. Try again later.");

  if(!args[0])
return message.channel.send("Please enter a youtube URL for me to load the song from.");

  let validate = await ytdl.validateURL(args[0]);
  if(!validate)
    return message.channel.send("Whoops, re-check the URL you gave me, I am getting an error while trying to play the song. ");

  let info = await ytdl.getInfo(args[0]);
  let voiceChannel = message.member.voiceChannel;
  let connection = await voiceChannel.join();
  let dispatcher = await connection.playStream(ytdl(args[0], {filter: 'audioonly'}))
    .on("end", end => {
      message.channel.send(`Finished Playing: ${info.title}`);
      voiceChannel.leave();
    })
    .on("error", error => {
      console.error(error);
      message.channel.send("Error Occurred during playback. Try again later.");
    });
  return message.channel.send(`Now Playing: ${info.title}`);
}

module.exports.help = {
  name: "play"
}

0 个答案:

没有答案