Bot status occasionally reverts back to initial status from the status set in ClientReady

Im using DJS with sharding. I initialize the client with a status of "Starting" and then in ClientReady I basically set the status to something else ("bla" in this case).
const client = new DiscordClient({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.DirectMessages,
],
presence: {
status: "dnd",
activities: [{ name: "Starting...", type: ActivityType.Playing }],
},
});

client.on(Events.ClientReady, async() => {
client.user?.setActivity({ name: `Bla`, type: ActivityType.Playing });
});

// login and stuff
const client = new DiscordClient({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.DirectMessages,
],
presence: {
status: "dnd",
activities: [{ name: "Starting...", type: ActivityType.Playing }],
},
});

client.on(Events.ClientReady, async() => {
client.user?.setActivity({ name: `Bla`, type: ActivityType.Playing });
});

// login and stuff
so pretty straight forward stuff. However: if the bot is unattended for some time without restart, sometimes the activity resets back to "Starting..." and remains stuck there. I assume that happens when some connection times our for a bit (shard?) but I'm not sure. Is there a way to "catch" that and set the status again? Im not sure if its a sharding issue but would Events.ShardResume or something work? Is that a common issue? --- npm list discord.js: discord.js@14.14.1 node -v: v21.0.0
5 Replies
d.js toolkit
d.js toolkit8mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs8mo ago
:interface: ClientOptions Options for a client.
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
Shadow
Shadow7mo ago
Nah I don't think an interval is a good solution. Since somehow the client options are re-set under a certain condition without calling ClientReady again, I assume the shard is re-initialized. Which means I should be able to set the status again by catching the ShardResume event. but since I can't really test that I thought I'd ask here first the presence isn't static. ill give shardReady a try. whats the difference between that and shardResume? alright, I see. thanks! :) so, shardReady doesnt really work in my case because it fires with ClientReady which is why I do it with a preiodic check like @gwapes suggested now
const client = new DiscordClient({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.DirectMessages,
],
presence: {
status: "dnd",
activities: [{ name: "Starting...", type: ActivityType.Playing }],
},
});

// ...

client.on(Events.ClientReady, async() => {
const guildCount = await client.guilds.fetch().then(guilds => guilds.size);

await registerCommands(client)
.then(() => client.on(Events.InteractionCreate, async interaction => interactionCreateHandler(interaction)));

client.user?.setActivity({ name: `Working on ${guildCount} servers!`, type: ActivityType.Playing });

let lastGuildCount = guildCount;
setInterval(async() => {
const newGuildCount = await client.guilds.fetch().then(guilds => guilds.size);
const statusHasReset = client.user?.presence.activities[0].name === "Starting...";

if (newGuildCount !== lastGuildCount || statusHasReset){
lastGuildCount = newGuildCount;
client.user?.setActivity({ name: `Working on ${newGuildCount} servers!`, type: ActivityType.Playing });
}
}, 5 * 60 * 1000);

client.user?.setStatus("online");
});
const client = new DiscordClient({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.DirectMessages,
],
presence: {
status: "dnd",
activities: [{ name: "Starting...", type: ActivityType.Playing }],
},
});

// ...

client.on(Events.ClientReady, async() => {
const guildCount = await client.guilds.fetch().then(guilds => guilds.size);

await registerCommands(client)
.then(() => client.on(Events.InteractionCreate, async interaction => interactionCreateHandler(interaction)));

client.user?.setActivity({ name: `Working on ${guildCount} servers!`, type: ActivityType.Playing });

let lastGuildCount = guildCount;
setInterval(async() => {
const newGuildCount = await client.guilds.fetch().then(guilds => guilds.size);
const statusHasReset = client.user?.presence.activities[0].name === "Starting...";

if (newGuildCount !== lastGuildCount || statusHasReset){
lastGuildCount = newGuildCount;
client.user?.setActivity({ name: `Working on ${newGuildCount} servers!`, type: ActivityType.Playing });
}
}, 5 * 60 * 1000);

client.user?.setStatus("online");
});
Want results from more Discord servers?
Add your server