使用discord.js的Discord bot无法播放mp3文件,未给出错误

时间:2020-05-18 13:10:07

标签: javascript bots discord.js

我的代码到达第24行const dispatcher = connection.play('/sounds/Pigstep.mp3', { volume: 0.9 });,我知道因为第26行的console.log('audio is now playing!')被执行了。该漫游器确实进入了语音通道,但没有播放歌曲。我在终端中没有收到错误消息。

我的机器人有权加入语音频道并讲话。 我已经安装了ffmpeg并将其添加到路径。

这是我的代码:

const Discord = require('discord.js');
const auth = require('./auth.json');
const ytdl = require('ytdl-core');

const client = new Discord.Client();

client.login(auth.token);

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}`);
    console.log('Ready!');
});

client.on('message', async msg => {
    if (msg.content.substring(0, 1) === '!') {
        console.log('Message received!');
        let args = msg.content.substring(1).split(' ');
        const cmd = args[0];

        args = args.splice(1);
        switch (cmd) {
            case 'cricket':
                var connection = await msg.member.voice.channel.join();
                const dispatcher = connection.play('/sounds/Pigstep.mp3', { volume: 0.9 });
                dispatcher.on('start', () => {
                    console.log('audio is now playing!');
                });
                console.log('Playing!');
                break;
        }
    }
});

在此先感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

游戏需要绝对路径。另外,请确保您的代码中文件的目录正确

connection.play(require("path").join(__dirname, '/sounds/Pigstep.mp3'));