1

I'm looking for a way yo check if 2 images are the same in discord.js, I'm programming a discord bot and couldn't find a way to check if 2 images are the same, I tried checking the attachments (see code below) but the biggest problem is that if you copy a image and send it in discord, the attachment URL changes (even if it is the same image), so checking if they are the same doesn't work. How can I check if a discord photo is an certain image?

This is what I have tried so far:

const image = new Discord.MessageAttachment ("https://cdn.discordapp.com/attachments/866328575451856937/868592900274012190/ZxZsWb93GUuscqCfviBuMYqk6mofTSE_1f4nHsR5uwyhZLllNEFFzyEO8zr0l8iRQJwKYHyKDVLEY1WJvWJIp0n1PfPNdXebz8c3.png")
if (msg.attachments.size > 0) {
    if (msg.attachments.first().attachment == image.attachment) {
        msg.channel.send("Same photo is sent")
    }
}
Sulcheck
  • 11
  • 1
  • My guess is you would have to use something like canvas – MrMythical Jul 24 '21 at 21:07
  • Maybe this could help a bit: [How to compare content of two html5-canvas-elements?](https://stackoverflow.com/questions/8341791/how-to-compare-content-of-two-html5-canvas-elements#) – MrMythical Jul 24 '21 at 21:18
  • Does this answer your question? [How to compare two images using Node.js](https://stackoverflow.com/questions/18510897/how-to-compare-two-images-using-node-js) – node_modules Jul 24 '21 at 22:55

1 Answers1

1

If you have access to the files or are able to download them, you can calculate and compare the MD5 hashes. If the hashes are the same, then you can assume they are the same.

I'll hazard a guess, but you can google how to do it if this doesn't work.

const crypt = require("crypto")

function createMD5Hash(attachment){
    return crypt.createHash("md5")
        .update(attachment)
        .digest("base64");
}

const image = new Discord.MessageAttachment ("https://cdn.discordapp.com/attachments/866328575451856937/868592900274012190/ZxZsWb93GUuscqCfviBuMYqk6mofTSE_1f4nHsR5uwyhZLllNEFFzyEO8zr0l8iRQJwKYHyKDVLEY1WJvWJIp0n1PfPNdXebz8c3.png")

const imageHash = createMD5Hash(image.attachment);
if (msg.attachments.size > 0) {
    const attachmentHash = createMD5Hash(msg.attachments.first().attachment);
    if (attachmentHash === imageHash) {
        msg.channel.send("Same photo is sent")
    }
}
Alex Simons
  • 38
  • 1
  • 4
  • Thanks for your answer, but it didn't work for me :( I tried to google more about it and found an easy way out, by comparing the width, height and size of the images. Source: https://stackoverflow.com/questions/1310378/determining-image-file-size-dimensions-via-javascript – Sulcheck Jul 25 '21 at 16:49