我打算为我的 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
的文件,所以你需要定义resource
像这样:let resource = createAudioResource(join('./musicfiles/', 'Bastille_Pompeii.mp3'));
关于javascript - discord.js 机器人播放音频但没有声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69666315/