简体   繁体   English

类型错误:无法读取未定义的属性(读取“缓存”)(discord.js)

[英]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.我也尝试过重新安装 discord.js 但仍然没有成功。 (I am using Discord v12) It works originally but every time throws that error so stops the whole thing. (我使用的是 Discord v12)它最初可以工作,但每次都会抛出该错误,因此会停止整个过程。 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.编辑:因为我收到了关于错误来源的评论,因为它显示路径来自 node_modules 文件夹。 I only have one js file which is the code I sent.我只有一个 js 文件,这是我发送的代码。 I do not make the node_modules files.我不制作 node_modules 文件。

However, here I have attached the MessageCreate.js which is found within the node_modules folder.但是,我在这里附上了位于 node_modules 文件夹中的 MessageCreate.js。

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

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.你应该看看你正在使用的 DiscordJS 的版本。 If it's < 13.3.1, consider upgrading - 13.3.1 contains the fix for this issue.如果是 < 13.3.1,请考虑升级 - 13.3.1 包含针对此问题的修复程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Discord.js - TypeError:无法读取未定义的属性(读取“设置”) - Discord.js - TypeError: Cannot read properties of undefined (reading 'set') TypeError:无法读取未定义的属性(读取“设置”)Discord.js - TypeError: Cannot read properties of undefined (reading 'set') Discord.js Discord.js:TypeError:无法读取未定义的属性(读取“has”) - Discord.js: TypeError: Cannot read properties of undefined (reading 'has') Discord.js:TypeError:无法读取未定义的属性(读取“添加”) - Discord.js: TypeError: Cannot read properties of undefined (reading 'add') 类型错误:无法读取未定义的属性(读取“createdTimestamp”)discord.js - TypeError: Cannot read properties of undefined (reading 'createdTimestamp') discord.js 类型错误:无法读取 discord.js 中未定义的属性(读取“发送”) - TypeError: Cannot read properties of undefined (reading 'send') in discord.js TypeError:无法读取 Discord.js 中未定义的属性“缓存” - TypeError: Cannot read property 'cache' of undefined in Discord.js Discord.js:类型错误:无法读取 null 的属性(读取“状态”) - Discord.js: TypeError: Cannot read properties of null (reading 'status') discord.js 使用 axios:TypeError:无法读取未定义的属性(读取“短暂”) - discord.js using axios: TypeError: Cannot read properties of undefined (reading 'ephemeral') discord.js v13 类型错误:无法读取未定义的属性(读取“存在”) - discord.js v13 TypeError: Cannot read properties of undefined (reading 'presence')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM