Mention who clicked the reaction after the bot - javascript

I use ${reaction.message.author} so the bot mentions the person who clicked on the reaction after it. But the bot is mentioning itself.
How to make the bot to mention the user who clicked on the reaction after the bot?
===Code===
client.on('message', (message) => {
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if (message.content.startsWith(prefix + 'Interpol')) {
const response = args.join(' ');
message.channel
.send({
content: '<#&776034311245660200>',
embed: {
color: 3447003,
title: 'Новый вызов',
timestamp: new Date(),
},
})
.then((sentMessage) => {
sentMessage.react('1️⃣');
message.delete({ timeout: 100 });
const filter = (reaction, user) => {
return ['1️⃣'].includes(reaction.emoji.name);
};
sentMessage
.awaitReactions(filter, {
max: 2,
time: 60000,
errors: ['time'],
})
.then((collected) => {
const reaction = collected.first();
if (reaction.emoji.name === '1️⃣') {
console.log(reaction.message.author);
message.author.send({
embed: {
color: 3447003,
title: 'Вызов принят',
description: `**Сотрудник:** ${reaction.message.author}`,
timestamp: new Date(),
},
});
}
});
});
}
});

You can update your filter to filter out reactions if the user is a bot. This way the first reaction will be from a member:
const filter = (reaction, user) => {
return ['1️⃣'].includes(reaction.emoji.name) && !user.bot;
};
Update: So, you want to find the first member who reacted to the message and is not a bot. The problem is that you send a DM with reaction.message.author. reaction.message is the message that this reaction refers to, so the sentMessage in your case and the author of this message is the bot itself.
You can .find() the first member who reacted to the message by checking the reaction.users.cache. reaction.users is manager of the users that have given this reaction (including the bot). By using .find(), you can return a single member:
message.channel
.send({
content: '<#&776034311245660200>',
embed: {
color: 3447003,
title: 'Новый вызов',
timestamp: new Date(),
},
})
.then((sentMessage) => {
sentMessage.react('1️⃣');
message.delete({ timeout: 100 });
const filter = (reaction, user) => {
return !user.bot && ['1️⃣'].includes(reaction.emoji.name);
};
sentMessage
.awaitReactions(filter, {
max: 1,
time: 60000,
// errors: ['time'],
})
.then((collected) => {
const reaction = collected.first();
if (reaction.emoji.name === '1️⃣') {
// find the first member who reacted and is not a bot
const member = reaction.users.cache.find((user) => !user.bot);
message.author.send({
embed: {
color: 3447003,
title: 'Вызов принят',
description: `**Сотрудник:** ${member}`,
timestamp: new Date(),
},
});
}
})
.catch(console.log);
});

You're just missing a filter, if I'm not mistaken. This is a common occurrence when trying to collect something, the bot picks its own message/reactions to itself.
To correct this, all you have to add onto your filter function. && user.id === message.author.id;
So:
return ['1️⃣'].includes(reaction.emoji.name) && user.id === message.author.id;
This basically checks if the user who reacted was also the sender of the message, who I'm assuming is the bot. Else not, then you could just make it !user.bot to check if it's not a bot who responded like Zsolt said.

Related

How do you send a DM to users that react to a bot sent message? (Discord.JS)

Pretty much what the title says, but I just wanted to figure out how to get my bot to send a DM to anyone and everyone who reacts to the message it sends.
name: "sign-up",
description: "signup thing",
category: "misc",
execute(message, client){
const args = message.content.slice(prefix.length).trim().split(' ');
const command = args.shift().toLowerCase();
const say = args.join(" ");
if (!args.length) {
return message.channel.send(`You didn't provide any arguments, ${message.author}!`);
}
message.channel.send(say).then(message =>{
message.react("✅")
const filter = (reaction, user) => {
return ['✅'].includes(reaction.emoji.name) && user.id === message.author.id;
};
const collector = message.createReactionCollector(filter, {time: 60000 });
collector.on('collect', (reaction, reactionCollector) => {
reaction.users.last().send('Some')
.catch(console.error)
});
}
In this case, an admin will write a message (which comes up correctly as an args), and I have gotten this far where I just wanted to return the reaction users in my console. I am pretty much stuck here.
Apologies if the code is weird.
this code is missing } and you should try using async/await that will easier to look and understanding for newbie. And as your code of filter using on createReactionCollector you are capture emoji that reacted by who send bot command request not every user (user.id === message.author.id)
name: "sign-up",
description: "signup thing",
category: "misc",
async execute(message, client){
const args = message.content.slice(prefix.length).trim().split(' ');
const command = args.shift().toLowerCase();
const say = args.join(" ");
if (!args.length) {
return message.channel.send(`You didn't provide any arguments, ${message.author}!`);
}
const newMsg = await message.channel.send(say)
newMsg.react("✅")
const filter = (reaction, user) => {
return user.id === message.author.id;
};
const collector = message.createReactionCollector(filter, {time: 60000 });
collector.on('collect', (reaction, user) => {
if (r.emoji.name === '✅') {
user.send('Some');
}
});
}

Discord.js: Embed not sending

I'm trying to make a help embed command with reactions, but I keep getting this error
I'm trying to make a help embed with reactions, but when I do !help, it doesnt send the embed???
I'm trying to make a help embed command with reactions, but I keep getting this error I'm trying to make a help embed with reactions, but when I do !help, it doesnt send the embed???
This is my code:
const Discord = require('discord.js')
module.exports = {
name: 'help',
description: "Help command",
execute(message, args){
async function helpEmbed(message, args) {
const helpEmbed = new Discord.MessageEmbed()
.setTitle('Help!')
.setDescription(`**React with one of the circles to get help with a category!**`)
.addFields(
{ name: 'Moderation', value: ':blue_circle:'},
{ name: 'Fun', value: ':green_circle:'},
{ name: 'Misc', value: ':purple_circle:'},
)
.setColor(4627199)
message.channel.send({embed: helpEmbed}).then(embedMessage => {
embedMessage.react('🔵')
embedMessage.react('🟢')
embedMessage.react('🟣')
})
const msg = await sendReact()
const userReactions = message.reactions.cache.filter(reaction => reaction.users.cache.has(userId));
const filter = (reaction, user) =>{
return ['🔵', '🟢', '🟣'].includes(reaction.emoji.name) && user.id === message.author.id;
}
msg.awaitReactions(filter, { max: 1, time: 10000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '🔵') {
try {
for (const reaction of userReactions.values()) {
reaction.users.remove(userId)
}
} catch (error) {
console.error('Failed to remove reactions.')
}
message.channel.send('Moderation');
} else if (reaction.emoji.name === '🟢') {
try {
for (const reaction of userReactions.values()) {
reaction.users.remove(userId)
}
} catch (error) {
console.error('Failed to remove reactions.')
}
message.channel.send('Fun');
} else if (reaction.emoji.name === '🟣') {
try {
for (const reaction of userReactions.values()) {
reaction.users.remove(userId)
}
} catch (error) {
console.error('Failed to remove reactions.')
}
message.channel.send('Misc')
}
})
.catch(collected => {
message.reply('you reacted with neither a thumbs up, nor a thumbs down.');
});
}
}
}
You defined helpEmbed funcion but not called it.
try writing "helpEmbed()" after "execute(message, args){"
if still doesn't work:
Check bot's permission
Bot needs SEND_EMBED_LINKS Permission in that channel.

How to detect more than one reaction to a message in discord.js?

I'm trying to create a dynamic help command, in the sense that the users can decide what "page" they would like to go to simply by reacting to the message. I have tried doing this, however, with my code it only detects the first reaction. I have tried setting the max for the awaitReactions method to more than 1, however once I do that it doesn't detect any reaction. Here is my code:
const Discord = require('discord.js');
const fs = require('fs');
module.exports = {
name: 'help',
aliases: ('cmds'),
description: 'Shows you the list of commands.',
usage: 'help',
example: 'help',
async execute(client, message, args, prefix, footer, color, invite, dev, devID, successEmbed, errorEmbed, usageEmbed) {
const helpEmbed = new Discord.MessageEmbed()
.setColor(color)
.setAuthor(`${client.user.username} Discord Bot\n`)
.setDescription('• 📍 Prefix: ``' + prefix + '``\n' +
`• 🔧 Developer: ${dev}\n\n⚙️ - **Panel**\n👮 - **Moderation**\n❔ - **Other**`);
const moderationEmbed = new Discord.MessageEmbed()
.setColor(color)
.setAuthor(`Config\n`)
.setDescription('To get more information about a certain command, use ``' + prefix +
'help [command]``.\n\n•``test``, ``test2``, ``test3``.');
try {
const filter = (reaction, user) => {
return (reaction.emoji.name === '⚙️' || '👮' || '❔') && user.id === message.author.id;
};
message.delete();
message.channel.send(helpEmbed).then(embedMsg => {
embedMsg.react("⚙️")
.then(embedMsg.react("👮"))
.then(embedMsg.react("❔"))
embedMsg.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '⚙️') {
embedMsg.edit(helpEmbed);
} else if (reaction.emoji.name === '👮') {
embedMsg.edit(moderationEmbed);
} else if (reaction.emoji.name === '❔') {
message.reply('test.');
}
})
.catch(collected => {
message.reply('didnt work.');
});
});
} catch (e) {
console.log(e.stack);
}
}
}
Used a collector.
const collector = embedMsg.createReactionCollector(filter);
collector.on('collect', (reaction, user) => {
reaction.users.remove(user.id); // remove the reaction
if (reaction.emoji.name === '⚙️') {
embedMsg.edit(helpEmbed);
} else if (reaction.emoji.name === '🛠️') {
embedMsg.edit(utilityEmbed);
} else if (reaction.emoji.name === '👮') {
embedMsg.edit(moderationEmbed);
}
});

How to make bot send message to another channel after reaction | Discord.js

How do I make it so that when someone reacts with the first emoji in this command, the bot deletes the message and sends it to another channel?
Current Code:
const Discord = require("discord.js");
module.exports.run = async (bot, message, args) => {
if (!message.member.hasPermission("MANAGE_MESSAGES"))
return message.channel.send("You are not allowed to run this command.");
let botmessage = args.join(" ");
let pollchannel = bot.channels.cache.get("716348362219323443");
let avatar = message.author.avatarURL({ size: 2048 });
let helpembed = new Discord.MessageEmbed()
.setAuthor(message.author.tag, avatar)
.setColor("#8c52ff")
.setDescription(botmessage);
pollchannel.send(helpembed).then(async msg => {
await msg.react("715383579059945512");
await msg.react("715383579059683349");
});
};
module.exports.help = {
name: "poll"
};
You can use awaitReactions, createReactionCollector or messageReactionAdd event, I think awaitReactions is the best option here since the other two are for more global purposes,
const emojis = ["715383579059945512", "715383579059683349"];
pollchannel.send(helpembed).then(async msg => {
await msg.react(emojis[0]);
await msg.react(emojis[1]);
//generic filter customize to your own wants
const filter = (reaction, user) => emojis.includes(reaction.emoji.id) && user.id === message.author.id;
const options = { errors: ["time"], time: 5000, max: 1 };
msg.awaitReactions(filter, options)
.then(collected => {
const first = collected.first();
if(emojis.indexOf(first.emoji.id) === 0) {
msg.delete();
// certainChannel = <TextChannel>
certainChannel.send(helpembed);
} else {
//case you wanted to do something if they reacted with the second one
}
})
.catch(err => {
//time up, no reactions
});
});

missing .then's but idk where to put

So i think i quite forgot some .then's beause the bot sends the B emoji message instanntly without a reaction from the user and even when i would provide a "suggestion" then it wouldnt send it to the specific channel, but idk where i have to put the missing .then's. Can someone help me please? I tried to figure it out myself and tested some but it didn't make anything better.
execute(message, client, args) {
const Discord = require('discord.js');
let Embed = new Discord.MessageEmbed()
.setColor('0x0099ff')
.setDescription(`Suggestion categories`)
.addField(`For what you want to suggest something?`, `\nA: I want to suggest something for the Website/Servers/Discord Server\nB: I want to suggest something for the CloudX Bot \n\nPlease react to this message with A or B`)
message.channel.send(Embed).then(function (message) {
message.react("🇦").then(() => {
message.react("🇧")
const filter = (reaction, user) => {
return ['🇦', '🇧'].includes(reaction.emoji.name) && user.id;
}
message.awaitReactions(filter, { max: 1 })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '🇦') {
const filter = m => m.author.id === message.author.id;
message.channel.send(`Please provide a suggestion for the Website/Servers/Discord Server or cancel this command with "cancel"!`).then(() => {
message.channel.awaitMessages(filter, { max: 1, })
.then(async (collected) => {
if (collected.first().content.toLowerCase() === 'cancel') {
message.reply("Your suggestion has been cancelled.")
}
else {
let embed1 = new Discord.MessageEmbed()
.setColor('0x0099ff')
.setAuthor(message.author.tag)
.addField(`New Suggestion:`, `${collected.first().content}`)
.setFooter(client.user.username, "attachment://CloudX.png")
.setTimestamp();
const channel = await client.channels.fetch("705781201469964308").then(() => {
channel.send({embed: embed1, files: [{
attachment:'CloudX.png',
name:'CloudX.png'
}]})
message.channel.send(`Your suggestion has been filled to the staff team. Thank you!`)
})
}
})
})
}
if (reaction.emoji.name === '🇧') {
const filter = m => m.author.id === message.author.id;
message.channel.send(`Please provide a suggestion for the CloudX Bot or cancel this command with "cancel"!`).then(() => {
message.channel.awaitMessages(filter, { max: 1, })
.then(async (collected) => {
if (collected.first().content.toLowerCase() === 'cancel') {
message.reply("Your suggestion has been cancelled.")
}
else {
let embed2 = new Discord.MessageEmbed()
.setColor('0x0099ff')
.setAuthor(message.author.tag)
.addField(`New Suggestion:`, `${collected.first().content}`)
.setFooter(client.user.username, "attachment://CloudX.png")
.setTimestamp();
const channel = await client.channels.fetch("702825446248808519").then(() => {
channel.send({embed: embed2, files: [{
attachment:'CloudX.png',
name:'CloudX.png'
}]})
message.channel.send(`Your suggestion has been filled to the staff team. Thank you!`)
})
}
})
})
}
})
})
})
},
I would suggest learning await/async functions.
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await
This will clean up your code and keep things steady without five thousand .then()
async execute(message, client, args) {
const { MessageEmbed } = require('discord.js');
const embed = new MessageEmbed()
.setColor('#0099ff')
.setDescription(`Suggestion categories`)
.addField(`For what you want to suggest something?`, `\nA: I want to suggest something for the Website/Servers/Discord Server\nB: I want to suggest something for the CloudX Bot \n\nPlease react to this message with A or B`)
const question = message.channel.send(embed)
await question.react("🇦")
await question.react("🇧")
const filter = (reaction, user) => {
return ['🇦', '🇧'].includes(reaction.emoji.name) && user.id;
}
This is just part of it but you should be able to get the gist...

Categories