在公会频道播放音频文件(不适用于 Ubuntu 服务器)

Play audio files on a guild channel (Not working on Ubuntu Server)

我想在我的 Discord Bot 上制作一个 "soundboard"

该机器人目前 运行 在 Ubuntu 服务器 18.04 上 VPS 主机。

我通过 aptitude apt-get install ffmpeg 安装了 ffmpeg,并通过 在我的项目中安装了相应的节点模块npmnpm install ffmpeg-binaries --savenpm install node-opus --save

我有这个临时代码:

//Command syntax: !play (sound)
if (!args[0]) return message.channel.send('noCorrectSyntax'); //args is provided by module.run

let sound = args[0];
let isReady = true;

if (isReady) {
    isReady = false;
    let voiceChannel = message.member.voiceChannel;
    if (!voiceChannel) return message.channel.send('noChannel');

    voiceChannel.join().then(connection =>{
        const dispatcher = connection.playFile(`../resources/audios/${sound}.mp3`);
        if (!dispatcher) return message.channel.send('soundNotFound');

        console.log(`Playing ${sound} on ${message.member.voiceChannel.name}`);

        dispatcher.on("end", end => {
            voiceChannel.leave();
            console.log(`Finished`);
        });
    }).catch(err => console.log(err));
    isReady = true;
} else {
    return message.channel.send('botNotAvailable');
}

当我的机器人加入语音室时,它没有播放声音就立即离开了。

我这样安装ffmpeg编解码器是不是做错了什么? VPS 有问题吗?

(我尝试了一个新的机器人,在 windows 上安装了 ffmpeg 并设置了环境变量路径,它工作正常)

编辑:

我在 StreamDispatcher(如 https://discord.js.org/#/docs/main/stable/class/StreamDispatcher?scrollTo=e-error 中指定)上监听了 "error" 和 "debug" 事件,但我没有收到错误或调试信息。

当我监听 "speaking" 事件时,在我的控制台上显示为 false

解决方案:

问题是我没有注意进程指定的工作目录。我认为它会从文件系统上的实际文件位置开始工作。

我在单独的文件上使用 index.js 到 运行 命令,如下所示:

let commandFile = require(`./commands/${command}`);
commandFile.run(discord, fs, etc ...);

在每个命令文件中我都放置了这段代码:

exports.run = async (discord, fs, etc ...) => {CODE FOR THE COMMAND};

这就是问题所在。正如 Splingush#7845 在官方 Discord.js 支持服务器 上解释的那样: "Sounds like you had an issue with relative paths, when you do ../resources/audios/${sound}.mp3"

来自 fs: "Relative paths will be resolved relative to the current working directory as specified by process.cwd()" (https://nodejs.org/dist/latest-v10.x/docs/api/fs.html#fs_file_paths).

然后,我唯一要做的就是将这一行重写为如下所示:

const dispatcher = connection.playFile(`./resources/audios/${sound}.mp3`);