(node:4468) UnhandledPromiseRejectionWarning: TypeError: Cannot read property ‘guild’ of undefined Discord.js

I was following a tutorial on modlog channel but i keep getting this error.

(node:4468) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'guild' of undefined

Here is the code

client.on('messageDelete', async (client, message) => {
    if (message.partial) await message.fetch();

    let modlog = db.get(`moderation.${message.guild.id}.modlog`);
    if (!modlog) return;

    if (message.channel.id === modlog.channel) return;

    let toggle = modlog.toggle;
    if (!toggle || toggle == null || toggle == false) return;

    const embed = new MessageEmbed()
    .setTitle("Message Deleted")
    .setDescription(`Message deleted in <#${message.channel.id}> by **${message.author.tag}** n> ${message.content}`)

    return message.guild.channels.cache.get(modlog.channel).send(embed);
})

Answer

The Client#messageDelete event gives you only the Message as the first argument. The second argument is undefined.


client.on("messageDelete", async (message) => {
    if (message.partial) await message.fetch();

    let modlog = db.get(`moderation.${message.guild.id}.modlog`);
    if (!modlog) return;

    if (message.channel.id === modlog.channel) return;

    let toggle = modlog.toggle;
    if (!toggle || toggle == null || toggle == false) return;

    const embed = new MessageEmbed().setTitle("Message Deleted").setDescription(`Message deleted in <#${message.channel.id}> by **${message.author.tag}** n> ${message.content}`);

    return message.guild.channels.cache.get(modlog.channel).send(embed);
});