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

标签 javascript node.js ffmpeg discord discord.js

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

相关文章:

javascript - 动画.css : Animate on hover script

javascript - 在 Node.js 中维护 session

javascript - 无法向 Node 服务器发出发布请求

javascript - 在 JavaScript 中获取我的 Web 应用程序基本 URL

ffmpeg - 使用 ffmpeg 和 drawtext 过滤器时输出质量差

linux - 将经过处理的视频从 OpenCV 传递到 FFmpeg 以进行 HLS 流式传输 (Raspberry PI)

javascript - 灯箱覆盖在关闭后仍然存在,页面 100% 高度

javascript - Objective-C 方法/函数调用。 (来自 JavaScript 示例)

c# - 使用 jquery 将客户端事件绑定(bind)到动态数据列表项

animation - 如何在 ffmpeg 中使用时间线编辑单个图像输入?