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 = <
JavaScript
x
5
1
client.on("ready", () => {
2
console.log(`${client.user.username} ready!`);
3
client.user.setActivity(`${PREFIX}help | ${PREFIX}play ${client.guilds.cache.size} servers `, { type: "LISTENING" });
4
});
5
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:
JavaScript
1
20
20
1
// emitted when the client becomes ready to start working
2
client.on('ready', () => {
3
console.log(`${client.user.username} is ready!`);
4
5
client.user.setActivity(
6
`${PREFIX}help | ${PREFIX}play | ${client.guilds.cache.size} servers`,
7
{ type: 'LISTENING' },
8
);
9
});
10
11
// emitted whenever the client joins a guild
12
client.on('guildCreate', (guild) => {
13
console.log(`${client.user.username} joined the ${guild.name} server`);
14
15
client.user.setActivity(
16
`${PREFIX}help | ${PREFIX}play | ${client.guilds.cache.size} servers`,
17
{ type: 'LISTENING' },
18
);
19
});
20
PS: your client needs the GUILDS
intent to be enabled