2
votes

my problem is that when user go live and have live status on discord, in console i get error "
TypeError: Cannot read property 'activities' of undefined" and bot is crashing. I expect to bot send a message with link to stream. Discord.js - v12 Code:

    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

2 Answers

3
votes

oldMember and newMember are of type Presence, so you do not need to access the .presence property to get the member's activities, you can simply use oldMember.activities. For newMember.presence.activities.url & newMember.presence.activities.name do this:

newMember.activities.find(activity => activity.type === 'STREAMING').name
newMember.activities.find(activity => activity.type === 'STREAMING').url

Either way however, your code won't work, since activities returns an array of Activity so you can go about doing this in 2 ways.

(Recommended) Looking for Activity of type STREAMING:

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

Getting the first Activity:

let oldStreamingStatus = oldMember.activities[0].type === 'STREAMING' ? true : false
let newStreamingStatus = newMember.activities[0].type === 'STREAMING' ? true : false
0
votes

The problem is, that oldMember and newMember are already presences, so remove .presence.