Search code examples
javascriptnode.jsdiscorddiscord.jsnode-modules

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



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);
});

statuses.json

(nothing, so far)

I tried adding a JSON file (a random example shown below):

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

But yet, I am still not sure how to call this into my main index.js file.


Solution

  • Create a statuses.json file in the same folder like this: (The numbers are enums for the ActivityType)

    [
     { "name": "ABC", "type": 1 },
     { "name": "DEF", "type": 3 },
     { "name": "GHI", "type": 2 },
     { "name": "JKL", "type": 0 }
    ]
    

    Then in your code import your .json file:

    const statuses = require('./statuses.json');