【问题标题】:discord.js bot playing audio but no sound?discord.js 机器人播放音频但没有声音?
【发布时间】:2021-12-08 11:23:34
【问题描述】:

我打算为我的 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`)
                }
            })
        }

感谢阅读!

【问题讨论】:

  • 这能回答你的问题吗? Play local music files using djs v13
  • @MegaMix_Craft OP 似乎正在使用 v12,您可以通过他们加入 VoiceChannel.join 来判断这一点
  • @MegaMix_Craft 我试过了,但没有用,将代码中的行更改为 `connection.play(path.join('..', 'musicfiles', 'Bastille_Pompeii.mp3' ))`,因为该文件称为 Bastille_Pompeii.mp3 并且位于我所在脚本的父文件夹内的文件夹“musicfiles”中
  • @MrMythical 我正在使用 discord.js v13
  • @MrMythical 我不知道什么时候做的,但它对我有用,所以显然它没有被删除?

标签: javascript node.js ffmpeg discord discord.js


【解决方案1】:

这段代码应该适合你:

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'));

【讨论】:

  • 我在需要“@discordjs/voice”模块时被困在这里,我尝试了npm install discordjs/voicenpm install @discordjs/voice,但都没有成功。相反,这返回了以下错误:Error: Cannot find module '/Users/macci/Documents/Programming/JS/CBbot/node_modules/@discordjs/voice/dist/index.js'. Please verify that the package.json has a valid "main" entry
  • @MadMisty 这个错误清楚地表明你应该检查package.jsonPlease verify that the package.json has a valid "main" entry
  • 主条目已定义(据我所知应该如此)"main": "dist/index.js",
  • 终于成功了,谢谢! (不得不重新安装包,第一次安装显然出了点问题,使用相同的命令将其安装到错误的路径)
猜你喜欢
  • 2021-11-22
  • 2022-01-20
  • 2022-01-04
  • 2021-09-03
  • 2020-03-16
  • 1970-01-01
  • 2021-10-25
  • 1970-01-01
  • 2020-06-06
相关资源
最近更新 更多