How to fix null error when setting Discord bot status

I’m working on a Discord bot with discord.js v12 and running into an issue when trying to update the bot’s status. Every time I try to set the activity, I get an error saying the property is null.

Here’s what I’m using:

bot.user.setActivity('Type !commands for help', { type: 'PLAYING' });

The error message shows: TypeError: Cannot read property 'setActivity' of null.

I’m not sure why this is happening. The bot seems to be running fine otherwise, but I can’t get the custom status to work. Has anyone else experienced this problem before?

Check your login sequence too. I’ve seen this happen when there’s a timing issue between bot.login() and trying to set the activity. Make sure you’re calling bot.login() with your token before any status updates. Also, if you’re testing locally and the bot token got regenerated or invalidated, login will fail but sometimes won’t throw obvious errors. Try wrapping your login in a try-catch block and add some debugging to confirm the bot actually authenticates. The ready event approach mentioned above is definitely right, but auth issues can cause the same symptoms.

Yeah, Grace is right about the ready event, but double-check your bot token too. Login can fail silently and leave bot.user as null even when ready fires. Add some logging to see if ready’s actually triggering.

This happens because you’re trying to set the activity before the bot’s logged in. bot.user is null until the client’s ready. I ran into this exact issue when I started with discord.js v12. You need to put your setActivity call inside the ready event listener:

bot.on('ready', () => {
    bot.user.setActivity('Type !commands for help', { type: 'PLAYING' });
    console.log('Bot is ready and status set!');
});

The ready event fires once the bot’s logged in and cached all the data it needs. Without this, you’re trying to modify properties that don’t exist yet. Fixed it for me completely.