Skip to content
Advertisement

TypeError: Cannot read properties of undefined (reading ‘cache’) (discord.js)

This is the error I am facing:

/home/ry/node_modules/discord.js/src/client/actions/MessageCreate.js:11
      const existing = channel.messages.cache.get(data.id);
                                        ^

TypeError: Cannot read properties of undefined (reading 'cache')
    at MessageCreateAction.handle (/home/ry/node_modules/discord.js/src/client/actions/MessageCreate.js:11:41)
    at Object.module.exports [as MESSAGE_CREATE] (/home/ry/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/home/ry/node_modules/discord.js/src/client/websocket/WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (/home/ry/node_modules/discord.js/src/client/websocket/WebSocketShard.js:435:22)
    at WebSocketShard.onMessage (/home/ry/node_modules/discord.js/src/client/websocket/WebSocketShard.js:293:10)
    at WebSocket.onMessage (/home/ry/node_modules/ws/lib/event-target.js:132:16)
    at WebSocket.emit (node:events:527:28)
    at Receiver.receiverOnMessage (/home/ry/node_modules/ws/lib/websocket.js:1047:20)
    at Receiver.emit (node:events:527:28)
    at Receiver.dataMessage (/home/ry/node_modules/ws/lib/receiver.js:517:14)

This is the my code that I run.

const Discord = require('discord.js');
const client = new Discord.Client();

var threads = [
  {
    channel: '498391317399863307',
    //interval: 1000 * 7200,
    message: 'hi'
  },
  {
    channel: '498391317399863307',
    //interval:  1000 * 7220,
    message: 'hello'
  },
  {
    channel: '498391317399863307',
    //interval: 1000 * 43200,
    message: 'bye'
  }
]


client.on('ready', () => {
console.log(Discord.version)
  for (i in threads)
  {
    function a(t, message) {
      client.channels.cache.get(t.channel).send(t.message).then(() => {
          let x = 1;
          if(t.message == "hi") {
            x = (Math.random() * ((1000*9000) - (1000*7200))) + 7200000; 
            console.log("hi: " + (x/1000) + " secs");
            
          } else if (t.message == "hello") {
            x = (Math.random() * ((1000*9900) - (1000*25000))) + 9000000; 
            console.log("nHello: " + (x/1000) + " secs");

          } else if (t.message == "bye") {
            x = (Math.random() * ((1000*48600) - (1000*72000))) + 43200000; 
            console.log("nBye: " + (x/1000) + " secs");
          }
          
        setTimeout(a, x, t)
      }).catch(e => {
        console.log(e);
      })
    }
    setTimeout(a, threads[i].interval, threads[i])
  }
});

client.login('');

Essentially the error given is not to do with my code itself. I’ve tried also reinstalling discord.js but still no luck. (I am using Discord v12) It works originally but every time throws that error so stops the whole thing. I was wondering if anyone knows the solution for this issue. Thanks!

Edit: As I have received comments about where the error is from, as it shows the path is from the node_modules folder. I only have one js file which is the code I sent. I do not make the node_modules files.

However, here I have attached the MessageCreate.js which is found within the node_modules folder.

'use strict';

const Action = require('./Action');
const { Events } = require('../../util/Constants');

class MessageCreateAction extends Action {
  handle(data) {
    const client = this.client;
    const channel = client.channels.cache.get(data.channel_id);
    if (channel) {
      const existing = channel.messages.cache.get(data.id);
      if (existing) return { message: existing };
      const message = channel.messages.add(data);
      const user = message.author;
      let member = message.member;
      channel.lastMessageID = data.id;
      if (user) {
        user.lastMessageID = data.id;
        user.lastMessageChannelID = channel.id;
      }
      if (member) {
        member.lastMessageID = data.id;
        member.lastMessageChannelID = channel.id;
      }

      /**
       * Emitted whenever a message is created.
       * @event Client#message
       * @param {Message} message The created message
       */
      client.emit(Events.MESSAGE_CREATE, message);
      return { message };
    }

    return {};
  }
}

module.exports = MessageCreateAction;

Advertisement

Answer

I’m receiving the same type of error on my bot now, while it was previously working fine for a long time. You should take a look at the version of DiscordJS you’re using. If it’s < 13.3.1, consider upgrading – 13.3.1 contains the fix for this issue.

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