gpt4 book ai didi

javascript - discord.js 机器人播放音频但没有声音?

转载 作者:行者123 更新时间:2023-12-04 23:19:22 24 4
gpt4 key购买 nike

我打算为我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com