How to check server boost status in a Discord bot?

I’m building a Discord bot using Node.js and I’m stuck on a problem. I want to check if the server where the bot is used has any boosts before running a command. But I can’t figure out how to get the server’s boost level.

Every time I try, I get this error:

TypeError: Cannot read properties of null (reading 'premiumSubscriptionCount')

Here’s a bit of my code:

bot.on('messageReceived', async msg => {
  if (!msg.isCommand()) return;
  await msg.holdResponse();

  const { cmdType, parameters, serverID } = msg;
  const userIdentifier = msg.author.id;
  console.log(`Boost status: ${msg.server.boostCount}`);

I’ve double-checked that I have the right permissions set up. I’ve also looked at the docs and tried other methods like BoostTier, but I always get the same error.

Any ideas on what I’m doing wrong or how to fix this? Thanks!

It seems the issue might be with how you’re accessing the server object. In Discord.js, you typically use ‘guild’ instead of ‘server’. Try modifying your code like this:

const boostCount = msg.guild?.premiumSubscriptionCount ?? 0;
console.log(`Boost status: ${boostCount}`);

This approach uses optional chaining and nullish coalescing to safely access the boost count. If msg.guild is undefined, it’ll default to 0. Also, ensure your bot has the ‘GUILD_PRESENCES’ intent enabled in your bot’s Discord Developer Portal settings and in your client initialization code. This intent is required to access certain guild-related information.

Hey there! I ran into a similar issue when working on my Discord bot. Here’s what worked for me:

Make sure you’re using the latest version of discord.js. Older versions might not have the premium subscription properties available.

Also, check if you’ve properly initialized your client with the necessary intents. You’ll need the GUILD_PRESENCES intent for sure. Here’s how I set it up:

const client = new Discord.Client({ intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_PRESENCES] });

After that, you should be able to access the boost count like this:

const boostCount = message.guild.premiumSubscriptionCount;
console.log(`This server has ${boostCount} boosts`);

Hope this helps! Let me know if you need any more clarification.

i think msg.guild might be null. try guarding against it using optional chaining:

const boost = msg.guild?.premiumSubscriptionCount || 0;

double-check ur bot is properly connected to the server.