我正在使用这个 Discord.js Guide 在我的 discord.js 机器人中创建一个 !stats 命令。 当我使用基本命令处理程序时,这是我的 stats.js 代码:
module.exports = {
name: 'stats',
description: 'React to a message',
execute(message, client) {
message.channel.send(`Server count: ${client.guilds.cache.size}`);
},
};
然后我收到此错误:
TypeError: Cannot read property 'cache' of undefined
at Object.execute (E:\The Owner\Naffy Dharni\Discord\Bots\Toxado Manager\commands\Utility\stats.js:5:55)
at Object.execute (E:\The Owner\Naffy Dharni\Discord\Bots\Toxado Manager\events\message.js:69:12)
at Client.<anonymous> (E:\The Owner\Naffy Dharni\Discord\Bots\Toxado Manager\bot.js:30:61)
at Client.emit (events.js:315:20)
at MessageCreateAction.handle (E:\The Owner\Naffy Dharni\Discord\Bots\Toxado Manager\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (E:\The Owner\Naffy Dharni\Discord\Bots\Toxado Manager\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (E:\The Owner\Naffy Dharni\Discord\Bots\Toxado Manager\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (E:\The Owner\Naffy Dharni\Discord\Bots\Toxado Manager\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (E:\The Owner\Naffy Dharni\Discord\Bots\Toxado Manager\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (E:\The Owner\Naffy Dharni\Discord\Bots\Toxado Manager\node_modules\ws\lib\event-target.js:132:16)
我该怎么做才能解决这个问题?
答案 0 :(得分:0)
cache
属性之前的内容是 undefined
。
所以在你的情况下是 client.guilds。鉴于对于适当的 djs 客户端对象,这不能是未定义的,您在调用该文件时可能没有以正确的顺序传递您的值。也就是说,client
可能不是实际的 djs 客户端类/对象。
答案 1 :(得分:-1)
你使用的 discord.js 版本是什么?
在 12 版本上添加了缓存。
使用 npm i discord.js@12
或仅删除字段 cache
上的调用。
module.exports = {
name: 'stats',
description: 'React to a message',
execute(message, client) {
message.channel.send(`Server count: ${client.guilds.size}`);
},
};