Search code examples
node.jsdiscorddiscord.js

Cannot get user status in Discord.JS V14


I was trying to get the status of a specific user using DiscordJS

I tried this:

client.on("ready", async (client) => {
  console.log(
    chalk.bgGreen("CLIENT"),
    chalk.cyanBright(`${client.user.displayName} is ready`)
  );
  const guildId = "1151374436340088852";
  const memberId = "1098103731972747425";
  const guild = client.guilds.cache.get(guildId);
  const member = guild.members.cache.get(memberId);

  const status = member.presence.status;
  sMap.set("status", status);
});

But it is returning:

  const status = member.presence.status;
                                 ^

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

How can I actually get the status?


Solution

  • According to docs, presence can be null. You should handle this case accordingly. Docs: https://discordjs.guide/additional-info/changes-in-v13.html#guildmember-presence

    You can set some default value if it is null. For example:

    const status = member.presence?.status ?? 'offline';