discord.js 不播放音频但没有声音?

discord.js bot playing audio but no sound?

我打算为我的 discord 机器人添加一些语音功能,但由于某些原因,当我 运行 我的代码时,它没有发出声音 而 discord 确实显示绿色圆圈在我的机器人周围,显示它正在发出声音。它也不会记录任何错误或其他任何内容,有人知道我的代码有什么问题吗?

代码:

        if (command == "soundtest") {
            mongo().then(async (mongoose) => {
                const { voice } = msg.member

                if (voice.channelID) {
                    const vc = voice.channel
                    // console.log(channel)
                    try {
                        vc.join().then(connection => {
                            connection.play(path.join(__dirname, 'Bastille_Pompeii.mp3'))
                        })
                    } catch(e) {
                        console.log("error")
                        console.log(e)
                    }
                } else {
                    msg.channel.send(`You must join a voice channel to use this command`)
                }
            })
        }

感谢阅读!

此代码应该适合您:

const { createReadStream } = require('fs');
const { join } = require('path');
const { createAudioResource, StreamType, createAudioPlayer, joinVoiceChannel } = require('@discordjs/voice');
const player = createAudioPlayer()
joinVoiceChannel({
    channelId: message.member.voice.channel.id,
    guildId: message.guild.id,
    adapterCreator: message.guild.voiceAdapterCreator
}).subscribe(player)
let resource = createAudioResource(join('./folder/', '<song_name>.mp3'));

player.play(resource)

正如您所说,您试图播放名为 Bastille_Pompeii.mp3 的文件,因此您需要像这样定义 resourcelet resource = createAudioResource(join('./musicfiles/', 'Bastille_Pompeii.mp3'));