ghilasson
ghilasson

Reputation: 17

Discord Bot - Responses

If I have a number of responses: const exampleResponses = [ "name1", "name2", "name3"];

But let's say it's thousands of responses, not just three. Is there a way to pull in the responses from another file? As it stands, with thousands of responses it just clogs up my coding file.

Upvotes: 1

Views: 152

Answers (3)

Elitezen
Elitezen

Reputation: 6710

There are many ways you can go about this. One way would be to have a separate file with one array containing all the responses, then export that variable and require it in your main file

Responses.js

const responses = ['r1', 'r2', 'r3'] 
// And so on...

module.exports = {
   responses
}

Main File

const { responses } = require('filePathToResponsesJS')

Upvotes: 2

Jason
Jason

Reputation: 83

A way to do this is to make a JSON file and call from it with JavaScript.

const { exampleResponses } = require('./example.json')
{
  "exampleResponses": [
    "name1",
    "name2",
    "name3"
  ]
}

Upvotes: 2

Mr. Slug
Mr. Slug

Reputation: 110

You can import the FileSystem module and do something like this:

responses= JSON.parse( fs.readFileSync( 'data/responses.json', function( err, data ) { ... }

Similarly, you can write to said file to keep things neat and tidy.

Upvotes: 1

Related Questions