无法通过ytdl-core播放音乐

时间:2018-11-23 22:19:47

标签: node.js discord.js

现在在Google上搜索了大约2天后,我发现在通过ytdl-core播放音乐时,没有人遇到相同的问题。

我测试了DiscordJS文档中针对VoiceConnection.playStream()编写的代码,但这在使用我的代码进行测试时会抛出相同的错误。

所以我的方法实际上是这样的:

function play(connection, message) {
   var server = servers[message.guild.id];

   server.dispatcher = connection.playStream(ytdl(server.queue[0], {filter: "audioonly"}), {seek: 0, volume: 1});
   server.queue.shift();
   server.dispatcher.on("end", function() {
       if (server.queue[0]){
           play(connection, message);
       } else {
           connection.disconnect();
       }
   });
}

在我的计算机上对其进行测试时,它将引发如下错误:

(node:13520) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "file" argument must be of type string. Received type object
  at validateString (internal/validators.js:125:11)
  at normalizeSpawnArguments (child_process.js:394:3)
  at Object.spawn (child_process.js:532:38)
  at new FfmpegProcess (F:\Training\NodeJS\DiscordBot\node_modules\prism-media\src\transcoders\ffmpeg\FfmpegProcess.js:14:33)
  at FfmpegTranscoder.transcode (F:\Training\NodeJS\DiscordBot\node_modules\prism-media\src\transcoders\ffmpeg\Ffmpeg.js:34:18)
  at MediaTranscoder.transcode (F:\Training\NodeJS\DiscordBot\node_modules\prism-media\src\transcoders\MediaTranscoder.js:27:31)
  at Prism.transcode (F:\Training\NodeJS\DiscordBot\node_modules\prism-media\src\Prism.js:13:28)
  at AudioPlayer.playUnknownStream (F:\Training\NodeJS\DiscordBot\node_modules\discord.js\src\client\voice\player\AudioPlayer.js:97:35)
  at VoiceConnection.playStream (F:\Training\NodeJS\DiscordBot\node_modules\discord.js\src\client\voice\VoiceConnection.js:478:24)
  at play (F:\Training\NodeJS\DiscordBot\commands\voice\play.js:11:36
(node:13520) 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(). (rejection id: 1)
(node:13520) [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.

我使用opusscriptffmpeg-binaries作为依赖项。

我凭我的知识无法获得解决方案,因此希望任何人都可以帮助我解决该问题。 预先感谢!

1 个答案:

答案 0 :(得分:0)

遗憾的是,我无法真正修复您的代码,但我为您提供了一个很好的例子。

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


module.exports.run = async (client, message, args, ops) => {

  if (message.guild.me.voiceChannel) return message.channel.send('Sorry the bot is already connected on a voice channel.'); 
// if the bot user is already connected to any of the guild's voice channels, this message will show up.

  if (!message.member.voiceChannel) return message.channel.send('Please connect to a voice channel.');
// if the member that sent the message isn't connected to any voice channel, this message will show up. (note: `message.author` can't be used here)

  if (!args[0]) return message.channel.send('Sorry, please insert a valid URL.');
// if there are no arguments (url in this case) mentioned following the command, this message will show up.

  let valid = await ytdl.validateURL(args[0]);
// awaiting url validation.

  if (!valid) return message.channel.send('Sorry, please insert a valid URL.');
// if the url isn't valid, this will show up.

  let info = await ytdl.getInfo(args[0]);
// awaiting info. (getting audio file from the url)

  let connect = await message.member.voiceChannel.join();
// connects in the same channel as the `message.author`. (note: message.member is used here because there's no such module as `message.author.voiceChannel`)

  let dispatcher = await connect.playStream(ytdl(args[0], {filter:'audioonly'}));
// awaiting stream. (audio only output)

  message.channel.send(`Current Song: ${info.title}`);
// sends the song's title in the channel where the command was run.
}

module.exports.help = {
  name: "play"
}
// name of the command