Skip to content
Advertisement

Inconsistent error when checking for user ID’s while using partials as well. (Discord.js)

The code below is the code I am using to track deleted and edited messages. However, when I try to check the user ID (because sometimes, discord bots like groovy constantly delete and edit messages and I wanted those kinds of users to bypass the logging system) and make sure it’s the ID of the bots I want, I get this error.

  if (msg.author.id !== "760491381034319883" && msg.author.id !== "234395307759108106" && msg.author.id !== "235088799074484224" && msg.author.id !== "252128902418268161") {
               ^ TypeError: Cannot read property 'id' of null

However, the error is very inconsistent, sometimes it happens, sometimes it doesn’t, this same also goes for the messageUpdate event. Also to add, I’m not completely sure if I am using partials correctly.

bot.on('messageDelete', msg => {
if (msg.author.id !== "760491381034319883" && msg.author.id !== "234395307759108106" && msg.author.id !== "235088799074484224" && msg.author.id !== "252128902418268161") {
    if (!msg.partial) {
        const channel = bot.channels.cache.get('781032825050759229');
        if (channel) {
            const MessageDeletedByUser = new Discord.MessageEmbed()
                .setColor('YELLOW')
                .setThumbnail(msg.author.avatarURL(msg.author.defaultAvatarURL))
                .addField(msg.author.tag + " (MESSAGE DELETED)", ":warning:** <@" + msg.author.id + ">" + "'s message was deleted by the user**n ")
                .addField("n:speaking_head: Full Message :speaking_head:", "- `" + msg.content + "`")
                .addField("nn:newspaper: Message Info :newspaper: ", "n- Channel ID: " + msg.channel + ' [' + "<#" + msg.channel.id + ">" + "]" + "n- User ID: " + msg.author.id + " [" + "<@" + msg.author.id + ">" + "]")
                .setTimestamp()
                .setFooter('--Depressed Children--');
            channel.send(MessageDeletedByUser)

        }
    }
}});


bot.on('messageUpdate', (oldMessage, newMessage) => {
    if (newMessage.channel.type !== 'dm') {
        if (newMessage.author.id !== '760491381034319883' && newMessage.author.id !== '234395307759108106' && newMessage.author.id !== '235088799074484224' && newMessage.author.id !== '252128902418268161') {
            if (!oldMessage.partial) {
                const channel = bot.channels.cache.get('781032825050759229');
                if (channel) {
                    const MessageEditedByUser = new Discord.MessageEmbed()
                        .setColor('BLURPLE')
                        .setThumbnail(newMessage.author.avatarURL(newMessage.author.defaultAvatarURL))
                        .addField(newMessage.author.tag + " (MESSAGE EDITED)", ":warning:** <@" + newMessage.author.id + ">" + "'s message was edited by the user**n ")
                        .addField("n:speaking_head: Old Message :speaking_head:", "- `" + oldMessage.content + "`")
                        .addField("n:speaking_head: New Message :speaking_head:", "- `" + newMessage.content + "`")
                        .addField("nn:newspaper: Message Info :newspaper: ",
                            "n- Channel ID: " + newMessage.channel + ' [' + "<#" + newMessage.channel.id + ">" + "]" +
                            "n- User ID: " + newMessage.author.id + " [" + "<@" + newMessage.author.id + ">" + "]" +
                            "n- Message ID: " + newMessage.id + ' [' + newMessage.url + '] ')
                        .setTimestamp()
                        .setFooter('--Depressed Children--');
                    channel.send(MessageEditedByUser)


                }
            }
        }
    }
}

);

If someone could let me know what the problem is and how to fix it, that would be much appreciated!

Advertisement

Answer

You are checking for the message.author before checking for a partial. A partial message is only ever guaranteed to have an ID, as mentioned here. Always check that something exists in a partial before trying to access it.

Hope this helped.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement