How to write a JSON file with a list of discord.js statuses and import it into the main index.js?
//index.js
what should I write in my JSON file and how should I implement it? I want to put
as a seperate file and call it in my main index.js.
17 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!
- ✅
Marked as resolved by OPBasically, this is to reduce clutter all in one single .js file. Any method works, I am open to learning another (better) way too!
In the json file write a key like
statuses
or something and pass the array of ur statuses as it's value. To get the json as an object in ur code u can use the require()
functionHi, if it’s not too much to ask, could you provide a brief example of this?
also I had given the idea of a JSON file on a whim.. if there’s a better way to call the data from a different file please let me know too!
Is this data being read by other files, or only the index file?
id create a js file named Constants and export all of my constants from there
it would be better in this case since u can't use the ActivityType enum in json
^ This is the best route if you intend to use these variables elsewhere, sachoochoo. If you only intend to use these variables in index.js, just announce them at the top of your index file.
even if u only need to use them in 1 file id recommend storing them in a separate file so u have all constants in 1 file therefore if u want to change something in the constants u dont have to look through different files and its just overally cleaner than having ur constants spread among all files but im not an expert
i’ll try it out and give it a shot later! thank you so much
They're both valid options. I tend to keep my constants in the file which they are relevant to, and when I need to use them elsewhere I put them in a constants file. I never forget where they are, because if I need to change them the purpose is immediately relevant, but to each their own.
would a .js file labeled
statuses.js
work if i write this?:
i've just copied and pasted this code from index.js -> statuses.js
does not seem to work, however. i will try to find a solution.looks correct, how does it not work?
u get any errors?
yes:
^ for
index.js
u need to import
statuses
from the statuses.js filemay I see how that would work? i know it would require something along the lines of:
const newStatus = require('./statuses');
if u do
const newStatus = require('./statuses');
u store whatever statuses.js exports in newStatus
variable, in this case it's the array of statuses so
newStatus
is
i think i just got it working right as you said that...
const statuses = require('./statuses');
should be fine, it seems to be working! Let me know if it seems good from a 3rd person perspective.
thank you!