【问题标题】:Discord bot using discord.js not playing mp3 file, no errors givenDiscord bot 使用 discord.js 不播放 mp3 文件,没有给出错误
【发布时间】:2020-09-04 07:05:52
【问题描述】:

我的代码到达第 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;
        }
    }
});

提前感谢您的帮助:)

【问题讨论】:

  • 第22行是哪一行?
  • 这是console.log('audio is now playing!');第26行吗?
  • 是的,我也会将其添加到问题中
  • 你安装了FFMPEG吗?
  • @EvanSmith 是的,我已经安装并添加到我的路径中,在我收到它无法找到 ffmpeg 的错误之前,但通过安装它已解决。

标签: javascript bots discord.js


【解决方案1】:

播放需要绝对路径。还要确保您的代码中有正确的文件目录

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-20
    • 2021-11-18
    • 1970-01-01
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 2021-11-05
    相关资源
    最近更新 更多