Deepak Sharma
Deepak Sharma

Reputation: 21

My discord bot not updating status when it joins a new server

My bot not updating when someone invites my bot to their/another server. I have to restart the code, then it is working. I want that my bot updated when someone invites in status.

My current code is:

PREFIX = <

client.on("ready", () => {
  console.log(`${client.user.username} ready!`);
  client.user.setActivity(`${PREFIX}help | ${PREFIX}play  ${client.guilds.cache.size} servers `, { type: "LISTENING" });
});

Upvotes: 1

Views: 954

Answers (2)

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23160

It seems you want to update the bot's status whenever your bot joins a new server. Instead of using an unnecessary setInterval to check the cached guild size every X seconds, you could use the guildCreate event.

It emits whenever the client joins a guild, so whenever it fires, you can update the activity inside its callback:

// emitted when the client becomes ready to start working
client.on('ready', () => {
  console.log(`${client.user.username} is ready!`);

  client.user.setActivity(
    `${PREFIX}help | ${PREFIX}play | ${client.guilds.cache.size} servers`,
    { type: 'LISTENING' },
  );
});

// emitted whenever the client joins a guild
client.on('guildCreate', (guild) => {
  console.log(`${client.user.username} joined the ${guild.name} server`);

  client.user.setActivity(
    `${PREFIX}help | ${PREFIX}play | ${client.guilds.cache.size} servers`,
    { type: 'LISTENING' },
  );
});

PS: your client needs the GUILDS intent to be enabled

Upvotes: 2

Coddie Prince
Coddie Prince

Reputation: 76

You must update your status every X seconds. Your code that you are using right now just updated when the bot is ready.

To do this you must set a Interval to run the code every X seconds.

client.on("ready", () => {

    console.log(`${client.user.username} ready!`);

    setInterval(() => {
        client.user.setActivity(`${PREFIX}help | ${PREFIX}play  ${client.guilds.cache.size} servers `, { type: "LISTENING" });
    }, 15000);

})

The 15000 is the ms (milisecond) that you want your bot to update the status. (1000ms is equal to 1 Second.)

Due to the Discord API rules, to prevent spam, you can only update your bot status minimal every 15 seconds

Upvotes: 1

Related Questions