Bot status changes temporary

Viewed 305

I have PresenceUpdate Set so when a specific user streams the bot status updates and it announces in the channel. It did announce and it worked fine but the bot status seems to change for only ~20 seconds then it would go back to its default.

I've tried fixing it using:

  • For Loop
  • While Loop
  • Recursion function

Can anyone help me out and fix this? Here is my code:

client.on("presenceUpdate", (oldPresence, newPresence) => {
  let announcement = "Announcement Message";
  if (!newPresence.activities) return false;
  newPresence.activities.forEach(activity => {
      if (activity.type == "STREAMING") {
        if(newPresence.user.id == "ID OF USER STREAMING") {
          console.log(`${newPresence.user.tag} is streaming at ${activity.url}.`);
          client.channels.cache.get("My Channel ID").send(announcement);
          client.user.setActivity("to the server", { type: "STREAMING",  url: "Twitch Link"});
       };
      };
  });
});

EDIT: Okay so i managed to fix it by removing the part where if they r not streaming the status returns to default. HOWEVER, now it is stuck on streaming even when the user is no longer streaming. How do i solve this? The code has been also updated.

1 Answers

You could store the streaming user's id somewhere, and then check whenever the presence is update if it is this same user.

Here is an example :

if (client.streamingUserID !== undefined && client.streamingUserID !== newPresence.user.id) return;
newPresence.activities.forEach(activity => {
    if (activity.type == "STREAMING") {
        if(newPresence.user.id == "ID OF USER STREAMING") { 
            console.log(`${newPresence.user.tag} is streaming at ${activity.url}.`);
            client.channels.cache.get("My Channel ID").send(announcement);
            client.user.setActivity("to the server", { type: "STREAMING", url: "Twitch Link"});
            client.streamingUserID = newPresence.user.id;
        }; 
    } else {
        // set back activity to default and
        delete client.streamingUserID;
    }
});

Related