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.');
});
})
Related
I've been facing this problem.
My code now disappears when get a new message. I want to make messages pile up, but I have tried many ways, but I have not solved them. I would really appreciate it if you could tell me how to do this.
When I enter a message room, I get 25 messages from firebase server.
If there are more than 25 messages, last message disappears.
Disappear message. I don't want to like this ....
I want to like this.
here is my disappear code
const messageLimit = 25;
const [messagesSnapshot] = useCollection(
db
.collection('chats')
.doc(id)
?.collection('messages')
.orderBy('timestamp', 'desc')
.limit(messageLimit),
);
if (messagesSnapshot) {
const snap = messagesSnapshot.docs;
const startPost = messagesSnapshot.docs[messagesSnapshot.docs.length - 1];
setStartAt(startPost);
const messages = snap.map(message => ({
id: message.id,
user: message.data().user,
messageInfo: {
...message.data(),
timestamp: message.data().timestamp?.toDate().getTime(),
},
}));
setMessagesList(messages);
}
const getMoreMessages = async () => {
if (!lastPost) {
setIsGetMessagesLoading(true);
const query = await db
.collection('chats')
.doc(id)
.collection('messages')
.orderBy('timestamp', 'desc')
.startAfter(startAt)
.limit(messageLimit)
.get();
const messages = query.docs.map(message => ({
id: message.id,
user: message.data().user,
messageInfo: {
...message.data(),
timestamp: message.data().timestamp?.toDate().getTime(),
},
}));
setStartAt(query.docs[query.docs.length - 1]);
setMessagesList([...messagesList, ...messages]);
setIsGetMessagesLoading(false);
messages.length === 0 ? setLastPost(true) : setLastPost(false);
}
};
useEffect(() => {
showMessages();
}, [messagesSnapshot]);
As Shyam commented, you've limited the query to 25 messages, so when a new message is added, the oldest message falls out of the query.
One thing you could do is detect the changes between the updates, and never remove the "outdated" documents.
The example from the documentation:
db.collection("cities").where("state", "==", "CA")
.onSnapshot((snapshot) => {
snapshot.docChanges().forEach((change) => {
if (change.type === "added") {
console.log("New city: ", change.doc.data());
}
if (change.type === "modified") {
console.log("Modified city: ", change.doc.data());
}
if (change.type === "removed") {
console.log("Removed city: ", change.doc.data());
}
});
});
So you'd only handled added (which is initially every document) and possibly modified, but ignore removed.
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!
So I am trying to make my giveaway bot but can't make a create command so the person can add more details about the giveaway! Thus, I need questions but everytime I try it never goes well!
message.channel.send("Please mention the channel you want the giveaway to be in! **e.g #channel**");
try {
let msgs = await message.channel.awaitMessages(u2=>u2.author.id===message.author.id, { time: 15000, max: 1, errors: ["time"]});
if(parseInt(msgs.first().content)==mention.channel) {
const Channel = message.mentions.channels.first();
await Channel.send("HEY")
}
else {
message.channel.send("You did not mentioned a channel!");
}
}catch(e) {
return message.channel.send("Command Cancel!")
}
It either returns to the catch(e) line or "You did not mentioned a channel!"!
This should be what you want i think. (works with mentioning the channel, providing a channel ID or providing a channel name)
message.channel.send("Please mention the channel you want the giveaway to be in! **e.g #channel**");
let channel;
let response;
try {
response = await message.channel.awaitMessages(msg => msg.author.id === message.author.id, { max: 1, time: 1000*60*3, errors: ['time'] })
} catch {
return message.channel.send("Command Cancel!")
}
if (response.first().mentions.channels.first()) {
channel = response.first().mentions.channels.first()
} else if (!isNaN(response.first().content) && message.guild.channels.cache.get(response.first().content)) {
channel = message.guild.channels.cache.get(response.first().content)
} else if (isNaN(response.first().content) && message.guild.channels.cache.find(c => c.name.toLowerCase() === response.first().content.toLowerCase())) {
channel = message.guild.channels.cache.find(c => c.name.toLowerCase() === response.first().content.toLowerCase())
}
if (channel) {
await channel.send("HEY")
} else {
return message.channel.send("You did not mentioned a channel!");
}
Basically, what I'm trying to do is detect if a message is being sent, reset the content of that message and await a new reply and then check the content of that message for something in specific. The code looks something like this:
if (message.author.id === "318414570161045505") {
message.channel.send(`${message.author}` + " silence");
if (message.content.toUpperCase().includes("no")) {
console.log(message.content)
}
}
I hope I made myself clear on my problem?
You can use message.channel.awaitmessages
if (message.author.id === "318414570161045505") {
message.channel.send(`${message.author}` + " silence");
if (message.content.toUpperCase().includes("no")) {
message.delete()
const filter = m => m.author.id === message.author.id && m.content === '??????'
message.channel
.awaitMessages(filter, {
max: 1,
time: 60000,
errors: ['time'],
})
.then(collected => {
let messageCollected = collected.first()
})
.catch(collected => {
}
}
});
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