javascript - 需要帮助制作 Java 脚本 Discord 音乐机器人

标签 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);

youtube.js 文件

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;

};

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

加入将军

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

js:80

抛出新的错误(

^

错误:您的系统上未找到 FFMPEG,因此无法播放音频。请做出来 确保 FFMPEG 已安装并位于您的 PATH 中。

最佳答案

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

确保将其作为系统变量,如下所示: Screenshot

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

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

关于javascript - 需要帮助制作 Java 脚本 Discord 音乐机器人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41983317/

相关文章:

python - 如何使用python设置视频时长?

javascript - 加载时交替背景图像

java - 将MP4转换为WAV

android - 在android中启动时找不到audio-hal-2-0

qt - 从 QMediaPlayer 获取 QAudioBuffer

php - 有没有办法在 PHP 中继承 ffmpeg_movie 类?

php - 我是否需要安装 ffmpeg-php 才能使用 ffmpeg?

javascript - 使用 d3.js 从画笔选择返回表格数据

javascript - 使用 sinon 测试 javascript apply 方法

javascript - 在排序数组中换行