0
votes

I'm making a logs system for my Discord bot. It basically logs every deleted or edited message in a channel I specified.

Everything worked well, but when I added the code to log edited messages, deleting your message started to crash the bot. When I delete a message, it gives an error in messageUpdate saying field values can't be empty. It still logs the deleted message before crashing, though.

Here is my code:

client.on("messageDelete", (message) => {
    deletedMessage = message;
    if (message.guild.id == 806539207173472257) {
        const logsDeletedEmbed = new Discord.MessageEmbed()
            .setColor("#ff0000")
            .setTitle(`Deleted message in #${message.channel.name}`)
            .setAuthor(message.author.username, message.author.avatarURL())
            .setDescription(message.content)
            .setTimestamp();
        client.channels.cache.get("807286270274830357").send(logsDeletedEmbed);
    }
});

client.on("messageUpdate", (oldMessage, newMessage) => {
    editedMessage = oldMessage;
    if (oldMessage.guild.id == 806539207173472257) {
        const logsEditedEmbed = new Discord.MessageEmbed()
            .setColor("#277ecd")
            .setTitle(`Edited message in #${oldMessage.channel.name}`)
            .setAuthor(oldMessage.author.username, oldMessage.author.avatarURL())
            .addField("Old message", oldMessage.content)
            .addField("New message", newMessage.content)
            .setTimestamp();
        client.channels.cache.get("807286270274830357").send(logsEditedEmbed);
    }
});
2

2 Answers

0
votes

Just a guess here (as I'm not familiar with the discord bot api), but it sounds like deleting a message also triggers a "messageUpdate" event. Based on the error that field values cannot be empty I would assume that when messageUpdate events trigger on a deletion either oldMessage.content or newMessage.content is empty/null/undefined. My guess is newMessage.content as there probably isn't a new message on a delete. It's also possible that the oldMessage or newMessage object itself is null/undefined.

This could be fixed by adding some logic to verify you have content in oldMessage/oldMessage.content or newMessage/newMessage.content before triggering the logic that logs the edit.

0
votes

After some trial and error, I got the problem. It doesn't trigger messageUpdate when I delete messages, it triggers it when I send messages. When I send messages, it triggers it without oldMessage and newMessage having values. Here's how I fixed it:

client.on("messageUpdate", (oldMessage, newMessage) => {
  if (oldMessage.content != "" && newMessage.content != "") {
  editedMessage = oldMessage;
    if (oldMessage.guild.id == 806539207173472257) {
      const logsEditedEmbed = new Discord.MessageEmbed()
      .setColor("#277ecd")
      .setTitle(`Edited message in #${oldMessage.channel.name}`)
      .setAuthor(oldMessage.author.username, oldMessage.author.avatarURL())
      .addField("Old message", oldMessage.content)
      .addField("New message", newMessage.content)
      .setTimestamp();
      client.channels.cache.get('807286270274830357').send(logsEditedEmbed);
    }
  }
});