messageUpdate function being triggered when message text hasn't been updated

Hi there!

My bot has a logging system which logs any edited messages into a predefined log channel. For normal messages that only include text, my current code works great. However, any message that includes a link that Discord automatically turns into an embed (such as the one below) will trigger the message update event and send the message to the log channel.

The above Discord message will trigger the bot and it will send a log message where the Original message is exactly the same as the New message (see image below for output.)

Discord_FkkzsLxmMN

This is a problem because the actual text hasn’t been updated, thus no need for the event to send a log message.

I have tried adding this line of code:

if (oldMessage == newMessage) return;

with the idea that if the original message is the same as the new message, the bot will ignore it but sadly this didn’t work and I’m really confused as to why.

Any help with this would be much appreciated! Below is my current code for the message update event.

client.on("messageUpdate", (oldMessage, newMessage) => {
    if (oldMessage.guild.id != config.lcoz) return;
    if (oldMessage.author.bot) return;
    if (oldMessage == newMessage) return;
    fs = require("fs");
    fs.readFile("logs", "utf8", (err, data) => {
        if (err) {
            console.log("Error reading logs file!");
            console.log(err);
        }

        if (data == "on") {
            channel = client.channels.cache.get(config.logchannel);
            const Embed = new MessageEmbed()
                .setColor("#FF0000")
                .setTitle(":hammer: Message Updated:")
                .addField("**Member:**", oldMessage.author)
                .addField("**Channel:**", oldMessage.channel)
                .addField("**:pencil: Original message:**", oldMessage, true)
                .addField("**:pencil: New message:**", newMessage, true)
                .setThumbnail(oldMessage.author.displayAvatarURL({ dynamic: true }))
                .setTimestamp()
                .setFooter("Luck's Chill-Out Zone", "https://luckunstoppable7.com/media/logo.png");
            channel.send(Embed);
        }
    });
});
1 Like

These statements:

and

From these, you’re seeing that there’s more to Discord’s idea of what a “message” is than the piece of text that a user can type. That piece of text is the message’s “content,” and there’s an object around it with several other fields. You’re probably aware that there’s also, for example, the author of the message, what channel it was sent in, etc. You’ve even used these other fields later in your code.

According to the API docs, there’s also embeds, which sounds like something that would change when, as you describe, “Discord automatically turns into an embed.” And for that field to change, we’d naturally count that as the overall message having changed, and we’d expect to see that messageUpdate event.


So what is it with the oldMessage == newMessage comparison? If you’ve understood the above, then you’d have to wonder what it really means in JavaScript for two objects to be equal. There’s a guide here describing all these weird cases:

You’re using the == operator, and you’re comparing an object to an object. See if you can look up the rules on that and see how it fits in to the behavior you’re observing with this version of your code.

The rest is about coming up with what code you need to do what you really wanted. I won’t do this part for you, but here are a few points to get you started:

  1. You’re on the right track with the idea of using a comparison operator.
  2. You don’t care about the entire message object being equal—notably, the embeds field, for one, doesn’t matter.
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.