Bot discord set activity

Hi i have dev a little code for setup a activity for my bot so he dosen’t realy work how i want :

client.on("ready", () => {
var word = ["'the world', { type: 'WATCHING'}", "'with a cat', {type: 'PLAYING'}"];
let i = 0;
client.user.setActivity(word[0]);
i++;
setInterval(() => {
 if (i >= word.length + 1) {
   i = 0;
 }
client.user.setActivity(word[i]);
i++;
}, 1000 * 15); // 30 = 30s || 1000 * 1 = 1s
});

the problem is on the bot he display “the world’, { type: ‘WATCHING’}” and not just “the world” and somme times he display nothing and the last thing :
if i add “client.user.setStatus(“dnd”);” nothing change on the status

Thanks a lot for your answers and sorry for my bad english.

@Hrodvitnir-Fenrir, it’s actually the way you’re array is structured.

var word = ["'the world', { type: 'WATCHING'}", "'with a cat', {type: 'PLAYING'}"];

Here, 'the world', { type: 'WATCHING'} is treated as on item of the array because you’ve surrounded both of these in double quotes, which results in both the items 'the world', { type: 'WATCHING'} being treated as one item.

the problem if i write this :
var word = ['the world', { type: 'WATCHING'} , 'with a cat', {type: 'PLAYING'}];

witohout the double quotes he keep the playing he don’t change to watching so somme times is “playing the world”
im sorry i’m verry bad with JS
(and same somme time he show nothing, and the dnd whas fixed)

Well, the problem is that { type: 'WATCHING' } is an object. In the above code, enclosing it in quotes, it will become a string.

Here is a possible solution:

  1. Make array of the presence action alone.
var word = ["the world", "with a cat"];
  1. Check what word[i] is and make presence accordingly.
if (word[i] === "the world") {
   let action = "{ type: 'WATCHING' }";
   client.user.setActivity(args[i], JSON.parse(action));
} else if (word[i] === "with a cat") {
   let action = "{ type: 'PLAYING' }";
   client.user.setActivity(args[i], JSON.parse(action));
} else {
   // something else 
}

the tuto i see on guithub set the WATCHING in quotes :

but yeah a see what you whant to say,
I have set double quotes because I mean the problem whas the “comma”(sorry i don’t know if is this word for say “,”) : var word = ['the world', { type: 'WATCHING'} , 'with a cat', {type: 'PLAYING'}]; because you have 3 : 2 in the var and the midle comma for separate the 2 differents variables

@Hrodvitnir-Fenrir, see

And yes, that is what you call a ‘comma’! :slight_smile:

Okay thx i gonna try (if i don’t do errors ^^")

2 Likes

@Hrodvitnir-Fenrir, I made a small edit to the code, make sure to take a look.

I’m realy sorry to disturb you, but i’m verry bad with js i have add your code in mine :
Capture
i have a error at else :
else if (word[i] === “with a cat”) {
^^^^
SyntaxError: Unexpected token else
at Module._compile (internal/modules/cjs/loader.js:703:23)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:770:10)
at Module.load (internal/modules/cjs/loader.js:628:32)
at Function.Module._load (internal/modules/cjs/loader.js:555:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:826:10)
at internal/main/run_main_module.js:17:11

this is clearly not m’y level in development is pretty hard for me

Hey @Hrodvitnir-Fenrir,

It’s just a small error, nothing to worry about, it’s because you missed a } before else.

Here’s the updated code:

if (word[i] === "the world") {
   let action = "{ type: 'WATCHING' }";
   client.user.setActivity(args[i], JSON.parse(action));
} else if (word[i] === "with a cat") {
   let action = "{ type: 'PLAYING' }";
   client.user.setActivity(args[i], JSON.parse(action));
} else {
   // something else 
}

This is the final code:

client.on("ready", () => {
  var word = ["the world", "with a cat"];
  let i = 0;
  client.user.setActivity(word[0]);
  i++;
  setInterval(() => {
    if (i >= word.length + 1) {
      i = 0;
    }
    if (word[i] === "the world") {
      let action = "{ type: 'WATCHING' }";
      client.user.setActivity(args[i], JSON.parse(action));
    } else if (word[i] === "with a cat") {
      let action = "{ type: 'PLAYING' }";
      client.user.setActivity(args[i], JSON.parse(action));
    } else {
      // something else
    }
    i++;
  }, 1000 * 15); // 30 = 30s || 1000 * 1 = 1s
});

Okay so a lot of error :cold_sweat: :
last line : “}, 1000 * 15); // 30 = 30s || 1000 * 1 = 1s” need one more } after the first (my bad is on my code not on your but i have copy paste your code so is okay)
the two args need to be remplaced by “word” ?

after do this two thing :
{ type: ‘PLAYING’ }
^
SyntaxError: Unexpected token t in JSON at position 2
at JSON.parse (anonymous)
at Timeout._onTimeout (/app/server.js:37:42)
at listOnTimeout (internal/timers.js:531:17)
at processTimers (internal/timers.js:475:7)

I have got a static( it will stay like this until you add new or change this)

bot.on(‘ready’ , async () => {

console.log(`${bot.user.username} is online!`)

bot.user.setActivity("the world" , {type: "WATCHING"})
bot.user.setActivity("with cat" , {type: "PLAYING"})