PenguinCraft
PenguinCraft

Reputation: 3

Discord.js Bot Status

No, I am not asking how to set my bot's status. I am asking how to make my bot's status change every 5-7 seconds to another status. How would I make this:

client.user.setActivity(`dsc.gg/ultraa | ?help | ${client.guilds.cache.size} servers`, { type: 'WATCHING' });

change every few seconds to be like "It's frosty out there", and "420 servers", and "?help | dsc.gg/ultraa". How would I make those statuses rotate between each other?

Upvotes: 0

Views: 4509

Answers (2)

Technical Ehan
Technical Ehan

Reputation: 1

client.user.setPresence({ activities: [{ name: `Status`, type: `PLAYING` }] });

For Simple Status

Upvotes: 0

Hibiscus
Hibiscus

Reputation: 736

Note: This code is for Discord.js V13.3.1


For a rotating status, as you would describe, you would do this by placing some code in the ready event that changes the status after a set period of time. This will require the util package which can be installed using:

  • npm: npm install util
  • yarn: yarn install util

Once you have the package installed, you will create a function called wait. It's a simple way to wait without blocking the rest of the script.

const wait = require('util').promisify(setTimeout);

This function will serve our primary purpose, making the delay between switching the statuses. Then, we will use setInterval to continuously loop between the statuses.

<Client>.on('ready', async () => {
  // ...
  setInterval(async () => {
    client.user.setPresence({ activities: [{ name: `Status1`, type: `PLAYING` }] });
    await wait(5000); // Wait 5 seconds
    client.user.setPresence({ activities: [{ name: `Status2`, type: `PLAYING` }] });
    await wait(5000); // Wait 5 seconds
  });
});

As you can see, there are two line that repeat themselves. That is: <Client>.user.setPresence(...) and await wait(5000). The wait function will block the status from updating itself too early. You can edit the amount of time set by converting the seconds to milliseconds (5 seconds becomes 5000). The other portion sets the bot's status. The type shows what the bot is doing. The valid values for this are found in the docs. You can simply copy and paste the two lines and add more statuses as needed.

Upvotes: 1

Related Questions