我是discord.js的新手,我想知道如何解决此错误。您能否修复我的代码,然后告诉我将来如何避免此问题?谢谢!
代码:
client.on('message', message => {
if (message.content === '-ping') {
const embed = {
"title": "**Ping Results**",
"fields": [
{
"name": "Bot",
"value": "" +(botMsg.createdAt - message.createdAt) + "ms"
},
{
"name": "API ",
"value": Math.round(client.ping) + "ms"
},
{
"name": "Uptime",
"value": msToTime(client.uptime) + "ms"
}
]
};
message.channel.send({ embed });
}
});
控制台错误:
ReferenceError: botMsg is not defined
at Client.<anonymous> (C:\Users\parke\Documents\Discord Bot\index.js:15:31)
at Client.emit (events.js:314:20)
at MessageCreateAction.handle (C:\Users\parke\Documents\Discord Bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (C:\Users\parke\Documents\Discord Bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (C:\Users\parke\Documents\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (C:\Users\parke\Documents\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (C:\Users\parke\Documents\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (C:\Users\parke\Documents\Discord Bot\node_modules\ws\lib\event-target.js:125:16)
at WebSocket.emit (events.js:314:20)
at Receiver.receiverOnMessage (C:\Users\parke\Documents\Discord Bot\node_modules\ws\lib\websocket.js:797:20)
答案 0 :(得分:0)
此错误表示未定义变量botMsg
。
如我所见,您想执行ping命令,并且botMsg是您要收集的已发送消息。
此代码应该有效。
client.on('message', message => {
if (message.content === '-ping') {
message.channel.send("Checking the ping...").then(botMsg => {
const embed = {
"title": "**Ping Results**",
"fields": [
{
"name": "Bot",
"value": "" +(botMsg.createdAt - message.createdAt) + "ms"
},
{
"name": "API ",
"value": Math.round(client.ping) + "ms"
},
{
"name": "Uptime",
"value": msToTime(client.uptime) + "ms"
}
]
};
botMsg.edit({ embed });
})
}
});