I'm working with some if/else statements and I'm trying to do something like this:
const countriesArray = countries.includes(msg.body);
if (msg.body === 'searchCountry') {
// the user is send to here
msg.reply('Please, type the name of a country');
// The ideia now is that the next message of the user should go to `countriesArray` if statement
// But this is not happening because the if is getting the previous searchCountry message, instead
// of the next one, which would be the country name.
if (countriesArray) {
fetchData(lowerCaseMsg, true);
} else {
// the user gets throw to here because previously he typed searchCountry
msg.reply('Country not found');
}
}
Is there something like actualMessage or ignoreNextMessage? If not, is there a better way to do what I'm trying to archive?
Since getting messages is done through individual events, you won't be able to directly say something like .getNextMessage(). To achieve this you'll need to keep track of which contacts you're awaiting a reply from in some data structure.
This could look like a simple object mapping userId to a boolean that indicates that you're waiting for a response, or some other thing like a string with "searchCountry" if you want to use it for multiple commands.
Here's a rough example:
// earlier in your code
const awaitingResponse = {};
// message handler
const userId = msg.author || msg.from;
if(awaitingResponse[userId]) {
// the message body could be a country, do stuff here
} else if (msg.body === 'searchCountry') {
awaitingResponse[userId] = true;
msg.reply('Please, type the name of a country');
}
Sorry for reopening but the user never gets to the first if :(
also, awaitingResponse[userId] stays as undefined.
Can you post your code?
I just used your code in the sample.js. Sorry, I forgot to say this detail.
Make sure your awaitingResponse object is declared outside the message handler, otherwise it will get reset every time it鈥檚 called
Thanks and my bad for not trying this earlier xD
Since getting messages is done through individual events, you won't be able to directly say something like
.getNextMessage(). To achieve this you'll need to keep track of which contacts you're awaiting a reply from in some data structure.This could look like a simple object mapping userId to a boolean that indicates that you're waiting for a response, or some other thing like a string with "searchCountry" if you want to use it for multiple commands.
Here's a rough example:
// earlier in your code const awaitingResponse = {}; // message handler const userId = msg.author || msg.from; if(awaitingResponse[userId]) { // the message body could be a country, do stuff here } else if (msg.body === 'searchCountry') { awaitingResponse[userId] = true; msg.reply('Please, type the name of a country'); }
Thank you so much @pedroslopez :heart: :heart: :heart: :heart: :heart:
Now with that function you gave us i got my bot working to wait for person to respond before the bot asks another question! :rocket:
Most helpful comment
Since getting messages is done through individual events, you won't be able to directly say something like
.getNextMessage(). To achieve this you'll need to keep track of which contacts you're awaiting a reply from in some data structure.This could look like a simple object mapping userId to a boolean that indicates that you're waiting for a response, or some other thing like a string with "searchCountry" if you want to use it for multiple commands.
Here's a rough example: