how to capture the reaction of an emoji in a message - javascript

when a message receives a reaction or emoji, I want to save the letter "y" in Wish.
I need to use emoji in client.on("message", (message), not another, the next error is
ReferenceError: emoji is not defined
but i have no idea how to solve it
const { Client, Intents } = require("discord.js-selfbot");
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS],
});
let token = "";
client.on('ready', () => {
console.log('Ok!');
});
let ListWish = ["❤","💖","💘"];
let Wish;
client.on("message", (message) => {
if(ListWish.includes(emoji.name)){
Wish = 'y';
}
if(message.embeds.length >= 0)
// Check if the Message has embed or not
{
let embed = message.embeds
for(let i = 0; i < embed.length; i++)
{
if (!embed[i] || !embed[i].description) return;
// check each embed if it has description or not, if it doesnt then do nothing
{
if(Wish === 'y'){
message.channel.send("Have emoji heart")
}
}
}
}
});
client.login(token);

You can use the Message.awaitReactions() method to listen for reactions on a certain message without the need of using client.on('event').
The documentation already contains code that easily summarizes how to use it, so I won't write any code here.
Edit: I almost forgot to add this, I am pretty sure you can pass in a callback after the options, so there is no need to use .then().

Related

How can I make a code which is triggered by someone mentioning certain user and warns the triggering user

This is a code idea I had to make code that is triggered by mentioning a certain user and warning the triggered user, but it just doesn't work
const Client = require("./Structures/Client.js");
const config = require("./Data/config.json");
const client = new Client();
client.on("messageCreate", message => {
if(message.content == "<#USER2ID>") message.reply("User1#1234 has mentioned User2#1234");
});
client.start(config.token);
You should be using the message.mentions object instead of getting the content of the message. Also is client.start not a function. You should use the client.login function instead. You've also forgot to mention the intents that you want to use. Please take a look at the following example:
const {Client, Intents} = require('discord.js'); // Import the client and intents
const config = require('./Data/config.json'); // Import the config
const client = new Client({
intents: [Intents.FLAGS.GUILD_MESSAGES] // Uses the intent 'GUILD_MESSAGES' to be able to get the messages
}); // Creates the client
client.on('messageCreate', message => { // Creates an event listener
if(message.author.bot || message.channel.type === `DM`) return; // Returns when the author is a bot or if the message is being send in a DM
if(message.mentions.members.get(`0123456789`)){ // If the user with the user id '0123456789' has been mentioned in the message
message.reply(`The user ${message.mentions.members.get(`0123456789`).user.tag} has been mentioned`); // Sends the reply
}
});
client.login(config.token); // Login to the bot

Is it possible to send a message to every guild my bot is in?

I have tried every tutorial but they don't work, here is my current code:
bot.on('message', message => {
if (message.content.startsWith(`${prefix}globalannounce`)) {
var msg = message.content.split(" ").slice(1).join(" ")
var guildList = bot.guilds.array;
try {
let messageToSend = new Discord.MessageEmbed()
.setTitle("Hello, you don't see me messaging in your server often...")
.setDescription(`I have just flown in to tell you that my developers have something to say: \n ${msg}`)
guildList.array.forEach(channel => {
if (channel.type === 'text') channel.send(messageToSend).catch(console.error)
});
} catch (err) {
console.log(err);
}
}
});
It will not work and the error is TypeError: Cannot read property 'array' of undefined.
discord.js v12.x uses Managers, so you'll have to go through the cache property to get a list of guilds. See this post for more information.
Also, GuildList.forEach() would iterate a function throughout all Guilds in the collection, not all the channels. You can use the Channel.type property and Collection.find() to find the first text channel available.
var guildList = bot.guilds.cache; // go through the cache property
try {
let messageToSend = new Discord.MessageEmbed()
.setTitle("Hello, you don't see me messaging in your server often...")
.setDescription(
`I have just flown in to tell you that my developers have something to say: \n ${msg}`
);
guildList.forEach((guild) => {
const channel = guild.channels.cache.find((channel) => channel.type === 'text') // try to find the channel
if (!channel) return; // if it couldn't find a text channel, skip this guild
channel.send(messageToSend); // otherwise, send the message
});
} catch (err) {
console.log(err);
}
If you're using discord.js v12
You have the error TypeError: Cannot read property 'array' of undefined. This means that bot.guilds is equal to undefined. So the problem is here :
var guildList = bot.guilds.array;
You'll have to replace it with
var guildList = bot.guilds.cache
So your entire code would look like this :
bot.on('message', message => {
if (message.content.startsWith(`${prefix}globalannounce`)) {
var msg = message.content.split(" ").slice(1).join(" ")
var guildList = bot.guilds.cache
try {
let messageToSend = new Discord.MessageEmbed()
.setTitle("Hello, you don't see me messaging in your server often...")
.setDescription(`I have just flown in to tell you that my developers have something to say: \n ${msg}`)
guildList.forEach(guild =>{
guild.channels.cache.find(c => c.type === 'text').send(messageToSend)
});
} catch (err) {
console.log(err);
}
}
});

Creating a bot in discord. (Javascript)

Good morning! I would like to know how I can make my bot to ban words, but not just the word, I want it to ban the entire sentence that is written. I've done this, but the problem is that it doesn't ban the entire sentence.
client.on('message', message => {
if (message.content === 'BAD WORD EXAMPLE') {
message.delete({
timeout: 1,
reason: 'Mensaje eliminado, contenido inapropiado..'
});
message.channel.send(' Mensaje eliminado por contenido inapropiado');
}
})
If you want to simply ban the member that sent a message including badWords, basically you can follow #Nurfey's answer and there's much simpler code, like
const badWords = ["foo", "faz", "bar"];
client.on('message', message => {
const hasBadWord = badWords.some(banWord => message.includes(banWord))
if(hasBadWord) {
// delete the message
}
});
If your checking will be more complex so that you want to write 2+ sentences, you can also do this:
const hasBadWord = badWords.some(banWord => {
// multiple sentences here, and returns true or false
})
The full documentation of Array.some() is available on MDN.
Based on what you've written, you could try this:
const badWords = ["foo", "faz", "bar"];
client.on('message', message => {
let hasBadWord = false;
badWords.forEach(badWord => {
if(hasBadWord === false) {
if(message.includes(badWord)) hasBadWord = true; // you could do message.toLowerCase().includes(badWord) for case sensitivity
}
});
if(hasBadWord === true) {
// delete the message
}
});
it's not particularly refined, but you could optimize it if you want, this is just for making it as easily readable as I can make it

Filtering the content of all messages in a channel

I'm writing a bot which logs user reactions to specific messages (events). Each generates a message containing id of the event to which they reacted on a log channel. Now I'm trying to get the bot to remove any generated messages for an event if the reaction was removed. My code:
client.on("messageReactionRemove", (reaction, user) => {
if(user.bot) return;
let message = reaction.message;
ORid = message.id;
ORid = ORid.toString();
if(message.channel.id == '709887163084439555') {
if(message.content.charAt(0) == '|'){
var logChannel = client.channels.get('710092733254991893')
logChannel.fetchMessages().then(messages => {
var msgToRemove = messages.filter(m => message.content.includes(ORid))
logChannel.bulkDelete(msgToRemove)
}).catch(err => {
console.log('Error while doing Bulk Delete');
console.log(err);
});
} else return;
} else return;
});
The first id is event channel, the other is where logs are generated. However, instead of filtering all the messages on the log channel, it checks whether the event contains it's id and if so, purges all logs. What can I do to fix this?
It looks like you have a bug in the line filtering the messages:
var msgToRemove = messages.filter(m => message.content.includes(ORid))
You're checking message.content.includes(ORid), which is always going to be false.
This is because you're using the message variable defined earlier instead of m from the filter. The correct way to write the line would be:
var msgToRemove = messages.filter(m => m.content.includes(ORid))

How Find Emojis By Name In Discord.js

So I have been utterly frustrated these past few days because I have not been able to find a single resource online which properly documents how to find emojis when writing a discord bot in javascript. I have been referring to this guide whose documentation about emojis seems to be either wrong, or outdated:
https://anidiots.guide/coding-guides/using-emojis
What I need is simple; to just be able to reference an emoji using the .find() function and store it in a variable. Here is my current code:
const Discord = require("discord.js");
const config = require("./config.json");
const fs = require("fs");
const client = new Discord.Client();
const guild = new Discord.Guild();
const bean = client.emojis.find("name", "bean");
client.on("message", (message) => {
if (bean) {
if (!message.content.startsWith("#")){
if (message.channel.name == "bean" || message.channel.id == "478206289961418756") {
if (message.content.startsWith("<:bean:" + bean.id + ">")) {
message.react(bean.id);
}
}
}
}
else {
console.error("Error: Unable to find bean emoji");
}
});
p.s. the whole bean thing is just a test
But every time I run this code it just returns this error and dies:
(node:3084) DeprecationWarning: Collection#find: pass a function instead
Is there anything I missed? I am so stumped...
I never used discord.js so I may be completely wrong
from the warning I'd say you need to do something like
client.emojis.find(emoji => emoji.name === "bean")
Plus after looking at the Discord.js Doc it seems to be the way to go. BUT the docs never say anything about client.emojis.find("name", "bean") being wrong
I've made changes to your code.
I hope it'll help you!
const Discord = require("discord.js");
const client = new Discord.Client();
client.on('ready', () => {
console.log('ready');
});
client.on('message', message => {
var bean = message.guild.emojis.find(emoji => emoji.name == 'bean');
// By guild id
if(message.guild.id == 'your guild id') {
if(bean) {
if(message.content.startsWith("<:bean:" + bean.id + ">")) {
message.react(bean.id);
}
}
}
});
Please check out the switching to v12 discord.js guide
v12 introduces the concept of managers, you will no longer be able to directly use collection methods such as Collection#get on data structures like Client#users. You will now have to directly ask for cache on a manager before trying to use collection methods. Any method that is called directly on a manager will call the API, such as GuildMemberManager#fetch and MessageManager#delete.
In this specific situation, you need to add the cache object to your expression:
var bean = message.guild.emojis.cache?.find(emoji => emoji.name == 'bean');
In case anyone like me finds this while looking for an answer, in v12 you will have to add cache in, making it look like this:
var bean = message.guild.emojis.cache.find(emoji => emoji.name == 'bean');
rather than:
var bean = message.guild.emojis.find(emoji => emoji.name == 'bean');

Categories

Resources