gpt4 book ai didi

javascript - Discord.js:无法读取 Null 的属性 "Name"- 如何修复此问题?

转载 作者:行者123 更新时间:2023-12-02 23:35:10 25 4
gpt4 key购买 nike

我想出了如何让我的 Discord 机器人在特定用户玩特定游戏时向特定 channel 发送图像,但我还有另一个问题。

当应用程序关闭时,我收到此错误消息,“无法读取 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"
]
});
}
}
});

我希望即使应用程序关闭,代码也能正常工作。相反,它无法读取 nullname。我该如何修复这个错误?

最佳答案

当用户停止玩游戏时,很可能会引发此错误,因为 newMember.presence.game 在逻辑上将为 null。然后,当您尝试读取 newMember.presence.gamename 时,您会收到错误。

使用修改后的代码:

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);
}
});

关于javascript - Discord.js:无法读取 Null 的属性 "Name"- 如何修复此问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56318191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com