Home > OS >  Finding a certain users status in discord.js
Finding a certain users status in discord.js

Time:11-08

Okay so I am trying to make a discord bot that detects when a certain user is online in my server. But every time I try to get the users presence status it always says "const usersStatus = user.presence.status; TypeError: Cannot read properties of undefined (reading 'status')". How do I get the presence status of a specific user? Here's my code so far

const { Client, Intents } = require('discord.js');
const { token} = require('./config.json');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_PRESENCES] });

const user = client.users.fetch('Users ID');
const usersStatus = user.presence.status;

client.once('ready', () => {
    console.log("should be online");
    console.log(usersStatus);
})

//place at end of file
client.login(token);

CodePudding user response:

client.users.fetch returns a Promise. You will need to await it and get the presence from there

(async () => { //await needs async
const user = await client.users.fetch('Users ID');
const usersStatus = user.presence.status
})()

Additionally, you won't get any users if the client is not ready. You should make sure this is in your ready event so the client fetches the user properly.

In discord.js v13 Users don't have presences. This is only on GuildMembers now. I believe this is for security reasons - like bots can only DM members they are in a guild with. You can get any guild that this user is in, then get the member from there where you will then get their status

const member = await guild.members.fetch('Users ID'); //remember you still need async
const membersStatus = member.presence.status

CodePudding user response:

In Discord, offline members don't have any presence, so the presence is null, hence you get the error.

So if presence is null, either the User defined isn't a user or they are offline.

Secondly, you will need to resolve the promise by adding await

Usually, I would do this to get to know their status.

let user = await client.users.fetch("ID")
console.log(`${user.tag}'s presence status: ${user.presence?.status ?? "offline"}`)

This will log for example: Ashish#0540's presence status: dnd if the person is on do not disturb, or Ashish#0540's presence status: offline

  • Related