如何创建一个仅将本地音频文件播放到您的频道中的 Discord 机器人?

How to create a Discord bot which simply plays local audio files into your channel?

我想知道如何创建这样一个 Discord 机器人,播放存储在我的 PC 中的音乐文件(而不是来自任何互联网资源,如 youtube...)。 我阅读了 解决方案,但必须对 discord.js v12

进行一些更正

所以这是我为 discord v12 制作的简单代码。 (请注意,我是javascript的新手,所以如果有任何错误,请告诉我)

settings.json

{
  "token" : "{YOUR TOKEN}",
  "filesDir" : "./files/",
  "commandPlay" : ["!playLocal", "!playlocal"],
  "commandStop" : ["!stopLocal", "!stoplocal"],
  "commandList" : ["!playLocal list", "!playlocal list"],
  "warningPlayArgsSentence" : "Aucun argument renseigné, veuillez informer du titre du fichier à jouer. Exemple : !playLocal FoxMix",
  "filesListSentence" : "Liste des fichiers .mp3 disponibles : ",
  "warningFolderNotFoundSentence" : "Ce répertoire semble être impossible à scanner : " 
}

index.js

const settings = require('./settings.json')
const Discord = require('discord.js')
const fs = require('fs');

var isReady = true;

const bot = new Discord.Client();

function startsWithInList(message, list) {
  var found = false;
  list.forEach(function (item) {
    if( message.startsWith(item) ) {
      found = true;
    }
  })
  return found;
}

function foundsInList(message, list) {
  var found = false;
  list.forEach(function (item) {
    if( message === item ) {
      found = true;
    }
  })
  return found;
}

bot.on( 'message', message => {
  if( foundsInList(message.content, settings.commandList) ) {
    fs.readdir( settings.filesDir, function (err, files) {
      if(err) {
        return message.channel.send(settings.warningFolderNotFoundSentence+settings.filesDir);
      } 
      let listTxt = "";
      files.forEach(function (file) {
        if( file.endsWith('.mp3') || file.endsWith('.MP3') ) {
          listTxt += `\`${file.split('.')[0]}\` `;
        }
      })
      message.channel.send(settings.filesListSentence);
      message.channel.send(listTxt);
    })
    return;
  }
  if( isReady && startsWithInList(message.content, settings.commandPlay) ) {
    isReady = false;
    const args = message.content.slice(10).trim().split(' ');
    if( args.length != 1 || !args[0] || args[0] === "" ) {
      return message.channel.send(settings.warningPlayArgsSentence);
    }
    var voiceChannel = message.member.voice.channel;
    voiceChannel.join().then( connection => {
      const dispatcher = connection.play(settings.filesDir+args[0]+'.mp3')
      dispatcher.on('finish', () => {
        voiceChannel.leave();
        isReady = true;  
      })
    });
  }
  if ( startsWithInList(message.content, settings.commandStop) ) {
    message.member.voice.channel.leave();
    isReady = true;
  }
})

bot.login(settings.token);

对于那些迷路但从未制作过机器人的人:

  1. 在网上查找“如何创建 discord 机器人”
  2. 下载 ffmpeg 二进制文件(将放在工作文件夹中)+ 从网络安装 node.js
  3. 运行 在 Windows PowerShell

npm init

npm install discord.js

npm install -g windows-build-tools

npm install node-opus

最后,在工作目录中创建一个名为“files”的文件夹,并将您的 .mp3 文件放入其中。 运行 和

node .\index.js

并测试“!playLocal musicFile”discord 评论