Skip to content
Advertisement

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" });
});

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement