I am creating an awaitMessages for my discord bot to add an item to my virtual shop on my bot. In this awaitMessages I would like to indicate the price of the article. So the answer must be a number, but the problem is that when I write an answer which isn't a number, the awaitMessages is canceled. I would like, when I send a message that isn't a number, it sends an error message but the awaitMessages doesn't cancel and I write a response again.
const filter = m => m.author.id === message.author.id;
message.channel.send('Price').then(msg => {
message.channel.awaitMessages(filter, {
max: 1,
time: 10000,
errors: ['time'] })
.then(collected => {
if(isNaN(collected)) return message.channel.send('not a number')})
.catch(collected => msg.delete());
});
You need to add conditions to your filter.
const filter = m => m.author.id === message.author.id && !isNaN(parseInt(m.content));
message.channel.send('Enter a price\nHas to be a number.').then(msg => {
message.channel.awaitMessages(filter, {
max: 1,
time: 10000,
errors: ['time']
})
.then(collected => {
// Do something
})
.catch(collected => msg.delete());
});
This won't send an error message, however it won't trigger your collector and therefore won't stop trying to collect a number until one is sent or until the time runs out.
Related
In a pervious post I was having issues getting the bot to recognise reactions and the fix worked however and then changed it to react on a message that the bot says afterwards and now it isnt working again, I have tried changing the user condition so its the original command author but that didn't seem to work
So you run the code and it makes the embed perfectly and reacts to it however it doesnt recognise when you react and makes the timeout message
exports.run = async (client, message, args) => {
message.delete()
const MINUTES = 5;
const questions = [
{ answer: null, field: 'placeholdquestion' },
{ answer: null, field: 'placeholdquestion' },
{ answer: null, field: 'placeholdquestion' },
]; //to add more questions just add another line of the above code {answes: null, field: `Question Here`}
let current = 0;
const commanduser = message.author.id
// ...
// wait for the message to be sent and grab the returned message
// so we can add the message collector
const sent = await message.author.send(
`${questions[current].field}`,
);
const filter = (response) => response.author.id === message.author.id;
// send in the DM channel where the original question was sent
const collector = sent.channel.createMessageCollector(filter, {
max: questions.length,
time: MINUTES * 60 * 1000,
});
// fires every time a message is collected
collector.on('collect', (message) => {
//if (questions > 1 && questions < 10) {
// add the answer and increase the current index HERE
questions[current++].answer = message.content;
const hasMoreQuestions = current < questions.length; //change to be an imput of how many questions you want asked
if (hasMoreQuestions) {
message.author.send(
`${questions[current].field}`,
);
}
});
// fires when either times out or when reached the limit
collector.on('end', (collected, reason) => {
if (reason === 'time') {
return message.author.send(
`I'm not saying you're slow but you only answered ${collected.size} questions out of ${questions.length} in ${MINUTES} minutes. I gave up.`,
);
}
const embed = new MessageEmbed()
.setTitle("LOA Request")
.addFields(
{ name: 'placehold', value: questions[0].answer+'/10' },
{ name: 'placehold', value: questions[1].answer+'/10' },
{ name: 'placehold', value: questions[2].answer+'/10', inline: true },)
.setColor(`#1773BA`)
.setTimestamp()
.setThumbnail("https://media.discordapp.net/attachments/772915309714735205/795378037813805126/mvg_clean_2.png")
.setFooter("request by: " + message.author.tag);
;
message.channel.send(embed)
.then(function (message) {
message.react("👍")
message.react("👎")})
const filter = (reaction, user) => {
return ['👍', '👎'].includes(reaction.emoji.name) && user.id === commanduser; //changed to try and fix it didnt work as message.author.id or this
};
message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] } )
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '👍') {
message.channel.send('you reacted with a thumbs up.');
}
else {
message.reply('you reacted with a thumbs down.');
}
})
.catch(collected => {
console.log(`After a minute, only ${collected.size} out of 4 reacted.`);
message.reply('you didn\'t react with neither a thumbs up, nor a thumbs down.');
});
});
;
}
You have a slight logic error. You need to fit the code from your second filter to the message.awaitReactions inside of your message.channel.send(embed).then(function (message)...) method. In your code, the bot is trying to check for reactions from the original message, which you already deleted (since the awaitReactions is outside the scope of your function where you send and react to the embed).
Like this:
message.channel.send(embed)
.then(function (message) {
message.react("👍")
message.react("👎")
const filter2 = (reaction, user) => {
return ['👍', '👎'].includes(reaction.emoji.name) && user.id === commanduser;
};
message.awaitReactions(filter2, { max: 1, time: 60000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '👍') {
message.channel.send('you reacted with a thumbs up.');
}
else {
message.reply('you reacted with a thumbs down.');
}
})
.catch(collected => {
console.log(`After a minute, only ${collected.size} out of 4 reacted.`);
message.reply('you didn\'t react with neither a thumbs up, nor a thumbs down.');
});
})
After about 20 hours of searching for answers, editing the code, and trying new methods. I've given up and decided to ask here.
I'm absolutely sick of this piece of code.
What I'm trying to do is: 1. Wait for a message to receive 2 reactions 2. After 2 reactions, post message to a separate channel
EDIT: After some users recommended removing my stupidly placed console.log before my .then this code now proceeds to not wait for a reaction on a message and go through the entire process without posting a message to the specified channel
Please bear with me, I'm still learning.
Here's the code:
client.on('message', msg => {
const filter = (reaction, user) => reaction.emoji.name === '703033480090484756' && user.id === message.author.id;
msg.awaitReactions(filter, { max: 1, time: 30000, errors: ['time'] })
.then(collected => {
const starboard = new Discord.MessageEmbed()
.setColor(0xFF0000)
.setAuthor(`${msg.author.username}`, `${msg.author.avatarURL()}`)
.setDescription(`${msg.author.content}`)
client.channels.cache.get('714842643766444122').send(starboard)})
.catch(err => console.log(err), ('Passed! Added to starboard.'));
console.log('Added to Starboard!');
If you need more details, Please ask.
client.on('message', async msg => {
let rMessage = await msg.channel.send("Test Message");
await rMessage.react('703033480090484756');
const filter = (reaction, user) => reaction.emoji.name === '703033480090484756' && user.id === msg.author.id;
rMessage.awaitReactions(filter, { max: 1, time: 30000, errors: ['time'] })
.then(collected => {
const starboard = new Discord.MessageEmbed()
.setColor(0xFF0000)
.setAuthor(`${msg.author.username}`, `${msg.author.displayAvatarURL()}`)
//.setDescription(`${msg.author.content}`)
client.channels.cache.get('714842643766444122').send(starboard)})
.catch(err => console.log(err), ('Passed! Added to starboard.'));
console.log('Added to Starboard!');
You used .then on your console.log('Woah! Passed the filter...').
I have a little question.
I'm currently in the progress of trying out creating a tickets bot, and had the idea of having so when a user types !close, it would present him with an embed, asking him if he really does want to close it using reactions (:wastebasket: for Yes, :x: for No).
If the user reacts for Yes, the channel will close. As for No, the embed message will be deleted.
I will be glad to help.
You can use something a little like this:
const prevMsg = message
message.channel.send(embed).then(message => {
const questionMessage = message;
questionMessage.react('🗑️')
.then(() => questionMessage.react('❌'));
const filter = (reaction, user) => {
return ['🗑️', '❌'].includes(reaction.emoji.name) && user.id === prevMsg.author.id;
};
questionMessage.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '🗑️') {
// closing function here
} else {
questionMessage.delete();
}
})
})
If it didn't work, please let me know!
First time I use createReactionCollector.
It doesn't work, when I react the collector gets nothing.
But when I make the bot react, I get that reaction but, not mine.
const collector = startMessage.createReactionCollector(x => {
return true
}, { time: 5000 })
collector.on('collect', (r, collector) => {
console.log("collected");
})
collector.on('end', r => {
console.log(r)
})
That's because you're not using a valid filter, you need to pass the reaction and user parameters into the filter and use them like so:
const filter = (reaction, user) => reaction.emoji.name === '👌' && user.id === 'someID';
Then add the filter to the collector like so:
const collector = message.createReactionCollector(filter, { time: 15000 });
These are from the example of createReactionCollector() on the docs
I am trying to make an automatic system in which the bot has a timer and if it doesn't get enough reactions to the bot's message, it says one thing, but if the votes match, the other message is sent, I've got most of it working but I get an error with the "rpMessage.awaitReactions" line.
I've tried making the initial message a Const, a variable, a "let" and just message.awaitReactions
if(cmd === `${prefix}rp`) {
const rpMessage = message.channel.send("<#&608365714775998485> A Roleplay is being started by " + message.author + " React with :white_check_mark: to join / vote" + '\n' + "Just a few rules to remember:" + '\n \n' + "• Dont FRP as this will get you removed from the RP" + '\n' + "• Drive cars in your civ rank (You can find speeds if you click your name)" + '\n' + "• Listen to staff" + '\n' + "• Don't cop bait").then(rpvote => {
rpvote.react('👍').then(() => rpvote.react('👎'));})
const filter = (reaction, user) => {
return ['👍', '👎'].includes(reaction.emoji.name) && user.id === message.author.id;
};
rpMessage.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] }) // This part isn't working
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '👍') {
message.reply('you reacted with a thumbs up.');
}
else {
message.reply('you reacted with a thumbs down.');
}
})
.catch(collected => {
console.log(`After a minute, only ${collected.size} out of 4 reacted.`);
message.reply('>rpfail.');
});
}
It's supposed to get the reactions from the message, but it never is able to read the property of awaitReacions
I am assuming the problem to be reusing code from stackoverflow question Discord.js message after receiving emoji reaction without updating the variables.
rpMessage.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] }) // This part isn't working
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '👍') {
rpMessage.reply('you reacted with a thumbs up.');
}
else {
rpMessage.reply('you reacted with a thumbs down.');
}
})
.catch(collected => {
console.log(`After a minute, only ${collected.size} out of 4 reacted.`);
rpMessage.reply('>rpfail.');
});
}
The variable name is rPMessage for you and you have used message