How do I collect a message inside of an interaction event in discord js?
I'm trying to create an anti-bot feature where a user has to repeat what the bot says back at it after calling the bot with a slash command. I need to wait for a reply for x seconds, and if there's no response cancel the command. I'm using the discord event called on interactionCreate, so I don't have access to a message object from which to instantiate a collector from. I've looked through the documentation, and I'm unable to find a way to get follow up responses from the user. Here's my code:
} else if (commandName === 'mine') {
var isBot = true;
var increase;
const db = getDatabase();
const dbref = ref(db);
if (talkedRecently.has(interaction.user.id) && interaction.user.username != "Nurav") {
interaction.reply("Hold up, wait 1 minute before typing mine.");
} else {
increase = Math.floor(Math.random() * 5) + 1;
interaction.reply(`Please complete your mine by sending this number: ${increase}`);
Here is where I need to wait for their response and check whether it's equal to the number I sent earlier.
If they sent the right thing, I need to set isBot to false.
if (!isBot) {
get(child(dbref, `users/` + interaction.user.username)).then((snapshot) => {
if (snapshot.exists()) {
var newBal = snapshot.val().balance + increase;
if (!snapshot.val().banned) {
set(ref(db, 'users/' + interaction.user.username), {
username: interaction.user.username,
balance: newBal
});
const accountEmbed = new MessageEmbed()
.setColor('#ff5500')
.setTitle("Success!")
.setURL('https://ascent-projects-64363.web.app/')
.setAuthor({ name: 'Central Bank Of Firecoin', iconURL: 'https://cdn.discordapp.com/attachments/888920833769242688/925485246974156860/Asset_44x.png', url: 'https://ascent-projects-64363.web.app/' })
.setDescription('+' + increase.toString())
.setThumbnail('https://cdn.discordapp.com/attachments/888920833769242688/925485246974156860/Asset_44x.png')
.setTimestamp()
.setFooter('Central Bank Of Firecoin', 'https://cdn.discordapp.com/attachments/888920833769242688/925485246974156860/Asset_44x.png');
interaction.reply({ embeds: [accountEmbed] });
}
else {
interaction.reply("Your account has been flagged for botting. To appeal the flag, message Nurav#4295");
}
} else {
console.log("No data available");
interaction.reply("Account doesn't exist");
}
}).catch((error) => {
console.error(error);
});
}
// Adds the user to the set so that they can't talk for a minute
talkedRecently.add(interaction.user.id);
setTimeout(() => {
// Removes the user from the set after a minute
talkedRecently.delete(interaction.user.id);
}, 60000);
}
//Ant-Bot confirmation
}
You can use TextChannel.createMessageCollector
.
It creates a message collector at the channel, collects MESSAGES_TO_COLLECT
messages that satisfies filter for SECONDS_TO_REPLY
seconds, and when finished, emits end
event.
You can also use collect
event to do something when a message is collected. However, in this case, we collect only one message, so no need to use collect
event.
So the code would be:
const SECONDS_TO_REPLY = 60 // replace 60 with how long to wait for message(in seconds).
const MESSAGES_TO_COLLECT = 1
const filter = (m) => m.author.id == interaction.user.id
const collector = interaction.channel.createMessageCollector({filter, time: SECONDS_TO_REPLY * 1000, max: MESSAGES_TO_COLLECT})
collector.on('end', collected => {
if (collected.size > 0 && collected.first().content == increase) {
// not a robot
// do all stuffs here without using isBot
}
/*
// uncomment to do something when robot is detected
else {
}
*/
replace the value of SECONDS_TO_REPLY
with how long to wait for message(in seconds).