How to write a JSON file with a list of discord.js statuses and import it into the main index.js?

//index.js
const statuses = [
{ name: "ABC", type: ActivityType.Streaming },
{ name: "DEF", type: ActivityType.Watching },
{ name: "GHI", type: ActivityType.Listening },
{ name: "JKL", type: ActivityType.Playing },
];
client.on('ready', (c) => {
console.log(`✅ ${c.user.tag} is online!`);
setInterval(() => {
var newStatus = statuses[Math.floor(Math.random() * statuses.length)];
client.user.setActivity(newStatus);
}, 10000);
});
const statuses = [
{ name: "ABC", type: ActivityType.Streaming },
{ name: "DEF", type: ActivityType.Watching },
{ name: "GHI", type: ActivityType.Listening },
{ name: "JKL", type: ActivityType.Playing },
];
client.on('ready', (c) => {
console.log(`✅ ${c.user.tag} is online!`);
setInterval(() => {
var newStatus = statuses[Math.floor(Math.random() * statuses.length)];
client.user.setActivity(newStatus);
}, 10000);
});
what should I write in my JSON file and how should I implement it? I want to put
const statuses = [
{ name: "ABC", type: ActivityType.Streaming },
{ name: "DEF", type: ActivityType.Watching },
{ name: "GHI", type: ActivityType.Listening },
{ name: "JKL", type: ActivityType.Playing },
];
const statuses = [
{ name: "ABC", type: ActivityType.Streaming },
{ name: "DEF", type: ActivityType.Watching },
{ name: "GHI", type: ActivityType.Listening },
{ name: "JKL", type: ActivityType.Playing },
];
as a seperate file and call it in my main index.js.
17 Replies
d.js toolkit
d.js toolkit11mo 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
sachoochoo
sachoochooOP11mo ago
Basically, this is to reduce clutter all in one single .js file. Any method works, I am open to learning another (better) way too!
xx_lavaboy_xx123
xx_lavaboy_xx12311mo ago
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() function
sachoochoo
sachoochooOP11mo ago
Hi, 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!
BoxWithout
BoxWithout11mo ago
Is this data being read by other files, or only the index file?
xx_lavaboy_xx123
xx_lavaboy_xx12311mo ago
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
BoxWithout
BoxWithout11mo ago
^ 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.
xx_lavaboy_xx123
xx_lavaboy_xx12311mo ago
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
sachoochoo
sachoochooOP11mo ago
i’ll try it out and give it a shot later! thank you so much
BoxWithout
BoxWithout11mo ago
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.
sachoochoo
sachoochooOP11mo ago
would a .js file labeled statuses.js work if i write this?:
require('dotenv').config();
const { ActivityType } = require('discord.js');

const statuses = [
{ name: "ABC", type: ActivityType.Streaming },
{ name: "DEF", type: ActivityType.Watching },
{ name: "GHI", type: ActivityType.Listening },
{ name: "JKL", type: ActivityType.Playing },
];

module.exports = statuses;
require('dotenv').config();
const { ActivityType } = require('discord.js');

const statuses = [
{ name: "ABC", type: ActivityType.Streaming },
{ name: "DEF", type: ActivityType.Watching },
{ name: "GHI", type: ActivityType.Listening },
{ name: "JKL", type: ActivityType.Playing },
];

module.exports = statuses;
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.
xx_lavaboy_xx123
xx_lavaboy_xx12311mo ago
looks correct, how does it not work? u get any errors?
sachoochoo
sachoochooOP11mo ago
yes:
var newStatus = statuses[Math.floor(Math.random() * statuses.length)];
^

ReferenceError: statuses is not defined
at Timeout._onTimeout (/Users/satchitseth/StudySphere/src/index.js:17:21)
at listOnTimeout (node:internal/timers:573:17)
at process.processTimers (node:internal/timers:514:7)
var newStatus = statuses[Math.floor(Math.random() * statuses.length)];
^

ReferenceError: statuses is not defined
at Timeout._onTimeout (/Users/satchitseth/StudySphere/src/index.js:17:21)
at listOnTimeout (node:internal/timers:573:17)
at process.processTimers (node:internal/timers:514:7)
client.on('ready', (c) => {
console.log(`✅ ${c.user.tag} is online!`);
setInterval(() => {
var newStatus = statuses[Math.floor(Math.random() * statuses.length)];
client.user.setActivity(newStatus);
}, 10000);
});
client.on('ready', (c) => {
console.log(`✅ ${c.user.tag} is online!`);
setInterval(() => {
var newStatus = statuses[Math.floor(Math.random() * statuses.length)];
client.user.setActivity(newStatus);
}, 10000);
});
^ for index.js
xx_lavaboy_xx123
xx_lavaboy_xx12311mo ago
u need to import statuses from the statuses.js file
sachoochoo
sachoochooOP11mo ago
may I see how that would work? i know it would require something along the lines of: const newStatus = require('./statuses');
xx_lavaboy_xx123
xx_lavaboy_xx12311mo ago
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
[
{ name: "ABC", type: ActivityType.Streaming },
{ name: "DEF", type: ActivityType.Watching },
{ name: "GHI", type: ActivityType.Listening },
{ name: "JKL", type: ActivityType.Playing },
];
[
{ name: "ABC", type: ActivityType.Streaming },
{ name: "DEF", type: ActivityType.Watching },
{ name: "GHI", type: ActivityType.Listening },
{ name: "JKL", type: ActivityType.Playing },
];
sachoochoo
sachoochooOP11mo ago
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!
Want results from more Discord servers?
Add your server