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

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

这是我面临的错误:

/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)

这是我 运行 的代码。

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('');

基本上给出的错误与我的代码本身无关。 我也试过重新安装 discord.js 但还是不行。 (我正在使用 Discord v12) 它最初有效,但每次都会抛出该错误,因此会停止整个过程。 我想知道是否有人知道这个问题的解决方案。谢谢!

编辑:因为我收到了关于错误来源的评论,因为它显示路径来自 node_modules 文件夹。我只有一个 js 文件,这是我发送的代码。我不制作 node_modules 个文件。

但是,我在这里附上了 MessageCreate.js,它位于 node_modules 文件夹中。

'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;

我现在在我的机器人上收到相同类型的错误,而它以前可以正常工作很长时间。 你应该看看你正在使用的 DiscordJS 的版本。如果是 < 13.3.1,请考虑升级 - 13.3.1 包含针对此问题的修复程序。