无法读取未定义的属性“活动”

时间:2020-05-10 11:09:12

标签: node.js discord.js

我的问题是,当用户上线并在不和谐状态下上线时,在控制台中出现错误“
TypeError:无法读取未定义的属性“活动”,并且bot崩溃了。我希望bot发送一条消息,其中包含指向流的链接。 Discord.js-v12 代码:

    client.on('presenceUpdate', (oldMember, newMember) => {
    const channel = newMember.guild.channels.cache.find(x => x.name === "test");
    if (!channel) return;
        let oldStreamingStatus = oldMember.presence.activities.type ? oldMember.presence.activities.streaming : false;
        let newStreamingStatus = newMember.presence.activities.type ? newMember.presence.activities.streaming : false;
  if(oldStreamingStatus == newStreamingStatus){
    return;
  }

  if(newStreamingStatus){
    if(message.member.roles.cache.find(r => r.name === "test")) {
        channel.send(`${newMember.user}, is live URL: ${newMember.presence.activities.url} ${newMember.presence.activities.name}`);
    return; 
  }else
  return;
}});

2 个答案:

答案 0 :(得分:3)

oldMembernewMember的类型为Presence,因此您无需访问.presence属性即可获取成员的活动,只需使用{{1 }}。对于oldMember.activitiesnewMember.presence.activities.url,请执行以下操作:

newMember.presence.activities.name

但是,无论哪种方式,您的代码都将无法工作,因为newMember.activities.find(activity => activity.type === 'STREAMING').name newMember.activities.find(activity => activity.type === 'STREAMING').url 返回了Activity的数组,因此您可以通过两种方式进行操作。

(推荐):寻找类型为activities的{​​{1}}:

Activity

获取第一个STREAMING

let oldStreamingStatus = oldMember.activities.find(activity => activity.type === 'STREAMING') ? true : false;
let newStreamingStatus = newMember.activities.find(activity => activity.type === 'STREAMING') ? true : false;

答案 1 :(得分:0)

问题是,oldMember和newMember已经存在,因此请删除.presence