Gallion
DIAdiscord.js - Imagine an app
•Created by Gallion on 11/5/2024 in #djs-questions
Loading Files synchronously
Hello!
I am currently making a slash command to load in a txt file and place the lines in an object and then manipulate the object.
Everything in the code is running as expected. There is only just one problem - the execute function for the slash command does not wait for my file to close. The consequence of this is that the object is not ready to be manipulated when the next function is called, and thus crashes the application.
I am using fs.readstream to read the file
Here is the binpath to the slash command file: https://pastebin.com/wEwdrxLQ
The crash is expected of the behavior described above. I get
TypeError: Cannot read properties of undefined (reading '[random int]')
From testing, what I believe is happening is that in getTracks
, the function goes through once, returns undefined, and then goes onto the next function. The function does not stay in the readstream as long as the file is open.
One solution would be to put all the post-processing functions inside the close event, but that would be rather clunky and tedious
Another solution I tried was to make execute
asynchronous and put await in front of the call to getTracks
. This did not work.
A final solution I tried was to put the read events in a while loop with a sentinel that would be changed on the close event. This only prevented from any events being able to be read. Thank you for reading this!5 replies