Goal
I want to be able to play longer audio files using my discord bot.
Working Code
Using Discord.js and Node.js, I have created a discord bot. So far my code is able to join a voice call and can only play very short audio files <50KB. Which is great, besides the short file part.
Testing Process: to test different audiofiles, I simply replace the path, during the resource creation, to be the desired audio file.
I have tested 5 short files and it works great! But the three long files >~75KB, that I have, do not work. No errors pop up, it just doesn't play.
const { SlashCommandBuilder } = require('@discordjs/builders');
const { joinVoiceChannel } = require('@discordjs/voice');
const { createAudioPlayer , createAudioResource, StreamType} = require('@discordjs/voice');
const { createReadStream } = require('node:fs');
const { join } = require('node:path');
const { VoiceConnectionStatus, AudioPlayerStatus } = require('@discordjs/voice');
module.exports = {
data: new SlashCommandBuilder()
.setName('jensenrage')
.setDescription('Joins channel and plays jensen\'s rage'),
async execute(interaction){
const { joinVoiceChannel } = require('@discordjs/voice');
const channel = interaction.member.voice.channel
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
});
const resource = createAudioResource(createReadStream(join(__dirname, '../AudioFiles/short.mp3')), {
inlineVolume : true
});
resource.volume.setVolume(0.2);
const player = createAudioPlayer();
connection.subscribe(player);
player.play(resource)
await interaction.reply( "Enjoy!")
}
}
I have looked into why this is, but most of the answers do not explain why the short files play but the long ones do not.
I am assuming that this line is the the issue, but unsure why.
const resource = createAudioResource(createReadStream(join(__dirname, '../AudioFiles/short.mp3')), {
inlineVolume : true
});
These are my dependencies
--------------------------------------------------
Core Dependencies
- @discordjs/voice: 0.8.0
- prism-media: 1.3.2
- prism-media: 1.3.2
Opus Libraries
- @discordjs/opus: 0.5.3
- opusscript: 0.0.8
Encryption Libraries
- sodium: not found
- libsodium-wrappers: 0.7.10
- tweetnacl: not found
FFmpeg
- version: 4.4.1-essentials_build-www.gyan.dev
- libopus: yes