Detect message's name of sent image - javascript

I'm using a pokecord bot on my discord server, and I also have my own bot in the server. I want to make a feature, that when pokecord sends messageEmbed with pokemon spawn, my bot reacts to it. I tried the message.content.startsWith() method, but because it is messageEmbed, it isn't working. Is there some other way to do that? I was thinking, when you click on the pokemon picture, it opens in a new tab, called PokecordSpawn.jpg, maybe it could be done somehow in this way?

You can check for the image file name in the MessageEmbed like so:
if (message.embeds) {
if (message.embeds[0].image.url.includes('PokecordSpawn.jpg')) {
// Do something
}
}
You can also use message.embeds.forEach() in case the message has multiple embeds

Related

Message Specific PFP. (like TupperBox)

I am looking to send a message that has a different profile picture than the bot. Basically, like Tupper Bot does. I can't seem to figure out how to do it anywhere, I've read the documentation and searched around.
You can use a webhook to do what you describe. If you already have a webhook link see here on how to use it. This works from external programs too.
If you don't, you can create a webhook with your bot and then send messages to it. In this example we will create a simple webhook and send a message to it.
First we should check if the channel already has a webhook. That way you don't create a new one everytime you use the command. You do that with the fetchWebhooks() function which returns a collection if there is a webhook present. If that is the case we will get the first webhook in the collection and use that. Note: if you have more then one webhook you can of course add additional checks after the initial one.
If there are no webhooks present, we create a new one with createWebhook(). You can pass additional options after the name.
let webhook;
let webhookCollection = await message.channel.fetchWebhooks();
if (webhookCollection.first()) {
webhook = webhookCollection.first();
} else {
webhook = await message.channel.createWebhook("new webhook", {
avatar: "https://i.imgur.com/tX5nlQ3.jpeg"
});
}
Note: in this example we use the channel the message came from but you can use any channel you want
Technically we can now send messages already.
webhook.send("this is a test");
What you can also do is create a webhookClient. Thats a little like the discord client you create when you start your bot. It takes the webhook ID and token as well as additional options you can set for clients.
const hookclient = new Discord.WebhookClient(webhook.id, webhook.token);
This lets you use a few additional methods like setInterval.
hookclient.setInterval(() => {
hookclient.send("This is a timer test");
}, 1000)
This lets you use the webhook without creating or fetching one first.

Issue with Discord.js avatarURL

.avatarURL for discord.js is not displaying any image.
I'm trying to make a command for my discord bot that lets someone ping another and have the bot display their profile picture. I quickly homed in on .avatarURL being the solution for this, but it doesn't seem to be displaying any image at all. Any help would be appreciated.
if (message.content.startsWith(`${prefix}showpfp`)) {
let embed = new Discord.RichEmbed().setTitle(tMember.displayName + "'s Profile Picture").setImage(tMember.avatarURL).setColor('#add8e6')
message.channel.send(embed);
}
after typing in command _showpfp #user the bot will respond with
user's Profile Picture
and thats it...
The issue is that tMember doesn't have a property avatarURL. In your comment you say that you get the value of tMember by doing message.mentions.members.first(), however message.mentions.members is a collection of GuildMembers. A GuildMember doesn't have a property avatarURL.
However, the class User does have the property avatarURL. Therefor you need to fetch the member as an instance of User first. Luckily for you, the class GuildMember has a property .user which does just that.
So the fix for your problem is to change the parameter of the setImage to tMember.user.avatarURL, as can be seen below.
if (message.content.startsWith(`${prefix}showpfp`)) {
let embed = new Discord.RichEmbed().setTitle(tMember.displayName + "'s Profile Picture").setImage(tMember.user.avatarURL).setColor('#add8e6')
message.channel.send(embed);
}

use react() from DM

I want to use .react() from DM way. But the reaction into the post is sended by bot. I need send a reaction by the user.
var guild = bot.guilds.get(GUILD_ID);
if(guild && guild.channels.get(ch_ID)){
guild.channels.get(ch_ID).fetchMessage(msg_ID).then(message => {
message.react('🤔')
})
}
As I shared in a comment...
A client cannot add a reaction as another user.
Unfortunately, that means that you can't accomplish what you're trying to do within the Discord API. You can read reactions, but the bot simply would never be able to force someone to add a reaction.

Problems with messageDelete in Discord js

I try to make my bot on Discord server. Want to make a function, which will copy all deleted message in text channel, but, messageDelete hear only deleted message which was writing after bot start. When I delete message which make earlier bot start, its not work.
{
client.on ("messageDelete", messageDelete =>{
let channel = client.channels.find(channel => channel.name === 'log-deleted-message')
console.log(`Deleted :${messageDelete.content}`)
channel.send(`${messageDelete.author.username} write : ${messageDelete.content}`
})
}
The above answer is now outdated.
With Discord.js V12 you can now Cache messages on the messageDelete event, You would need to enable Partials. Once this is enabled, you can fetch the message beforehand like this:
if(message.partial){
let msg = await message.fetch()
console.log(msg.content)
}
That will then log the content of the previously uncached message.
messageDelete is an event that is called when a message is deleted while the bot is on. If a message is deleted before the bot is turned on there is no way to recover it, which is why it's known as deleted. The only way to accomplish the goal that you want is to leave the bot on permanently. Read more in the docs if you want more information.

Using Discord.js to check the url of an image in an embed

The idea
What I seek to do, is upon a message being sent, if that message is an embed, I want my bot to check the image in that embed (if there is one) for it's url, and if the url matches a specific url I provide, for the bot to send a specific message that I provide.
The issue
While I know the event for messages being sent (client.on("message", function(message) {)
I have no idea how to have the bot check to see if that message is an embed, and how to have it check the url of the image in that embed, if there is one.
If you subscribe the event client.on("message"), you'll recieve all the Messages the bot can read.
With message.embeds you'll get an array with all of the embeds on that message.
With MessageEmbed you can look at messageEmbed.thumbnail or messageEmbed.image depending on which one you want, and get the url out of it.
client.on("message", message => {
if(message.embeds.length > 0){
var embed = message.embeds[0];
if(embed.image && embed.image.url == "myurl.com"){
// do something
}
}
}
Something along those lines.

Categories