So I want to add an emoji reaction to the bot's message. But idk what code to make it.
I only know the code to react to the command message.
else if(command === "guessage"){
message.channel.send({ embed: {
color: 16758465,
title: "Are you...",
description: Math.floor((Math.random() * 20) + 11) + " " + "years old?"
}
})
message.react('👍').then(() => message.react('👎'));
const filter = (reaction, user) => {
return ['👍', '👎'].includes(reaction.emoji.name) && user.id === message.author.id;
};
message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
.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 => {
message.reply('you reacted with neither a thumbs up, nor a thumbs down.');
});
}
Handle the promise of each Message#reply()
Example Using Callbacks:
message.reply('you reacted with a thumbs up.').then(botsMessage => botsMessage.react('EMOJI-HERE'));
Example using Async/Await
(Recommend for maintaining reaction order):
// Inside an async function
const botsMessage = await message.reply('you reacted with a thumbs up.');
await botMessage.react('EMOJI-1');
await botMessage.react('EMOJI-2');
await botMessage.react('EMOJI-3');
Understanding Promises - Discord.JS
You need to await the sending of the message and use it's message object.
For example:
else if (command === "guessage") {
(async () => {
let bmsg = await message.channel.send({
embed: {
color: 16758465,
title: "Are you...",
description: Math.floor((Math.random() * 20) + 11) + " " + "years old?"
}
})
await bmsg.react('👍');
await bmsg.react('👎');
const filter = (reaction, user) => {
return ['👍', '👎'].includes(reaction.emoji.name) && user.id === message.author.id;
};
bmsg.awaitReactions(filter, {
max: 1,
time: 60000,
errors: ['time']
})
.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 => {
message.reply('you reacted with neither a thumbs up, nor a thumbs down.');
});
})();
}
I'm using an async IIFE to allow await to be used. There are other places where await should be used, but I'll leave that up to you.
Related
I'm pretty trash at coding so I need a little bit of help. I'm trying to code my discord bot to delete someone's messages for one minute after they click a react emoji. It sounds simple but for my tiny pea brain, it's not. This is what I have got so far. It deletes all messages from different users and guilds it's in, forever. I want it so it only delete messages in one channel for one minute.
client.once('message', async userMessage => {
if (userMessage.content.startsWith(''))
{
botMessage = await userMessage.channel.send('Who here likes goats?')
await botMessage.react("👍")
await botMessage.react("👎")
const filter = (reaction, user) => {
return (
["👍", "👎"].includes(reaction.emoji.name) && user.id === userMessage.author.id
);
};
botMessage
.awaitReactions(filter, { max: 1, time: 60000, errors: ["time"] })
.then((collected) => {
const reaction = collected.first();
if (reaction.emoji.name === "👎") {
userMessage.channel.send(`${userMessage.author}, how dare you. I guess no on here likes me. Hmmm, because of that I shall now eat all your messages! BAAAAAHAHAHHAHAHA!`)
setTimeout(() => {
client.on("message", async msg => {
if (author.msg.content.startsWith("")) {
userMessage.channel = await msg.delete();
}
});
}, 2000);
} else {
userMessage.reply("Thanks!");
}
})
.catch((_collected) => {
userMessage.channel.send("Hehe")
});
}
});
Btw, the code is in discord.js!
Your problem is this chunk of code
setTimeout(() => {
client.on("message", async msg => {
if (author.msg.content.startsWith("")) {
userMessage.channel = await msg.delete();
}
});
}, 2000);
This is not how you use events.
A) Your message event is nested within another which could cause memory leaks.
B) To get the content you need to use msg.content, author.msg Is not a thing.
C) I assume your intention here: msg.content.startsWith("") is to always fire the if statement, in that case why not do if (true).
Here's how I would do it:
Create a Set in the namespace which will hold id's of users who's messages should be deleted
const toDelete = new Set();
If they react with a 👎 add them to the set.
if (reaction.emoji.name === "👎") {
userMessage.channel.send('Your message here');
if (!toDelete.has(userMessage.author.id)) {
toDelete.add(userMessage.author.id);
}
}
On each message event check if the author of the message has their id in the set, If so delete their message
client.once('message', async userMessage => {
if (toDelete.has(userMessage.author.id)) {
return userMessage.delete()
.catch(console.error);
}
if (userMessage.content.startsWith('')) {
// Rest of your code
I think your problem in understanding how everything works.
I took everything from discord.js documentation.
Type reaction command to see how it works.
const Discord = require("discord.js");
require("dotenv").config();
const TOKEN = process.env.TOKEN||"YOUR TOKEN";
const PREFIX = process.env.PREFIX||"YOUR PREFIX";
const bot = new Discord.Client();
bot.on("ready", async function(e) {
console.log("Loaded!");
})
bot.on("message", async function(message) {
if (message.author.bot) return;
if (!message.content.startsWith(PREFIX)) return;
let args = message.content.slice(PREFIX.length).trim().split(/\s+/);
let command = args.splice(0, 1).toString().toLowerCase();
if (command == "reaction") {
message.delete();
let msg = await message.channel.send("Click on the reaction");
await msg.react("👍");
await msg.react("👎");
let filter = (reaction, user) => {
return ["👍", "👎"].includes(reaction.emoji.name) && user.id == message.author.id;
}
msg.awaitReactions(filter, {max: 1, time: 10000, errors: ["time"]}).then(collected => {
let reaction = collected.first();
if (reaction.emoji.name == "👎") {
return message.channel.send("downvote");
}
return message.channel.send("upvote");
}).catch(e => {
message.channel.send("user didn't vote");
})
}
})
bot.login(TOKEN);
I'm trying to have a bot react when users click the emojis on the bot's embeds.
I'm receiving in the console:
UnhandledPromiseRejectionWarning: TypeError: chestEmbed.awaitReactions
is not a function
The code:
module.exports = {
name: 'enterhouse',
aliases: 'eh',
permissions: ["ADMINISTRATOR", "MANAGE_MESSAGES", "CONNECT"],
description: "Pick a number",
async execute(client, message, args, Discord){
const chestEmbed = new MessageEmbed()
.setColor('#FFA500')
.setImage('https://imageURL.jpg');
message.channel.send(chestEmbed).then(chestEmbed => {chestEmbed.react('1️⃣').then(() => chestEmbed.react('2️⃣').then(() => chestEmbed.react('3️⃣')))}
)
.catch(() => console.error('One of the emojis failed to react.'));
const filter = (reaction, user) => {
return (['1️⃣', '2️⃣', '3️⃣'].includes(reaction.emoji.name) && user.id === message.author.id);
};
chestEmbed.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '1️⃣') {
chestEmbed.delete();
message.reply('you reacted with 1');
} else if (reaction.emoji.name === '2️⃣') {
message.reply('you reacted with 2');
} else {
message.reply('you reacted with 3');
}
})
.catch(collected => {
message.reply('Time is up, you did not react.');
});
}
}
It worked fine when it was message.awaitReactions instead.
Any help is really appreciated!
You are awaiting messages from the embed it's self not the message the embed is in.
Simple fix:
...
chestEmbed = await message.channel.send(chestEmbed)
...
Full Code:
module.exports = {
name: 'enterhouse',
aliases: 'eh',
permissions: ["ADMINISTRATOR", "MANAGE_MESSAGES", "CONNECT"],
description: "Pick a number",
async execute(client, message, args, Discord) {
const chestEmbed = new MessageEmbed()
.setColor('#FFA500')
.setImage('https://imageURL.jpg');
chestEmbed = await message.channel.send(chestEmbed)
chestEmbed.react('1️⃣').then(() => chestEmbed.react('2️⃣')).then(() => chestEmbed.react('3️⃣'))
const filter = (reaction, user) => {
return (['1️⃣', '2️⃣', '3️⃣'].includes(reaction.emoji.name) && user.id === message.author.id);
};
chestEmbed.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '1️⃣') {
chestEmbed.delete();
message.reply('you reacted with 1');
} else if (reaction.emoji.name === '2️⃣') {
message.reply('you reacted with 2');
} else {
message.reply('you reacted with 3');
}
})
.catch(collected => {
message.reply('Time is up, you did not react.');
});
}
}
Currently, I am using Discord.js to make a bot.
client.on('message', (message) => {
if (message.content === '$wa') {
message.channel.send({ embed: exampleEmbed }).then((embedMessage) => {
embedMessage.react('❤️');
embedMessage
.awaitReactions(
(filter = (reaction, user) => {
return reaction.emoji.name === '❤️' && user.id === message.author.id;
}),
{ max: 2, time: 60000, errors: ['time'] }
)
.then((collected) => {
const reaction = collected.first();
if (reaction.emoji.name === '❤️') {
message.channel.send(
':sparkling_heart: **Hanno** and **Roronoa Zoro** are now married! :sparkling_heart:'
);
}
});
});
}
});
If I type $wa the bot shows some embed. But the thing is that it automatically adds a heart to the embed. I want that if I click the heart as well (for a total count of 2 hearts) it executes the if statement at the bottom.
I've tried multiple methods but none worked. This is also my first time with Discord.js
You need to account for the bots own reaction. I recommend redoing your filter implementation to something like this.
The key takeaway is that you have to add !user.bot to the filter so that the bot's own reaction is ignored
const filter = (reaction, user) => {
return reaction.emoji.name === "❤️" && user.id === message.author.id && !user.bot
}
embedMessage.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
Please try this:
client.on('message', message => {
if (message.content === '$wa') {
message.channel.send({ embed: exampleEmbed }).then(embedMessage => {
embedMessage.react('❤️');
embedMessage.awaitReactions(filter = (reaction, user) => {
return reaction.emoji.name === '❤️' && user.id === message.author.id;
},
{ max: 1, time: 60000, errors: ['time'] }).then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '❤️') {
message.channel.send(':sparkling_heart: **Hanno** and **Roronoa Zoro** are now married! :sparkling_heart:');
}
}).catch(() => {
// user didn't react with ❤️ in given time (here: 60 secs)
message.channel.send('no reaction in time');
});
});
}
});
I changed the max value to 1 and also added a catch block to catch a UnhandledPromiseRejectionWarning. If you don't do so in the future, it might exit the program with an error. You can of course execute whatever you like when the user didn't react to the embedMessage in time.
How to fix this ? i want to delete message when user click reaction X
client.on('message', async message => {
if (message.channel.id === emojiChannelID) {
try {
await message.react('✅');
await message.react('✖');
} catch(err) {
console.error(err);
}
}
});```
There's an message.awaitReaction() in discord.js, that will return reactions from users
// Filter for only
const filter = function(reaction, user) {
return reaction.emoji.name === '✅' || reaction.emoji.name === '✖';
}
// {...}
let reactionMessage = await message.react('✅');
// Make sure to set max: 1 so that the promise returns after the first reaction
let reactionCollection = await reactionMessage.awaitReactions(filter, { max: 1});
// reactionCollection is a Collection<string, MessageReaction>
// Use first() to get the first (and only)
let reaction = reactionCollection.first();
Kian here,
This code should work for you,
if you would like I can go through and explain each line :)
Have a good day chief!
async function emojiMessage(message, validReactions) {
for (const reaction of validReactions) await message.react(reaction);
const filter = (reaction, user) => validReactions.includes(reaction.emoji.name) && (!user.bot)
return message
.awaitReactions(filter, {
max: 1,
time: 42000
})
.then(collected => collected.first() && collected.first().emoji.name);
}
async function deleteMessage(message) {
const emoji = await emojiMessage(message, ["✅", "❌"]);
console.log(emoji)
// if the emoji is a tick:
if (emoji === "✅") {
// delete their message
console.log("tick")
if (message.deletable == true) {
console.log("can delete")
console.log("attempting to delete")
message.delete()
}
if (!message.deletable == false) {
"cannot delete"
}
} else if (emoji === "❌") { // if the emoji is a cross
/*
* do something else
*/
return;
}
}
client.on('message', message => {
if (message.channel.id === emojiChannelID) {
// runs the function
deleteMessage(message)
}
/*
* do something else
*/
})
Note:
First upload 🎉
I've tried my best to make the code understandable/work , if there is any issues feel free to comment, I'll fix it :)
Example Usage:
const m = await message.channel.send('hi!');
reactionDelete(m, message, 20000); // assuming 'message' is the actual sent message
async function reactionDelete (botMessage, playerMessage, timeout) {
const filter = (reaction, user) => {
return ['🗑️'].includes(reaction.emoji.name) && user.id === playerMessage.author.id;
};
botMessage.react('🗑️');
botMessage.awaitReactions(filter, { max: 1, time: timeout})
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '🗑️') {
botMessage.delete();
}
})
.catch(collected => {
if (botMessage.deletable) botMessage.reactions.removeAll();
});
};
I'm setting up a bot, who will await for a user's reaction to write a short message. Currently, the bot responds itself to his reaction! Why?
const { Client, RichEmbed, Discord } = require('discord.js');
const config = require('./config.json');
const client= new Client();
client.on('ready', () => {
console.log('je suis pret!');
client.user.setActivity("/help pour appeler de l'aide (avec le code NOMAD bien sur)", { type: 'WATCHING' });
})
client.on('messageReactionAdd', (reaction, user) => {
console.log(`${user.username} reacted with "${reaction.emoji.name}".`);
});
client.on('messageReactionRemove', (reaction, user) => {
console.log(`${user.username} removed their "${reaction.emoji.name}" reaction.`);
});
client.on('message', async message => {
if (message.author.bot) return;
if (message.content.toLowerCase().startsWith('!rea')) {
try {
const sentMessage = await message.channel.send('Select a number.');
for (let n = 1; n <= 5; n++) await sentMessage.react(`${n}⃣`);
const filter = (reaction, user) => ['1', '2', '3', '4', '5'].includes(reaction.emoji.name.slice(0, 1)) && user.id === message.author.id;
const collected = await sentMessage.awaitReactions(filter, { maxMatches: 1, time: 60000 });
if (collected.size === 0) {
await sentMessage.clearReactions();
await message.channel.send('Your time ran out.');
} else {
const reaction = collected.first();
switch(reaction.emoji.name.slice(0, 1)) {
case '1':
await message.channel.send('You chose `one`.');
break;
}
}
} catch(err) {
console.error(err);
}
}
});
client.login(config.token);
I found the problem: the bot considers the first message: "!rea", so if I add the reaction below !rea, it answers!
How can I fix this, because I want that he considered the embed reactions!
Thank you for your help
Your client's message event is emitted by any message, including those sent by itself.
To prevent bots (and therefore your client as well) from triggering the code, add a condition checking the author's User.bot property at the beginning of the callback, and return if it's true.
if (message.author.bot) return; // Stop if the author is a bot.
// Continue with your code.
Additionally, you're collecting reactions on the wrong message, and on every one. message outside of your first then() callback is the one the bot received.
To make things much more simple and readable, we can use the await keyword (note that it can only be used asynchronous context, like in a function declared as async).
Combining these changes...
client.on('message', async message => {
if (message.author.bot) return;
if (message.content.toLowerCase().startsWith('!rea')) {
try {
const sentMessage = await message.channel.send('Select a number.');
for (let n = 1; n <= 5; n++) await sentMessage.react(`${n}⃣`);
const filter = (reaction, user) => ['1', '2', '3', '4', '5'].includes(reaction.emoji.name.slice(0, 1)) && user.id === message.author.id;
const collected = await sentMessage.awaitReactions(filter, { maxMatches: 1, time: 60000 });
if (collected.size === 0) {
await sentMessage.clearReactions();
await message.channel.send('Your time ran out.');
} else {
const reaction = collected.first();
switch(reaction.emoji.name.slice(0, 1)) {
case '1':
await message.channel.send('You chose `one`.');
break;
}
}
} catch(err) {
console.error(err);
}
}
});