为什么我在 discord.js 中收到客户端错误
Why am I getting a client error in discord.js
我正在我的 discord.js 机器人中使用此 Discord.js Guide 创建 !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)
我该怎么做才能解决这个问题?
你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}`);
},
};
cache
属性 之前的内容是 undefined
。
所以在你的情况下是 client.guilds。鉴于不能为正确的 djs 客户端对象取消定义,您在调用该文件时可能没有以正确的顺序传递您的值。也就是说,client
可能不是真正的 djs 客户端 class/object.
我正在我的 discord.js 机器人中使用此 Discord.js Guide 创建 !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)
我该怎么做才能解决这个问题?
你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}`);
},
};
cache
属性 之前的内容是 undefined
。
所以在你的情况下是 client.guilds。鉴于不能为正确的 djs 客户端对象取消定义,您在调用该文件时可能没有以正确的顺序传递您的值。也就是说,client
可能不是真正的 djs 客户端 class/object.