Home > Software engineering >  How can set a shuffle status with array in discord.js (javascript)
How can set a shuffle status with array in discord.js (javascript)

Time:06-09

I want to set my discord bot's activity shuffle messages that i have to get from an array. I've tested this code but it will only shows the last one in array or just first and last sometimes :

    const array = [
  {
    id: '08933438391',
    type: 'ejx1'
  },
  {
    id: '12361425430',
    type: 'ejx3'
  },
  {
    id: '63746455430',
    type: 'ejx2'
  },
  {
    id: '83972535430',
    type: 'ejx4'
  }
]
setInterval(() => {
const aa = [
     { type: 'PLAYING', message: array.type }
];
const presence = aa[Math.floor(Math.random() * aa.length)];
client.user.setActivity(presence.message, { type: presence.type });
}, 10000);

And this is my array :

    [
  {
    id: '08933438391',
    type: 'ejx1'
  },
  {
    id: '12361425430',
    type: 'ejx3'
  },
  {
    id: '63746455430',
    type: 'ejx2'
  },
  {
    id: '83972535430',
    type: 'ejx4'
  }
]

Thank you for your helps.

CodePudding user response:

There are two ways to do this: either pick a random element from an array at every interval or loop through all of them and once all the elements are done, then start again:

  • Method 1: To do this all you have to add is a random element generator in the setInterval() function to pick a random element from the array, then you can just apply it by using this:
setInterval(() => {
    const randomElement = array[Math.floor(Math.random() * array.length)];
    const aa = [
        { 
            type: 'PLAYING', 
            message: randomElement.type 
        }
    ];
    client.user.setActivity(aa.message, { type: aa.type });
}, 10000);
  • Method 2: For this method, you have to keep a counter outside of the setInterval() function and then increment it every time the function runs. Once the counter reaches a certain number, then reset it back to 0. An example:
let counter = 0
setInterval(() => {
    const aa = [
        { 
            type: 'PLAYING', 
            message: array[counter].type 
        }
    ];
    client.user.setActivity(aa.message, { type: aa.type });
    counter  ;
    counter = counter === array.length - 1 ? 0 : counter
}, 10000);

CodePudding user response:

try this code:

  {
    id: '08933438391',
    type: 'ejx1'
  },
  {
    id: '12361425430',
    type: 'ejx3'
  },
  {
    id: '63746455430',
    type: 'ejx2'
  },
  {
    id: '83972535430',
    type: 'ejx4'
  }
]
setInterval(() => {
  const aa = [
    { type: 'PLAYING', message: messageArray[0].type }
  ]
  const presence = messageArray[Math.floor(Math.random() * messageArray.length)]
  client.user.setActivity(presence.message, { type: presence.type })
}, 10000)

CodePudding user response:

you don't have to use aa variable just do it like this:

 setInterval(() => {
    const presence = array[Math.floor(Math.random() * array.length)];
    client.user.setActivity(presence.type, { type: "PLAYING });
    }, 10000);

and don't forget this will return a random type, and you may take 4 ejx1 in a row

  • Related