Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Discord.js:无法读取属性;名称“;空-如何修复此问题?_Javascript_Null_Discord.js - Fatal编程技术网

Javascript Discord.js:无法读取属性;名称“;空-如何修复此问题?

Javascript Discord.js:无法读取属性;名称“;空-如何修复此问题?,javascript,null,discord.js,Javascript,Null,Discord.js,我想出了如何让我的Discord机器人在特定用户玩特定游戏时向特定频道发送图像,但我还有另一个问题 当应用程序关闭时,我收到这样一个错误,即,“无法读取null的属性‘name’。如何解决此问题 我没有尝试过任何东西,因为我不知道如何使用null // Game Detector \\ client.on("presenceUpdate", (oldMember, newMember) => { if(newMember.id === '406742915352756235') {

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

当应用程序关闭时,我收到这样一个错误,即,
“无法读取null的属性‘name’。
如何解决此问题

我没有尝试过任何东西,因为我不知道如何使用
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',(旧成员,新成员)=>{
if(newMember.id!=“406742915352756235”)返回;//仅检查此用户
if(newMember.presence.game&&newMember.presence.game.name==='ROBLOX'){
log(“检测到ROBLOX”);
const channel=client.channels.get('573671522116304901');
if(!channel)返回console.log('找不到频道');
频道发送(“**加入游戏:*”{
文件:['https://cdn.discordapp.com/attachments/567519197052272692/579177282283896842/rblx1.png']
}).catch(控制台错误);
}    
});

这意味着
newMember.presence.game
null
。什么是
newMember
?你能举个例子吗?@JackBashford我想这有点像是一个
GuildMember
。无意冒犯,但这并不是真正的问题,因此评论没有任何意义。但这就是问题所在-
newMember.presence.game
null
“当用户停止玩游戏时,很可能会抛出此错误,因为
newMember.presence.game
在逻辑上是
null
”这就是我刚才说的。对不起,我不明白你说的“应用程序关闭”是什么意思。这段代码应该可以解决你的问题。