I'm currently developing a discord.js bot on the latest version. I'm currently trying to get the following bit of code to send a message giving instructions with what to say, then once I get a response, send another message. however, I can't get it working. any help would be appreciated!
client.on('messageCreate', message => {
//check if message is from self
if (message.author === client.user) {
//if it is. do nothing
return
} else {
//if its not, read message
if (message.content === prefix + "start") {
let filter = m => m.author.id === message.author.id
message.channel.send("Testing.. Type YES or NO").then(() => {
message.channel
.awaitMessages(filter, {
max: 1,
time: 30000,
errors: ['time']
})
.then(message => {
message = message.first()
if (message.content.toUpperCase() == 'YES' || message.content.toUpperCase() == 'Y') {
message.channel.send(`Hello World!`);
} else if (message.content.toUpperCase() == 'NO' || message.content.toUpperCase() == 'N') {
message.channel.send(`Ping`);
} else {
message.channel.send(`Terminated: Invalid Response`);
}
})
.catch(collected => {
message.channel.send('Timeout');
});
})
}
}
});
Scoping problems. The result of your awaitMessages() promise is called message. However, the outer scope of the function itself has a variable named message as well, hence you are referencing different things:
// from client.on("messageCreate", message)
message.channel.awaitMessages(filter, {
max: 1,
time: 30000,
errors: ['time']
})
// not the same; from awaitMessages() instead
.then(message => {
message = message.first()
})
You can fix it by doing this:
message.channel.awaitMessages(filter, {
max: 1,
time: 30000,
errors: ['time']
})
// we call it 'collected' as we essentially collect a message
.then(collected => {
// redeclare a new variable named 'msg'
const msg = collected.first()
if (msg.content.toUppercase() == "YES" || msg.content.toUppercase() == "Y") {
msg.channel.send(`Hello World!`);
}
else if (...)
You get the gist here.