how to set and track audio player states?
hi there, im learning data structures and algorithms and i thought it would be a nice exercise using them in a discord bot that takes a local folder and creates a track instance and plays it according to an initialized DSA... the thing is, i need to keep track of the player status, but it seems that
if (player.state === AudioPlayerStatus.__)
have no overlap and it complains, ive seen some player.on(___, async () =>)
implementations but looks confusing and verbose, is there any way to properly track and set player states to ensure the behaviour of a queue, stack or whatever?
12 Replies
• What's your exact discord.js
npm list discord.js
and node node -v
version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.Node 18.16.0
how can i make if statements with player.status if AudioPlayerStatus
has no overlap and cant call AudioPlayerState
enums since are types and not values?Use player.state.status
i figured out that, but now i have the same issue, how can i track the state between resources?
this generates an "infinite" loop where the bot constantly sends follow ups to the original command request but also does the job of keep playing until the queue is empty, how can i either:
a. keep track constantly of the state of the player and change it accordingly, or
b. send only and only one interaction even on a loop?
c all of the above? haha
oh, i have to remove it, came from the first blogs i was following that are by now fairly deprecated, my bad
thank you for noticing it!
You should be listening to the AudioPlayer.stateChange event instead of the while loop
how can i do that? ive seen implementations with
player.on('stateChange'
but couldnt understand how to then swap the already played resource with one that is pending in the queueSwap?
You just play the new resource
i mean like, continue the queue
Pop the queue, then play the song
and whats the syntax of the
stateChange
? is with the player.on
or is there another way?It’s an event that provides the old and new state
Normal event emitter syntax
do you have an example snippet that i can check? when i access
AudioPlayer.stateChange
the property doesnt exist, and .on
of course is available since is the class that creates the player
but im farily lost and confused by now, im so sorry for all the hassle 😭
i did it! although its not the most clean code and im sure its reduntand af, but couldnt figure out how to track the state without creating a loop that emptied the queue when changing state or plain out making a queue underflow, thanks for all the patience and sorry if i came out as lazy or frustratingly dumb, i was really tired since it was late here, thanks once again for the guidance this was my implementation
i just wanted to bother with a last question, is there any way to clean up or refactor the code so its more concise and DRY?