I want to do is that when the embed message is changed, the reactions are reset, not deleted.
I mean that when someone reacts to the emoji and embed its changed, the reaction returns to 1 and does not stay at 2.
and when it returns to 1 I can send a third embed
ty
This is the code I am using:
const embed = new MessageEmbed()
.setTitle("Test1")
.setFooter("Test1");
message.channel.send(embed).then(sentEmbed => {
sentEmbed.react("➡");
const filter = (reaction, user) => {
return (
["➡"].includes(reaction.emoji.name) &&
user.id === message.author.id
);
};
sentEmbed
.awaitReactions(filter, { max: 1, time: 30000, errors: ["time"] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === "➡") {
const embed2 = new MessageEmbed()
.setTitle('test2')
.setDescription('test2')
sentEmbed.edit(embed2);
}
})
});
You can remove all reactions by using message.reactions.removeAll().catch(error => console.error('Failed to clear reactions: ', error)); and then add reactions back if you want. I would do this in a function so you don't have to add all the reactions over and over each time you want to update the embed.
Related
I tried doing this and adapting it from discordjs.guide, but I've either adapted something wrongly or it isn't working in my context. I tried this:
botMessage.react('❌');
const filter = (reaction, user) => {
return ['❌'].includes(reaction.emoji.name);
};
botMessage.awaitReactions({ filter, max: 1, time: 60000, errors: ['time'] }).then(collected => {
const reaction = collected.first();
console.log('a')
if(reaction.emoji.name === '❌'){
console.log('x')
botMessage.delete(1);
}
}).catch(collected => {});
botMessage is the response from the bot, since I am using a message command.
neither of the console.log() output anything.
any help is grateful and thank you
All you have to do is add the GUILD_MESSAGE_REACTIONS intent:
const client = new Client({ intents: [Intents.FLAGS.GUILD_MESSAGE_REACTIONS /* other intents */] });
I have been having a problem where I have been trying to figure out how to send an embed and then have the bot react to that embed and if you react to it, the bot will post another embed kind of like this,
-algebra,
(bot sends embed)
(bot reacts to embed)
(command user clicks on reaction)
(bot posts another embed specific to the reaction)
I really need it to have multiple reactions such as maybe ten and it would react with 1️⃣, 2️⃣, 3️⃣, 4️⃣, 5️⃣, 6️⃣, 7️⃣, 8️⃣, 9️⃣, 🔟
And if possible I need the bot to do this
-algebra
(bot posts embed)
(bot reacts)
(user reacts to the embed with one of the numbers)
(bot posts embed corresponding with reaction)
(bot reacts)
(user reacts)
(bot posts another embed corresponding with that)
making an almost infinite chain.
module.exports = {
name: 'algebra',
description: 'This is a command that should help you with algebra',
async execute(message, args, Discord, client){
let ageEmbed = new Discord.MessageEmbed()
.setTitle('Algebra! Its pretty damn hard.')
.addField('So you are just starting off with Algebra and you want to learn the basics, Well no problem just react with 1️⃣')
.addField('So you want to know about the questions such as "*x* = 21+5, what is *x*?" well no problem just react with 2️⃣')
.setColor('#FF1493')
let msg = await message.channel.send(ageEmbed)
await msg.react('1️⃣');
await msg.react('2️⃣');
const filter_age = (reaction, user) => {
return reaction.emoji.name === '1️⃣' || '2️⃣' && user.id === message.author.id && !user.bot;
}
const collector_age = msg.createReactionCollector(filter_age, {
time: 30000,
max: 1
});
collector_age.on('collect', async (reaction, user) => {
if (reaction.emoji.name === '1️⃣') {
let justbegginingembed = new Discord.MessageEmbed()
.setDescription(`Just beggining with Algebra..`)
.addField("This is where just beggining with algebra will be");
message.channel.send(justbegginingembed)
} else {
collector_age.on('collect', async (reaction, user) => {
if (reaction.emoji.name === '2️⃣') {
let algebraembed2 = new Discord.MessageEmbed()
.setDescription('so you want to learn about ....')
.addField('THIS IS WHERE');
message.channel.send(algebraembed2)
}
})
}
})}}
It doesnt work and I dont understand how to fix it. The bot even posts two embeds at times.
Thank you.
When you return the filter the second reaction will always return true because you're not comparing it to anything.
To fix this issue, replace return reaction.emoji.name === '1️⃣' || '2️⃣' && user.id === message.author.id && !user.bot; with return reaction.emoji.name === '1️⃣' || reaction.emoji.name === '2️⃣' && user.id === message.author.id && !user.bot;.
Full example:
module.exports = {
name: 'algebra',
description: 'This is a command that should help you with algebra',
async execute(message, args, Discord, client){
let ageEmbed = new Discord.MessageEmbed()
.setTitle('Algebra! Its pretty damn hard.')
.addField('So you are just starting off with Algebra and you want to learn the basics, Well no problem just react with 1️⃣')
.addField('So you want to know about the questions such as "*x* = 21+5, what is *x*?" well no problem just react with 2️⃣')
.setColor('#FF1493')
let msg = await message.channel.send(ageEmbed);
await msg.react('1️⃣');
await msg.react('2️⃣');
const filter_age = (reaction, user) => {
return reaction.emoji.name === '1️⃣' || reaction.emoji.name === '2️⃣' && user.id === message.author.id && !user.bot;
}
const collector_age = msg.createReactionCollector(filter_age, {
time: 30000,
max: 1
});
collector_age.on('collect', async (reaction, user) => {
if (reaction.emoji.name === '1️⃣') {
let justbegginingembed = new Discord.MessageEmbed()
.setDescription(`Just beggining with Algebra..`)
.addField("This is where just beggining with algebra will be");
message.channel.send(justbegginingembed)
} else {
collector_age.on('collect', async (reaction, user) => {
if (reaction.emoji.name === '2️⃣') {
let algebraembed2 = new Discord.MessageEmbed()
.setDescription('so you want to learn about ....')
.addField('THIS IS WHERE');
message.channel.send(algebraembed2)
}
});
}
});
}
}
I am in the process of creating a Would You Rather command for my bot.
I have everything in place, except one feature I can't work out how to implement.
I would love to have it so that when someone reacts with their answer (🅰️ or 🅱️) the bot then edits the embed and puts the user who replied under their answer like this:
The code I currently have is:
case "wyr":
embed.setColor('#fc2803')
embed.setTitle('Would You Rather?')
embed.setDescription(':a: **Be able to fly** \n \n :b: **Breathe underwater**')
message.channel.send(embed).then(m => m.react('🅰️')).then(r => r.message.react('🅱️'));
You can easily implement this using the discord.js-collector package.
But if you want to make it using plaine discord.js, then you will have to listen to reactions when sending the embed, and then edit it to what you want like in the example i will give.
Simple Example Using The discord.js-collector package:
Code Here
And Live Preview Here
Simple Example Using Plaine Discord.js:
const Discord = require("discord.js");
const embed = new Discord.MessageEmbed()
.setTitle("Hello There!");
const embedtosend = await message.channel.send(embed).then(m => m.react('🅰️')).then(r => r.message.react('🅱️'));
const filter = (reaction, user) => {
return ['🅰️', '🅱️'].includes(reaction.emoji.name) && user.id === message.author.id;
};
message.awaitReactions(filter, { max: 2, time: 60000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '🅰️') {
const someotherembed = new Discord.MessageEmbed()
.setTitle("This Is A Different Hello There!");
embedtosend.edit(someotherembed)
} else if (reaction.emoji.name === '🅱️') {
const anotherembed = new Discord.MessageEmbed()
.setTitle("This Is A Different Embed!");
embedtosend.edit(anotherrembed)
}
});
I haven't tested this code, so it might not work...
I've made a code for verify command, it's reacting to certain embed. But when I reboot the bot won't recognize it, so users can still react but won't get role at all. I've heard I can use Partial Event but I don't want to make it inside index.js.
Code:
let wembed = new Discord.MessageEmbed()
.setAuthor("Test")
.setColor("PURPLE")
.setDescription(arg1)
const reactmessage = await client.channels.cache.get(chx).send(wembed)
await reactmessage.react('✅');
const filter = (reaction, user) => reaction.emoji.name === '✅' && !user.bot;
const collector = reactmessage.createReactionCollector(filter);
collector.on('collect', async reaction => {
const user = reaction.users.cache.last();
const guild = reaction.message.guild;
const member = guild.member(user) || await guild.fetchMember(user);
member.roles.add("725747028323205170");
});
If there is a way of doing this help would be appreciated!
https://discordjs.guide/popular-topics/partials.html#enabling-partials
index.js:
const client = new Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });
your messageReactionAdd file:
module.exports = async (reaction, user) => {
if(reaction.partial) await reaction.fetch();
}
Now all reactions are sent to messageReactionAdd, now you just have to validate the reaction and add the role if it corresponds to the right emoji and the right message
in my discord bot, i have a command that scrambles a word, and the first user that responds in the chat gets a reward. i already made the command to await a message response, but how can i save who responded? heres my code so far
authorID = msg.author.id;
const filter = response => {
return response.author.id === authorID;
}
msg.channel.awaitMessages(filter, {
max: 1,
time: 5000
})
.then(mg2 => {
if (mg2.first().content === "1") {
msg.channel.send("you said 1");
}
});
With the filter, you are only allowing the original msg author to enter, you should make it so it detects if the msg.content equals the unscrabbled word. Also you don't have errors: ["time"].
const filter = m => m.content === unscrabledWord;
msg.channel.awaitMessages(filter, { max: 1, time: 5000, errors: ["time"] })
.then(collected => {
const winner = collected.first().author;
msg.channel.send(`${winner} Won!`)
})
.catch(() => msg.channel.send("Nobody guessed it"));