[英]Discord.js: Cannot Read Property “Name” of Null - How do I fix this?
我想出了如何讓我的Discord機器人每當特定用戶玩特定游戲時將圖像發送到特定頻道,但是我還有另一個問題。
當應用程序關閉時,出現此錯誤,提示"Cannot read property 'name' of null."
我該如何解決?
我沒有嘗試過任何東西,因為我不知道如何使用null
。
// Game Detector \\
client.on("presenceUpdate", (oldMember, newMember) => {
if(newMember.id === '406742915352756235') {
if(newMember.presence.game.name === 'ROBLOX') { // New Example: ROBLOX
console.log('ROBLOX detected!');
client.channels.get('573671522116304901').send('**Joining Game:**', {
files: [
"https://cdn.discordapp.com/attachments/567519197052272692/579177282283896842/rblx1.png"
]
});
}
}
});
我希望代碼能夠正常工作,即使應用程序關閉也是如此。 相反,它無法讀取null
name
。 如何解決此錯誤?
用戶停止玩游戲時最有可能引發此錯誤,因為newMember.presence.game
在邏輯上將為null
。 然后,當您嘗試讀取newMember.presence.game
name
時,會收到錯誤消息。
使用此修改后的代碼:
client.on('presenceUpdate', (oldMember, newMember) => {
if (newMember.id !== '406742915352756235') return; // only check for this user
if (newMember.presence.game && newMember.presence.game.name === 'ROBLOX') {
console.log('ROBLOX detected.');
const channel = client.channels.get('573671522116304901');
if (!channel) return console.log('Unable to find channel.');
channel.send('**Joining Game:**', {
files: ['https://cdn.discordapp.com/attachments/567519197052272692/579177282283896842/rblx1.png']
}).catch(console.error);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.