Get a GuildMember's nickname and status type in Discord.js v12 - discord.js

The title's pretty much everything. I have tried .addField(`**Nickname (If Applicable):**`, `${usr.nickname}`) but it says undefined in the embed... For the presence though, I've tried .addField(`**Status:**`, `${usr.presence}`). That gives me [object Object] in the embed. Can anyone help?

First of all, you're not able to get a GuildMember object's status but would have to first turn it into a user object.
This can be easily done as follows:
const memberObject = <guild>.members.cache.get('place id here') // Gives you the member object
// or alternatively you could search for him using his name using the .find() function
const userObject = memberObject.user // Gives you the user object of the member
From there on, we could simply check the user's nickname and status using:
console.log(userObject.presence.status)
console.log(memberObject.nickname)
Hopefully this helps!

Related

How to get bot to reply to user with a (#)ping

I am trying to get a bot to respond to people with a ping in the message, for example: "#user", but everything I've tried have given me not a function error or undefined error. All the stuff I can find about it is either outdated for discord.js v14 or it is for discord.py
Here is my code:
client.on("messageCreate", (message) => {
if (message.content.startsWith("test")) {
const user = message.author.userId();
message.channel.reply(`Hello <#${user}>`)
}
});
I've also attempted variations of the .userId() part - such as .tag, .user.id, and .username but all of them have come back with some sort of undefined error. I know it says userId is a snowflake on discord.js but I am unsure on how to use that for I am fairly new to javascript and discord.js. Also, please know that I am using Replit to host the bot and have discord.js#14.5.0 installed.
So, there are a couple issues here.
Issue 1
message.author.userId() is not a function. When trying to get a user's ID, you want to know what each property is returning.
message -> returns the Message object, which contains the data for the message.
message.author -> returns the User object, which contains the data for the user profile.
message.member -> returns the Member object, which contains the data for the guild member profile.
And so on.
In this case, you're going to want to get the User object: being message.author. You've already figured this out.
Now, the User Object has it's own set of properties (which you can find in the documentation at https://discord.js.org/).
The property that you are looking for is: .id, or in your use-case, message.author.id.
Your other attempts, message.author.tag returns the tag, message.author.user.id will throw an error, and message.author.username returns the user's username.
Issue 2
Your second issue is the .reply() method that you're using. The Channel Object doesn't have a .reply() method, but the message does.
So, how you would write this code:
client.on("messageCreate", async(message) => {
if(message.content.toLowerCase().startsWith("test")) {
// Note that I added the .toLowerCase() method to ensure you can do tEST and it still works!
message.reply({ content: `Hello, ${message.author}!` });
}
});
Also, a cool feature that discord.js has is that you can supply the User or Member Object to the content of a message, and it will automatically mention the desired person.
Hope this helps!

Is it possible to populate member collection from a message in Discord JS?

I'm trying to check if the message author has a specific role or not. The most common way I came across is the message.mentions.members.first();. But this would require the author to be mentioned in the message. So,
Is there a way to populate member collection and find if the message author has the role or not without mentioning him?
My current code looks like this:
//Some auth and import stuff here
let member = message.author;
//Some pre-validation stuff here
if(member.cache.roles.some(m => m.id === '608238654095360010')){
message.channel.send("You are already verified!");
return;
}
Before this I tried member.roles.cache.some but later found that discordJS got an update and became like above.
But I still get undefined error.
Edit : I tried using message.member.roles.some but that returns
TypeError: message.member.roles.some is not a function
So, I was using DiscordJS v12 which has quite a few updates.
Instead of using message.member.roles.some
I had to use , message.member.roles.cache.some(role => role.id === '608238654095360010')

Discord.js User DM from id

Im trying to dm a user but I only have there id. This is the code im using:
const member = bot.users.cache.find(ID)
member.send('TEXT')
it isn't working. Does anyone know why?
If the user is not cached you can do this.
const member = bot.users.fetch('user-id',false,true)
That helps to fetch member data directly from API. more at here
Like somebody else has stated, the user probably isn't cached. There isn't anything you can do about this as far as I can tell.
If you want to avoid the undefined error your having, and to catch any errors that may occur when sending the message:
const member = bot.users.cache.find(ID);
if (member) member.send('TEXT').catch(console.error);

Discord.js, message.guild.owner returns null

I'm coding a server-info command for my discord bot, and I want to get the owner username or tag from the actual guild. I found a way to do it in discord js 11, but it's not working anymore under 12th version :
const guild = client.guilds.get(message.guild.id);
message.channel.send(message.guild.member(guild.owner) ? guild.owner.toString() : guild.owner.user.tag);
// if the user is in that guild it will mention him, otherwise it will use .tag
So in discord js 12, client.guilds.get isn't a function, and guild.owner returns null.
message.guild.owner.user.usernameis also returning Cannot read property 'user' of null.
I took a look at the documentation, and message.guild.owner seems to be a real property (https://discord.js.org/#/docs/main/stable/class/Guild?scrollTo=owner). So I don't know why it's returning null.
I'd recommend that you first get the guild and make sure that it's available before trying to modify or access it. Therefore, your bot won't hit any errors. Additionally, as far as I know, guild.owner returns null but there is a way around this. You can get the guild owner ID through guild.ownerID and fetch the member as a guildMember. Here is the code:
const guild = message.guild; // Gets guild from the Message object
if(!guild.available) return; // Stops if unavailable
await message.guild.members.fetch(message.guild.ownerID) // Fetches owner
.then(guildMember => sOwner = guildMember) // sOwner is the owner
message.channel.send(guild.member(sOwner) ? sOwner.toString() : guild.owner.user.tag);
This is how you would get the owner's tag when you have the guild. This also does not rely on the cache, so if your bot restarts or the cache is cleared, it will work regardless.
let ownerTag = undefined;
<client>.users.fetch(<guild>.ownerID).then(user => ownerTag = user.tag);
.fetch() returns a promise, which is why I declared a variable and then assigned the value to it.
Make sure you enable these intents in your discord developer portal so your bot could access your server
After that, you could access your user owner tag.

Is there a way to add a role to someone without mentioning/pinging them? [discord.js]

Pretty much the title. Every time I try adding a role to someone using this:
const member = message.author;
member.roles.add('732727208774205460');
I end up with this TypeError:
TypeError: Cannot read property 'add' of undefined
However, if I use it like this:
const member = message.mentions.members.first();
member.roles.add('732727208774205460');
It works completely fine. Problem is, that only works if the person I'm adding a role to was mentioned/pinged by the user. I'm trying to add a role to the user himself where the user doesn't ping anyone (for censorship, mainly). Is there a way I could do this without getting the TypeError?
You are receiving an error because message.author.roles is undefined, so trying to use .add() on it causes an error.
message.author is of type User, which represents a discord user, and is not associated with any particular server.
You're looking instead for message.member which has type GuildMember, which represents a particular user's profile inside of a guild (message.member is just message.author but as a GuildMember instead of a User). It is the GuildMember type that has roles.
Solution:
const member = message.member;
member.roles.add('732727208774205460');
Official Documentation:
User,
GuildMember

Resources