0

I wanted to create an updating status with all kind of activity on my BOT. This is the code of what I have done so far:

const activities = [
        {
            type : "PLAYING",
            activity : "with the ball"
        },
        {
            type : "WATCHING",
            activity : "your soul"
        }
    ];

    for (var i = 0; i < activities.length; i++) {
        setInterval(() => client.user.setActivity(`${activities[i].activity}`, { type: `${activities[i].type}` }));
    };

When I try to start the BOT, the terminal shows

TypeError: Cannot read property 'activity' of undefined

Probably the code is totally wrong, but I'm not very good at javascript. Any suggestions?

BasZ
  • 37
  • 5
  • you have not pasted all of the code – jo3rn Feb 17 '21 at 12:36
  • if you try to just log activities[i].activity inside your for loop, what do you get? – TomBonynge Feb 17 '21 at 12:41
  • Ops, should be ok now. Anyways, I only put the part of code that I'm working on – BasZ Feb 17 '21 at 12:43
  • 1
    Change `var` to `let`, looking for a post now to explain why this happens – Reyno Feb 17 '21 at 12:43
  • If I try to log that i get "with the ball" and "your soul" – BasZ Feb 17 '21 at 12:46
  • Changing var to let seems to set the activity only to "Playing with the ball" – BasZ Feb 17 '21 at 12:49
  • Are you sure you need a `setInterval` for each item in your array? Don't you mean `setTimeout`. Don't forget to also scale the delay with your `i` variable. so `setInterval(func, delay)` should be `setInterval(func, i * delay)` – Reyno Feb 17 '21 at 12:56

1 Answers1

1

the setInterval will force an async operation, but the stack value works only in the loop context, because it's a pointer. You can create another variable first, javascripts Garbage Collector will notice "oh i can't clean up this, i need it later", so javascript still have this value in his stack

for (var i = 0; i < activities.length; i++) {
    var putonstack = activities[i];
    setInterval(() => client.user.setActivity(`${putonstack.activity}`, { type: `${putonstack.type}` }));
};

you can also complete avoid this by iterate like this:

// for (var i = 0, value; value = activities[i]; i++)
for (var value of activities) {
    setInterval(() => client.user.setActivity(`${value.activity}`, { type: `${value.type}` }));
};

And to solve what you asked for:

const activities = [
      {
          type : "PLAYING",
          activity : "with the ball"
      },
      {
          type : "WATCHING",
          activity : "your soul"
      }
  ];
  
  const timeoutForNms = 10000; // 10 seocnds
  let currentActivity = 0;
  setInterval(() => {
  console.log('set activity to %s type to %s', activities[currentActivity].activity, activities[currentActivity].type);
  client.user.setActivity(`${activities[currentActivity].activity}`, { type: `${activities[currentActivity].type}` });
  currentActivity++;
  if (currentActivity === activities.length) {
      currentActivity = 0;
  }
  }, timeoutForNms);
Scriptkiddy1337
  • 792
  • 4
  • 9