我运行了这段代码
bot.on("presenceUpdate", (oldMember, newMember) => {
let guildChannels = newMember.guild.channels;
if(oldMember.status !== newMember.status){
console.log(`${newMember.user.username} is now ${newMember.presence.status}`);
guildChannels.find('name','test')
.send(`${newMember.user.username} is now ${newMember.presence.status}`)
}
});
在运行此代码段(假设要登录控制台以及将消息发送到“测试”频道)时,会出现以下错误:
TypeError:无法读取未定义的属性“状态”
此行中的错误
if(oldMember.status !== newMember.status){
这是完整的错误日志
at Client.<anonymous> (C:\Users\Dillon\Desktop\test\test.js:18:18)
at Client.emit (events.js:315:20)
at PresenceUpdateAction.handle (C:\Users\Dillon\node_modules\discord.js\src\client\actions\PresenceUpdate.js:39:19)
at Object.module.exports [as PRESENCE_UPDATE] (C:\Users\Dillon\node_modules\discord.js\src\client\websocket\handlers\PRESENCE_UPDATE.js:4:33)
at WebSocketManager.handlePacket (C:\Users\Dillon\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (C:\Users\Dillon\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (C:\Users\Dillon\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (C:\Users\Dillon\node_modules\ws\lib\event-target.js:125:16)
at WebSocket.emit (events.js:315:20)
at Receiver.receiverOnMessage (C:\Users\Dillon\node_modules\ws\lib\websocket.js:797:20)
答案 0 :(得分:0)
首先全部使用newPresence和oldPresence 第二。它的NewPresence.activities获取更新活动的数组。 所以你的鳕鱼看起来像
bot.on("presenceUpdate", (oldPresence, newPresence) => {
let guildChannels = newMember.guild.channels;
if(oldPresence.activities !== newMember.activities){
console.log(`I dont know what u are trying to do.. You can try to get the the stuffs from the array by doing ${newPresence.activities[0].<PROPERTY>}`);
guildChannels.find('name','test')
.send(`${newPresence.username} is now ${newPresence.activities[0].<PROPERTY>}`)
}```
});