0

Problem related to : Discord.js

I just want to ask that how can I add my collectors data in a variable and use it outside from the file such as use it on index.js. I just want to create a setup command that when user type -setup it will automatically ask some questions and store those answers to variables and based on those variables it will add those variables on main file.

Basically I have three questions :

1 . how can I ask multiple questions to the user.

2 . How can I store those questions in a variable and use it outside from the file

3 . How can I filter that if the user has done the setup or not.

What am I working on : I am working on a welcome message bot, what it does that when we add him to our server it doesn't so anything, admin have to setup this bot for setting welcome message to your server .Admin have to use -setup command, what this -setup command do ? this command add a bunch of questions that what is channel id, what's your message, what's your invite pic etc., after gaining those answers he will setup a welcome message to the channel he/she provided, this message will be in a from of embed so I have to gain the embed title, avatar, description from the user.

One last question: do I need to setup a database for this?

slugster
  • 49,403
  • 14
  • 95
  • 145
  • 1
    You don't *have* to use a database, but you absolutely should. Storing these as local variables would make them disappear when your bot resets, and json files can corrupt more easily than a DB. – Squiddleton Jul 04 '21 at 01:29

1 Answers1

1
  1. Take a look at the Discord.js documentation on awaiting messages. You can wait for a message from a specific user by changing the filter to compare the person who sent the command against the message received.
  2. Take the variable you are storing the answers and use JSON.stringify(variable) which will turn it into text, which you can then use the fs module to write to a file with.
const fs = require('fs')

fs.writeFile('/Users/joe/test.json', variable, err => {
 if (err) {
   console.error(err)
   return
 }
 //file written successfully
})

To read from it just use var answers = JSON.parse(fs.readFileSync('file', 'utf8'));

  1. There are lots of ways to do this. If you store a file for each user, you can just check if a file exists with the user/server id.
fs.stat('test.json', function(err, stat) {
   if(err == null) {
       console.log('File exists');
   } else if(err.code === 'ENOENT') {
       // file does not exist
       fs.writeFile('log.txt', 'Some log\n');
   } else {
       console.log('Some other error: ', err.code);
   }
});

Whether or not you need a database really depends on the scale of your bot, I've never needed one before but I don't write major bots. At some point I'd recommend looking into how to use one. I've included links to more detailed explanations and documentations throughout as well, be sure to check those out if you're confused.