Skip to content
Advertisement

Problems playing audio files in Discord

So, It’s been two long days traying to figure what the reck is going on… I’m creating a Bot to my Discord Channel that plays an audio.mp3 when a command is written, like !laugh then the bot should enter the voice channel and reproduce laugh.mp3. I’ve tried in so manny different ways but the bot keeps entering the channel, the green circle quickly appears but no sound is played…

const { join } = require('path');
const { joinVoiceChannel,  createAudioPlayer,  createAudioResource, getVoiceConnection, entersState, StreamType,  AudioPlayerStatus,  VoiceConnectionStatus, AudioResource } = require("@discordjs/voice");

module.exports = {
   name: 'laugh', 
   aliases: ["l"], 

   run: async(client, message, args) => {

   const player = createAudioPlayer()
   const connection = joinVoiceChannel({
   channelId: message.member.voice.channel.id,
   guildId: message.guild.id,
   adapterCreator: message.guild.voiceAdapterCreator
                     }).subscribe(player)
   let resource = createAudioResource(join('./som/', 'laugh.mp3'));
   player.play(resource)
   player.on(AudioPlayerStatus.AutoPaused, () => {
   player.stop();
         
     });
   }
}

I’ve already tried to caugh error but apparently nothing is wrong, I already have all intents in my index.js.

So, anyone could help me find a solution?

Advertisement

Answer

Past the 3rd day I resolved to find other library I could use to solve the problem, turns out I found a solution using FS!

Instead of only try to play the file, I’ve created a stream and then I just play the stream… In terms of performance that’s not the greatest solution ever, but it works!

That’s how it goes:

   const {createReadStream } = require('fs')
   const { join } = require('path');
   const { joinVoiceChannel,  createAudioPlayer,  createAudioResource, 
   getVoiceConnection, entersState, StreamType,  AudioPlayerStatus,  
   VoiceConnectionStatus, AudioResource } = require("@discordjs/voice");

   module.exports = {
   name: 'laugh', 
   aliases: ["l"], 

   run: async(client, message, args) => {
   const player = createAudioPlayer();
   const connection = joinVoiceChannel({
   channelId: message.member.voice.channel.id,
   guildId: message.guild.id,
   adapterCreator: message.guild.voiceAdapterCreator,
   selfDeaf: false,
                }).subscribe(player);

   let resource = createAudioResource(createReadStream(join(__dirname, 'som/laugh.mp3')), {
   inlineVolume : true
});
   resource.volume.setVolume(0.9);
   console.log(join(__dirname, 'som/laugh.mp3'));

   player.play(resource)
   player.on(AudioPlayerStatus.AutoPaused, () => {
   player.stop();
         
    });

Something I must alert is about Replit not resolving properly Node.js 17 and other dependencies, so to bring your Bot up 24/7 use Heroku and GitHub. To finish the GUILD_VOICE_STATES Intents are required to be set, I was using 32767 but somehow this wasn’t working.

That’s it!

Advertisement