需要帮助制作Java Script Discord Music Bot

时间:2017-02-01 15:20:05

标签: javascript audio ffmpeg bots discord

我曾尝试使用此代码制作不和谐音乐机器人,但我收到错误告诉我需要ffmpeg,但我如何在此代码中实现它?

index.js文件



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

var youtube = require('./youtube.js');


var ytAudioQueue = [];
var dispatcher = null;

bot.on('message', function(message) {
    var messageParts = message.content.split(' ');
    var command = messageParts[0].toLowerCase();
    var parameters = messageParts.splice(1, messageParts.length);

    switch (command) {

        case "-join" : 
        message.reply("Attempting to join channel " + parameters[0]);
        JoinCommand(parameters[0], message);
        break;
    
        case "-play" :
        PlayCommand(parameters.join(" "), message);
        break;

        case "-playqueue":
        PlayQueueCommand(message);
        break;
    }
});


    function PlayCommand(searchTerm) {
        if(bot.voiceConnections.array().length == 0) {
            var defaultVoiceChannel = bot.channels.find(val => val.type === 'voice').name;
            JoinCommand(defaultVoiceChannel);
        }
        youtube.search(searchTerm, QueueYtAudioStream);
    }

    function PlayQueueCommand(message) {
        var queueString = "";

        for(var x = 0; x < ytAudioQueue.length; x++) {
            queueString += ytAudioQueue[x].videoName + ", ";
        }
        queueString = queueString.substring(0, queueString.length - 2);
        message.reply(queueString);
    }

    function JoinCommand(ChannelName) {
        var voiceChannel = GetChannelByName(ChannelName);

        if (voiceChannel) {
            voiceChannel.join();
            console.log("Joined " + voiceChannel.name);
        }
        
        return voiceChannel;
        
    }

    /* Helper Methods */

    function GetChannelByName(name) {
        var channel = bot.channels.find(val => val.name === name);
        return channel;
    }

  

    function QueueYtAudioStream(videoId, videoName) {
        var streamUrl = youtube.watchVideoUrl + videoId;

        if (!ytAudioQueue.length) {
            ytAudioQueue.push(
                {
                    'streamUrl' : streamUrl,
                    'videoName' : videoName
                }
            );
            console.log('Queued audio ' + videoName);
            PlayStream(ytAudioQueue[0].streamUrl);
        }
        else {
            ytAudioQueue.push(
                {
                    'streamUrl' : streamUrl,
                    'videoName' : videoName
                }
            );
        }
        console.log("Queued audio " + videoName);
    }

    function PlayStream(streamUrl) {
        const streamOptions = {seek: 0, volume: 1};

        if (streamUrl) {
            const stream = ytdl(streamUrl, {filter: 'audioonly'});

            if (dispatcher == null) {
                var voiceConnection = bot.voiceConnections.first();

                if(voiceConnection) {
                    console.log("Now Playing " + ytAudioQueue[0].videoname);
                    dispatcher = bot.voiceConnections.first().playStream(stream, streamOptions);

                    dispatcher.on('end', () => {
                        dispatcher = null;
                        PlayNextStreamInQueue();
                    });

                    dispatcher.on('error', (err) => {
                        console.log(err);
                    });
                }
            } else {
                dispatcher = bot.voiceConnections.first().playStream(stream, streamOptions);
            }
            
        }
    }

    function PlayNextStreamInQueue() {
        ytAudioQueue.splice(0, 1);

        if (ytAudioQueue.length != 0) {
            console.log("now Playing " + ytAudioQueue[0].videoName);
            PlayStream(ytAudioQueue[0].streamUrl);
        }
    }


bot.login(config.token);
&#13;
&#13;
&#13;

youtube.js文件

&#13;
&#13;
var request = require('superagent');

const API_KEY = "My API KEY";
const WATCH_VIDEO_URL = "https://www.youtube.com/watch?v=";

exports.watchVideoUrl = WATCH_VIDEO_URL;

exports.search = function search(searchKeywords, callback) {
  var requestUrl = 'https://www.googleapis.com/youtube/v3/search' + '?part=snippet&q=' + escape(searchKeywords) + '&key=' + API_KEY;

  request(requestUrl, (error, response) => {
    if (!error && response.statusCode == 200) {

      var body = response.body;
      if (body.items.length == 0) {
        console.log("I Could Not Find Anything!");
        return;
      }
      for (var item of body.items) {
        if (item.id.kind == 'youtube#video') {
          callback(item.id.videoId, item.snippet.title);
          return;
        }
      }
    } else {
      console.log("Unexpected error!");
      return;
    }
  });

  return;

};
&#13;
&#13;
&#13;

我尝试运行代码时遇到错误:

加入将军

C:\ Discord Bot \ node_modules \ discord.js \ src \ client \ voice \ pcm \ FfmpegConverterEngine。

JS:80

抛出新错误(

^

错误:系统上找不到FFMPEG,因此无法播放音频。请做出来  确定已安装FFMPEG并在PATH中。

1 个答案:

答案 0 :(得分:0)

您需要下载/解压缩FFMPEG并将其作为PATH / System变量[环境变量]

确保将它作为您的System变量,如下所示: Screenshot

系统变量应命名为“FFMPEG”,并应指向执行(.exe)文件的位置。它应该位于FFMPEG文件夹中名为“bin”的文件夹中。

您可以在线搜索/在线搜索如何为特定操作系统添加新的系统/ PATH变量。