【问题标题】:Need Help Making Java Script Discord Music Bot需要帮助制作 Java Script Discord 音乐机器人
【发布时间】:2017-06-18 09:48:45
【问题描述】:

我曾尝试使用此代码制作一个不和谐的音乐机器人,但我收到错误消息,告诉我我需要 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 中。
  • 我已将其添加到我的路径中,但我仍然收到相同的错误

标签: javascript audio ffmpeg bots discord


【解决方案1】:

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

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

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

您可以在网上搜索/google,了解如何为您的特定操作系统添加新的 System/PATH 变量。

【讨论】:

  • 我已完成此操作并将 ffmpeg 添加到我的路径中,但我仍然收到错误消息。 ://
  • @Gambino 请说明您尝试过的任何不起作用的解决方案。
  • 我进入系统、高级系统设置、环境变量、系统变量,并添加了FFMPEG并将值设置为.exe文件所在的位置。然后我重新启动机器人并尝试播放一首歌然后错误再次出现。所以我做了我之前尝试过的事情,进入系统变量,选择名为路径的变量并点击编辑,然后在编辑环境变量窗口中新建并将其定向到包含 ffmpeg.exe 的文件夹。它仍然给了我在问题中提到的错误。
  • 我基本上按照这里的说明进行操作 (wikihow.com/Install-FFmpeg-on-Windows)
猜你喜欢
  • 2021-01-16
  • 1970-01-01
  • 1970-01-01
  • 2018-05-18
  • 2021-05-27
  • 2020-09-27
  • 2019-04-30
  • 2021-05-24
  • 2018-07-24
相关资源
最近更新 更多