Looping through an array getting input each iteration
I'm trying to loop through an array to get input each iteration of the loop. The issue I'm running into right now is the loop goes right to the end. How can I delay this so that I can get the input at each step of the way rather than instantly jumping to the end? My code is:
As of now, when I input 2 players, foo and bar, in the array in order, the message automatically skips to bar and whichever button I click for bar is updated in by battleState for foo as well.
11 Replies
- 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!Msot likely, using promisified collectors like
await response.awaitMessageComponent(...)
and for...of
loops instead of .forEach()
callbacks would make this possible
Right now, each iteration of toFix.forEach()
just creates collectors, and once that is sucessfully done, it repeats by creating collectors with the next playerwhat does await message component do?
It resolves to a message component interaction that was received on the message, or it rejects if it didnt collect one in your specified
time
It would be blocking since
Would make it so b
never runs until i
is collected, and
would make it so it iterates over 1
, collects i
, and then starts to iterate over 2
to collect a new i
so I use
awaitMessageComponent
in place of createMessageComponentCollector
?You'll need to adjust more than only that, but that's right
so instead of
I do something like
me changing it to awaitMessageComponent has caused the same issue
Have you considered using await
I might be slow
wait where do I even use await here
following the guide on the website it doesnt say where to put await
nor does it use await itself
ok new error, it waits but when I come to second iteration it simply says this interaction failed
I put the await in front of the
response.awaitMessageComponent(...)
the error seems to be because I am not deferring the message reply or update, so I cannot track it past the first loop
I have 0 clue how to approach that
When I interact with it instantly, the second loop comes through but cannot be interacted with
but when I take a while, it doesn't come through at all
So to put things together, the current state of the code is this:
And it runs the first iteration fine, but does not even look at the second iteration
Error thrown is:
when I try to click the second bodyi thought you can't edit interactions after 2-3 seconds without deferring them