Sparkly
Sparkly

Reputation: 11

How to make my discord.js-bot log the edit of a message?

I'm trying to make my bot log when and what has been edited in a message.

This is the code of the listener:

client.on('messageUpdate', (oldMessage, newMessage,message) => {
    client.on('messageUpdate', (oldMessage, newMessage,message) => {
const MessageLog = client.channels.cache.find(channel => channel.id ==='802262886624919572');
var embed = new Discord.MessageEmbed()
.setAuthor(message.author.username).catch(console.error)
.setTimestamp(new Date())
.setColor('#392B47')
.addFields(
    {name: 'original:',value: oldMessage},
    {name: 'edit:', value: newMessage}    );
MessageLog.send(embed);
 });

So far he is having the problem fetching the message.author.username I have tried defyning the message with oldmessage and newmessage, but same problem there.

Console log: TypeError: Cannot read property 'author' of undefined

Upvotes: 0

Views: 4394

Answers (2)

teslafan0
teslafan0

Reputation: 25

First thing, the 'messageUpdate' event has two arguments: oldMessage and newMessage.
Second thing, you can't use .catch on a MessageEmbed. So instead, we will remove that part of code and replace it with an user check.
Third thing, you don't need to chain messageUpdate events.
Fourth thing, you can call setTimestamp empty as it will default to Date.now() (source)
So lets fix your code:

client.on('messageUpdate', (oldMessage, newMessage) => { // Old message may be undefined
   if (!oldMessage.author) return;
   const MessageLog = client.channels.cache.find(channel => channel.id ==='802262886624919572');
var embed = new Discord.MessageEmbed()
.setAuthor(newMessage.author.username)
.setTimestamp()
.setColor('#392B47')
.addFields(
    {name: 'original:',value: oldMessage},
    {name: 'edit:', value: newMessage}    );
MessageLog.send(embed);
}

Upvotes: 0

Fish
Fish

Reputation: 26

The error message says that message is undefined. The problem is that the messageUpdate event doesnt give the last message as a parameter (https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=e-messageUpdate) To fix this you could replace the message in message.author.username with newMessage.

I also found another problem with your code, when you add the fields to the embed, you should use newMessage.content as the value, instaid of just newMessage

Upvotes: 1

Related Questions