简体   繁体   中英

I am making a status that shows how many guilds my bot's in, but it doesn't update

I am trying to make a status for my Discord bot which shows how many servers my bot's in. I want it to refresh the status every time my bot is added to a new server. Here's my current code:

bot.on('ready', () => {
  console.log(`${bot.user.username} is now ready!`);
  status_list = ["stuff", `${bot.guilds.cache.size} servers`]
  setInterval(() => {
    var index = Math.floor(Math.random() * (status_list.length - 1) + 1);
    bot.user.setActivity(status_list[index], { type: "LISTENING" });
  }, 15000)
});

Any help will be appreciated, thanks!

Instead of relying on an interval, it's better to use the guildCreate and guildDelete events. These are fired every time the bot joins a guild or is removed from a guild, respectively. Take a look at the example code below.

client.on("ready", () => {
    client.user.setActivity("Serving " + client.guilds.cache.size + " servers");
});

client.on("guildCreate", () => {
    // Fired every time the bot is added to a new server
    client.user.setActivity("Serving "+ client.guilds.cache.size +" servers");
});

client.on("guildDelete", () => {
    // Fired every time the bot is removed from a server
    client.user.setActivity("Serving "+ client.guilds.cache.size +" servers");
});

Now if you want to pair this with selecting a random status, you could do the following as well:

const statusMessages = ['First status messages', 'Serving {guildSize} servers', 'Third possible message'];

let chosenMessageIndex = 0;

client.on("ready", () => {
    setInterval(() => {
        setRandomStatus();
    }, 15000);

    setRandomStatus();
});

client.on("guildCreate", () => {
    // Fired every time the bot is added to a new server
    updateStatus();
});

client.on("guildDelete", () => {
    // Fired every time the bot is removed from a server
    updateStatus();
});

function setRandomStatus() {
    chosenMessageIndex = Math.floor(Math.random() * statusMessages.length);

    // Set the random status message. If "guildSize" is in the status,
    // replace it with the actual number of servers the bot is in
    let statusMessage = statusMessages[chosenMessageIndex].replaceAll('{guildSize}', client.guilds.cache.size);

    client.user.setActivity(statusMessage);
}

function updateStatus() {
    // Check if the displayed status contains the number of servers joined.
    // If so, the status needs to be updated.
    if (statusMessages[chosenMessageIndex].includes('{guildSize}') {
        let statusMessage = statusMessages[chosenMessageIndex].replaceAll('{guildSize}', client.guilds.cache.size);

        client.user.setActivity(statusMessage);
    }
}

Your issue is here:

var index = Math.floor(Math.random() * (status_list.length - 1) + 1);

This will declare the same one every time, and that's why it doesn't update

I want it to refresh the status every time my bot is added to a new server.

This is what you missed (unintentionally). What you want to do is, as you've already done, refreshing it in an interval using setInterval() . This will refresh the status every interval whether the number changed or not. You don't have to make sure it changed, as it's a small load on your instance.
Now, try to follow the code below:

bot.on('ready', async () => { // async is recommended as discord.js generally uses async/await.

  // Logs in the console that the bot is ready.
  console.log(`${bot.user.username} is now ready!`);

  // Rather than using an array, which is generally harder to use, manually set the variables instead.
  const status = `stuff on ${bot.guilds.cache.size} servers.`

  // Set the Interval of Refresh.
  setInterval(async () => { // Again, async is recommended, though does not do anything for our current purpose.

    // Set the activity inside of `setInterval()`.
    bot.user.setActivity(status, { type: "LISTENING" });

  }, 15000) // Refreshes every 15000 miliseconds, or 15 seconds.
});

This should do what you need, but as I can see from your code, you tried to make a random status? If so, then follow the one below instead:

bot.on('ready', async () => {
  console.log(`${bot.user.username} is now ready!`);

  // In this one, you will need to make an array. Add as many as you want.
  const status_list = [`stuff on ${bot.guilds.cache.size} servers.`, `stuff on ${bot.channels.cache.size} channels.`];

  // Now randomize a single status from the status_list. With this, you have singled out a random status.
  const status = Math.floor(Math.random() * status_list.length);

  setInterval(async () => {
    bot.user.setActivity(status, { type: "LISTENING" });
  }, 15000)
});

Now, if you want to instead have a constantly changing (not random) status, you can use the following code:

bot.on('ready', async () => {
  console.log(`${bot.user.username} is now ready!`);
  const status_list = [`stuff on ${bot.guilds.cache.size} servers.`, `stuff on ${bot.channels.cache.size} channels.`];

  // Create a new `let` variable, which can be assigned to, say, `count`.
  // We start from 0 so that it doesn't mess up.
  let count = 0;

  setInterval(async () => {

    // Check if the count is already the length of the status_list, if it is, then return to 0 again. This has to be done before the `status` variable has been set.
    if (count === status_list.length + 1) count = 0;

    // Define Status by using the counter
    const status = status_list[count];
   
    // Add the counter by 1 every time the interval passed, which indicates that the status should be changed.
    count = count + 1;

    bot.user.setActivity(status, { type: "LISTENING" });
  }, 15000)
});

I think it should be something like this:

bot.on('ready', () => {
  console.log(`${bot.user.username} is now ready!`);
  setInterval({
    bot.user.setPresence({
      activity: {
        name: `Running in ${bot.guilds.cache.size} servers.`,
        type: "LISTENING"
      }
    });
  }, 15000);
});

This should show how many servers the bot is in every interval.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM