Home > Net >  discord.js bot playing audio but no sound?
discord.js bot playing audio but no sound?

Time:10-22

I was planning to add some voice features to my discord bot, but for some reason when I run my code, it doesn't make sound while discord does show the green circle around my bot, showing that it's making sound. It also does't log any errors or anything else, does anyone know what's wrong with my code?

Code:

        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`)
                }
            })
        }

Thanks for reading!

CodePudding user response:

This code should work for you:

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)

As you said, you tried to play file named Bastille_Pompeii.mp3, so you need to define resource like this: let resource = createAudioResource(join('./musicfiles/', 'Bastille_Pompeii.mp3'));

  • Related