The guide also shows it this way but it doesn't open the mp3

No description
55 Replies
d.js toolkit
d.js toolkit15mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
ZeKe
ZeKeOP15mo ago
@ShompiFlen
ShompiFlen
ShompiFlen15mo ago
okay so can you show me your package.json file also share your index.js file so i can see the code in its entirety
ZeKe
ZeKeOP15mo ago
No description
ShompiFlen
ShompiFlen15mo ago
hide any tokens you are missing some dependencies
ZeKe
ZeKeOP15mo ago
But my index consists of hundreds of lines
ShompiFlen
ShompiFlen15mo ago
then share the section of your code where you start using voice related functions
ZeKe
ZeKeOP15mo ago
No description
No description
ShompiFlen
ShompiFlen15mo ago
also read this part https://discordjs.guide/voice/#barebones there are some extra but optional dependencies that you should try installing, for example @discordjs/opus
discord.js Guide
Imagine a guide... that explores the many possibilities for your discord.js bot.
ShompiFlen
ShompiFlen15mo ago
im guessing you installed ffmpeg and added it to the path too right?
ZeKe
ZeKeOP15mo ago
yes
ZeKe
ZeKeOP15mo ago
No description
ZeKe
ZeKeOP15mo ago
Uploading now
ShompiFlen
ShompiFlen15mo ago
libsodium-wrappers is one that i would recommend installing too, although you may have issues installing that one, but give it a try so at this point, what does the bot do and what what it doesn't?, does it connect to the voice channel?
ZeKe
ZeKeOP15mo ago
I got an error downloading the sodium module and could not download it
ShompiFlen
ShompiFlen15mo ago
what does the error say?
ZeKe
ZeKeOP15mo ago
No description
ZeKe
ZeKeOP15mo ago
No description
ZeKe
ZeKeOP15mo ago
Yes, it connects to the audio channel, but it doesn't play anything, it just stops and waits The libsodium-wrappers module downloaded without any problems
ShompiFlen
ShompiFlen15mo ago
ah okay good so as the guide says, you should've at least have one of the encryption libraries installed to use voice in this case you installed libsodium-wrappers so try to play music now
ZeKe
ZeKeOP15mo ago
ZeKe
ZeKeOP15mo ago
My pc is bad sorry
ShompiFlen
ShompiFlen15mo ago
okay we will need to do some debugging here i need you to paste these pieces of code in your script
connection.on('stateChange', (oldState, newState) => {
console.log(`Connection transitioned from ${oldState.status} to ${newState.status}`);
});

player.on('stateChange', (oldState, newState) => {
console.log(`Audio player transitioned from ${oldState.status} to ${newState.status}`);
});
connection.on('stateChange', (oldState, newState) => {
console.log(`Connection transitioned from ${oldState.status} to ${newState.status}`);
});

player.on('stateChange', (oldState, newState) => {
console.log(`Audio player transitioned from ${oldState.status} to ${newState.status}`);
});
then test your bot again disconnect it first
ZeKe
ZeKeOP15mo ago
Trying right away
ShompiFlen
ShompiFlen15mo ago
then run it just paste them right below your player.play call
ZeKe
ZeKeOP15mo ago
wrote very long articles 😅
ShompiFlen
ShompiFlen15mo ago
try to find the beginning of it and send it like the first 10 lines or something or a screenshot of the entire terminal
ZeKe
ZeKeOP15mo ago
No description
ShompiFlen
ShompiFlen15mo ago
okay and how it ends?
ZeKe
ZeKeOP15mo ago
It seemed to go on forever, after 5 seconds i closed it myself from the terminal
No description
ShompiFlen
ShompiFlen15mo ago
alright, the bot is connecting to voice succesfully you are having trouble with the audio resource how long is this mp3?
ZeKe
ZeKeOP15mo ago
7 sec
ZeKe
ZeKeOP15mo ago
No description
ShompiFlen
ShompiFlen15mo ago
there is a slight chance of the audioresource ending before your bot is ready to play it so lets try the following
ZeKe
ZeKeOP15mo ago
But how can i prevent it?
ShompiFlen
ShompiFlen15mo ago
thats where im going at listen to this event
connection.on(VoiceConnectionStatus.Ready, () => {
console.log('The connection has entered the Ready state - ready to play audio!');
});
connection.on(VoiceConnectionStatus.Ready, () => {
console.log('The connection has entered the Ready state - ready to play audio!');
});
inside there, call player.play(resource) this will start playing the resource once your bot is connected and ready to transmit audio not before
ZeKe
ZeKeOP15mo ago
Is it right to do it this way?
No description
ShompiFlen
ShompiFlen15mo ago
sure, but preferably you would put that at the end but it should work either way make sure to import VoiceConnectionStatus
ZeKe
ZeKeOP15mo ago
Isn't that right import?
const { joinVoiceChannel, createAudioResource, createAudioPlayer, VoiceConnectionStatus } = require('@discordjs/voice');
const { joinVoiceChannel, createAudioResource, createAudioPlayer, VoiceConnectionStatus } = require('@discordjs/voice');
ShompiFlen
ShompiFlen15mo ago
yes
ZeKe
ZeKeOP15mo ago
It gave this warning and the bot didn't say anything
No description
ZeKe
ZeKeOP15mo ago
connection.subscribe(player); what does this saturation do? Ohh reaally?? when i put this line inside ready, it worked ohhhh
ShompiFlen
ShompiFlen15mo ago
pretty sure this is the thing that makes the audio player play things through the connection yea it worked because your mp3 is too short, it was probably ending before your bot was ready to transmit audio
ZeKe
ZeKeOP15mo ago
Thank you very much for your kalps
ShompiFlen
ShompiFlen15mo ago
the memory leak thing tho not sure, you should put some other checks in your code so this code doesn't run too frequently you can also change that bit for this await entersState(connection, VoiceConnectionStatus.Ready, 20_000) you have to make the event async though to use await this is the promisified version of connection.on(VoiceConnectionStatus.Ready, () ), you need to pass it the connection, the state you are waiting for, and how long to wait for it or throw an error otherwise
ZeKe
ZeKeOP15mo ago
should I delete this part in ss and write await entersState(connection, VoiceConnectionStatus.Ready, 20_000)
No description
ZeKe
ZeKeOP15mo ago
or only i delete line 60
ShompiFlen
ShompiFlen15mo ago
you can if you want but its not really necessary if you are okay working with that. You would also have to handle the promise rejection of it in case your bot can't reach the ready state okay so if you want to replace that for the promise, you have to delete line 60 and 63 then use this await entersState(connection, VoiceConnectionStatus.Ready, 20_000) and below this you call player.play and connection.subscribe
ZeKe
ZeKeOP15mo ago
I should import entersState, right?
ShompiFlen
ShompiFlen15mo ago
Yes Your code right now is kind of unsafe you should change this at some point and make your bot connect to the voice channel through a command not when it receives a VoiceStateUpdate thats probably why you got the memory leak warning
ZeKe
ZeKeOP15mo ago
School starts in 6 hours, I have to go to bed. Thank you very much for your help kalps
ShompiFlen
ShompiFlen15mo ago
np
ZeKe
ZeKeOP15mo ago
I did this command in the past when i was trying this system, i guess it's your night too. Good night, i am going to bed 🛏️ Thanks again for your help :)
ZeKe
ZeKeOP15mo ago
Should i press this button?
No description
ShompiFlen
ShompiFlen15mo ago
if your issue is resolved and you dont have any more questions then yes, feel free to resolve this post
Want results from more Discord servers?
Add your server