Discord Bot - Reaction Collector/Embed Edit - node.js

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...

Related

Playing a specific song when a user calling a command

so what I am trying to reach is when a user call a !song1 command the bot will play the specified YouTube URL for this command, I do not want any kind of search just playing the YouTube URL I put in the code, please see the code below I know it is old and I am looking for something new and no errors in future, and I am really beginner in coding bots.
I hope I explain this issue successfully, thanks in advance.
song1.js
const ytdl = require('ytdl-core');
const Discord = require('discord.js')
const client = new Discord.Client()
module.exports = {
commands: 'song1',
callback: (message) => {
const url = 'https://www.youtube.com/song1url';
if (message.content == '!song1') {
if (!message.member.voice.channel) return message.reply("you should be on a vc");
message.member.voice.channel.join().then(VoiceConnection => {
VoiceConnection.play(ytdl(url)).on("finish", () =>
VoiceConnection.disconnect());
const embed = new Discord.MessageEmbed()
.setTitle('Now Playing: song1')
.setAuthor(message.author.username)
.setTimestamp()
.setColor('#447a88')
message.reply(embed)
}).catch(e => console.log(e))
}
}
}

Trying to catch response but why wont it send?

I am currently working on a command right now that I would like the server owner to use. When done it sends the embed and will wait for the server owner to paste we will say the channel id. I want to label this to call in another command but at the moment it sends the embed but when I send a channel ID nothing happens. No message is sent but it also does not break the bot. I am still able to send the command again and again.
Please do not mind the slop, I am new to all this and trying to learn as I go any tips would be appreciated!
EDIT: I am using version 12 (12.5.3)
const { prefix } = require('../config.json');
const Discord = require('discord.js');
const client = new Discord.Client();
module.exports = {
name: 'setchannel',
description: 'command to assign bot to channel',
execute(message, args) {
if(message.channel.type == "dm") {
const keepinservercommandembed = new Discord.MessageEmbed()
.setColor('#FF0000')
.setTitle('**Im Sorry!**')
.addFields(
{ name: `**Can` + `'t accept: ${prefix}setchannel**`, value: 'Please keep this command to the server!' },
)
message.channel.send(keepinservercommandembed);
}
else {
const messagesender = message.member.id;
const serverowner = message.guild.owner.id;
const setchannelembed = new Discord.MessageEmbed()
.setColor('#FF0000')
.addFields(
{ name: `**Let's get searching!**`, value: `Hello ${message.guild.owner}, what channel would you like me to post group finding results?` },
)
const notownerembed = new Discord.MessageEmbed()
.setColor('#FF0000')
.addFields(
{ name: `**Whoops!**`, value: `Sorry only ${message.guild.owner} can use this command.` },
)
if(messagesender == serverowner) {
message.delete ();
const filter = m => m.content.message;
const collector = message.channel.createMessageCollector(filter, { time: 15000 });
message.channel.send(setchannelembed).then(() => {
collector.on('collect', m => {
message.channel.send(`${m.content}`)
});
});
}
else {
message.delete ();
message.channel.send(notownerembed);
}
}
}
}
After comparing your code to my similiar discord.js v13 code and checking out the v12 docs, I think I found the problem.
I don't think m.content.message is a function. If your aim is for someone to paste the channel ID, the filter code would be:
const filter = m => m.author.id === message.guild.owner.id;
To filter out other people who may be entering an ID (assuming the input is valid).
Use the filter as follows:
message.channel.createMessageCollector({filter: filter, time: 15000 });
To filter out invalid inputs:
collector.on('collect', m => {
if (isNaN(m)) return; //all channel id's in discord are integers (numbers)
else message.channel.send(`${m.content}`);
});

Having issues with making a reaction post another embed on Discord.js

I am having an issue with making an embed have reactions and when you react to it, it will post another embed in the discord chat, this is all trying to make a bot that explains about maths and algebra, I also need to do this in one file, I can't find any tutorials and I canp't figure out how to do it...
Thank you!
module.exports = {
name: 'algebra',
description: 'This is a test embed command',
async execute(message, args, Discord, client){
const newEmbed = new Discord.MessageEmbed()
.setColor('#3B0099')
.setThumbnail('https://i.imgur.com/a/Wnjoo1L')
.setTimestamp()
.setAuthor('This was written by (REDACTED)')
.setDescription('Once finished reading this you should know a lot about algebra!')
.setFooter('Algebra')
.addFields(
{name: 'The Basics Behind Algebra', value: 'Algebra can be a simpal concept if approached correctly, as in you must know what the steps you will take are and complete them in the correct order and way. Algebra mainly focuses on substituting numbers with letters. If you want to learn more about this please react with the letter A'},
{name: 'The Theory of Algebra', value: 'The theory of Algebra is one that is... If you want to learn more about this please react with the letter B'}
)
message.channel.send(newEmbed).then((msg) =>{
msg.react('1️⃣');
msg.react('2️⃣');
msg.react('3️⃣');
msg.react('4️⃣');
msg.react('5️⃣');
msg.react('6️⃣');
msg.react('7️⃣');
msg.react('8️⃣');
msg.react('9️⃣');
msg.react('🔟');
}).catch((err)=>{
throw err;
});
}
}
Please, read the documentation about collectors (https://discordjs.guide/popular-topics/collectors.html)
This is an example
let ageEmbed = new Discord.MessageEmbed()
.setTitle('Please, specify your age with the reactions below...')
.addField('1️⃣', '17-')
.addField('2️⃣', '18+')
.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 embed2 = new Discord.MessageEmbed()
.setDescription(`You reacted with :one:`)
.addField('Hey', ":one:");
message.channel.send(embed2)
} else {
let embed = new Discord.MessageEmbed()
.setDescription("You reacted with :two:")
message.channel.send(embed)
}
})
If you react with one, the bot will send an embed saying "You reacted with :one:". Else, the bot will send an embed saying "You reacted with :two:

Making reactions with Partial Events

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

How to get all the mentions from an user in a channel discord.js

So, I'm trying to create a simple bot that when the user writes:
!howmanypoints {user}
It should just go to a specific channel, and search how many times it was mentioned in the specific channel.
As of now, I encountered the function fetchMentions() but it seems deprecated and the API doesn't like it when a bot tries to do this..
Any input or at least a better approach for this. Maybe I'm fixated with making it work.
const Discord = require('discord.js')
const client = new Discord.Client()
client.on('message', msg => {
if (msg.content.includes('!howmanypoints') || msg.content.includes('!cuantospuntos')) {
const canal_de_share = client.channels.find('name', 'dev')
const id_user = client.user.id
client.user.fetchMentions({guild: id_user })
.then(console.log)
.catch(console.error);
}
})
ClientUser.fetchMentions() is deprecated, and only available for user accounts.
As an alternative, you could fetch all the messages of the channel and iterate over each, checking for mentions.
Example:
let mentions = 0;
const channel = client.channels.find(c => c.name === 'dev');
const userID = client.user.id;
channel.fetchMessages()
.then(messages => {
for (let message of messages) {
if (message.mentions.users.get(userID)) mentions++;
}
console.log(mentions);
})
.catch(console.error);

Resources