How to show roles of user discord.js / userinfo command
User.roles
is undefined
because that property doesn't exist: try using GuildMember.roles
instead:
let member = message.mentions.members.first() || message.member,
user = member.user;
let embed = new Discord.RichEmbed()
// ... all the other stuff ...
.addField('Roles:', member.roles.map(r => `${r}`).join(' | '), true)
The other properties will still use user
, but .roles
will be related to the GuildMember.
discord.js userinfo command show all roles
You're getting that error because user
does not have a property called member
. If you want to get the mentioned member, use this:
let member = message.mentions.members.first()
I am trying to show roles in my !!userinfo command but I get errors
I believe the problem lies with how you assign a value to the variable member
. Adding to that, I think you have some redundant code since you have a variable member
and a variable user
which you give a value with the same code.
Below you can find your code which I've rewritten. Give it a go and let me know what the result is.
module.exports.run = async (bot, message, args) => {
let guildMember;
if (message.mentions.members.first()) {
guildMember = message.mentions.members.first();
} else {
guildMember = message.member;
}
// We need the User object aswell for different properties
const user = guildMember.user;
let embed = new Discord.RichEmbed()
.setAuthor(user.username)
.setDescription("Users Info", true)
.setColor("#64FF00", true)
.addField("Full Username:", `${user.username}${user.discriminator}`, true)
.addField("ID:", user.id, true)
.addField("Created at:", user.createdAt, true)
.addField("Status:", `${user.presence.status}`, true)
.addField("Game:", `${user.presence.game}`, true)
.addField("Roles", guildMember.roles.map(r => `${r}`).join('|'), true);
message.channel.send(embed);
}
Discord.js Userinfo command TypeError: Cannot read property 'roles' of null
I beleive your problem is that you are getting the account information as a User
property.
The problem with this is that guild members and users are different. Guild members, hold additional information for a user in a specific server, such as roles.
Currently you get the profile information from this variable:
const user =
this.client.users.cache.get(args[0]) || // This is a user property
message.mentions.members.first() || // This is a member propery
message.author; // This is a user property
The problem with this, is that it sometimes it will return a user, which doesnt have any roles stored in its data.
Here is a possible fix:
// We need to make sure they run this in a server so we can access the roles
if (!message.guild) return message.channel.send("This command must be ran in a server")
// This will always return a guild member
const user =
message.guild.members.cache.get(args[0]) ||
message.mentions.members.first() ||
message.member;
You can read about the User and Member on the discord.js docs
Discord.JS Userinfo command
The following code should do all the stuff you want:
var commando = require('discord.js-commando');
var discord = require('discord.js');
class aboutuser extends commando.Command
{
constructor(client) {
super(client, {
name: 'aboutuser',
group: 'help',
memberName: 'aboutuser',
description: 'Lists information about a specific user.',
aliases: ['au', 'aboutu', 'auser', 'user'],
})
}
async run(message, args){
const userMention = message.mentions.users.first() || msg.author;
const memberMention = message.mentions.members.first() || msg.member;
let userinfo = {};
userinfo.bot = userMention.bot;
userinfo.createdat = userMention.createdAt;
userinfo.discrim = userMention.discriminator;
userinfo.id = userMention.id;
userinfo.mfa = userMention.mfaEnabled;
userinfo.pre = userMention.premium;
userinfo.presen = userMention.presence;
userinfo.tag = userMention.tag;
userinfo.uname = userMention.username;
userinfo.verified = userMention.verified;
userinfo.avatar = userMention.avatarURL;
const rolesOfTheMember = memberMention.roles.filter(r => r.name !== '@everyone').map(role => role.name).join(', ')
var myInfo = new discord.RichEmbed()
.setAuthor(userinfo.uname, userinfo.avatar)
.addField("Bot?",userinfo.bot, true)
.addField("Created At",userinfo.createdat, true)
.addField("Discriminator",userinfo.discrim, true)
.addField("Client ID",userinfo.id, true)
.addField("2FA/MFA Enabled?",userinfo.mfa, true)
.addField("Paid Account?",userinfo.pre, true)
.addField("Presence",userinfo.presen, true)
.addField("Client Tag",userinfo.tag, true)
.addField("Username",userinfo.uname, true)
.addField("Verified?",userinfo.verified, true)
.setColor(0xf0e5da)
.setFooter('s!aboutserver')
.setTitle("About this user...")
.setThumbnail(userinfo.avatar)
message.channel.sendEmbed(myInfo);
}
}
module.exports = aboutuser;
I added two new variables that check if there was a mention of a guildMember or not. If yes, the command shows the stats of the mentioned member, if not, the bot shows the stats of the message author.
Then I added also a new variable "rolesOfTheMember" which is a list of all roles that the member owns. You can simply add this variable in your Discord RichEmbed and then list the roles that the guildMember has on the Discord server!
Have fun!
discord.js v12 User Info Command
You have to turn on intents from discord developer portal
- Go To Discord Developer Portal > Applications > Your Application (Bot)
- In Your Application (Bot) Go To Settings > Bot Section
- Scroll Down To Privileged Gateway Intents
- Turn On PRESENCE INTENT (You Have To Do Verification If Your Bot Is In 100+ Guilds)
Thats It, Now Please Restart Bot & Try Again
How to check two roles
You need to use something like this:
let role1 = message.guild.roles.cache.find(r => r.name === "owner");
let role2 = message.guild.roles.cache.find(r => r.name === "admin");
if(!message.member.roles.cache.has(role1.id) && !message.member.roles.cache.has(role2.id)) return message.reply("User don't have required roles!")
How to check if an user has any role discord.js
GuildMember.roles.cache
is a Collection
(very similar to an Array
).
Therefore, you can use Collection#filter
and check if the Role
's name equals @everyone
. That'll remove the @everyone
role from the Collection
and you can check if the GuildMember
has any roles.
.addFields({
name: 'Highest role',
value: message.member.roles.cache.size ? message.member.roles.highest : "This member has no roles."
})
Useful links I strongly recommend you to visit:
Collection#map
Collection#filter
Conditional (ternary) operator
Add On to Discord.JS Userinfo post
Use
var umen = message.mentions.members.first() || message.member;
var umen2 = message.mentions.users.first() || message.author;
Also had trouble with "Booleans" and those should NOT be === "false"
as they are Booleans, not strings. Do === false
or === true
Related Topics
Javascript Function Not Defined Error (But It Is Defined)
Javascript: Replace Last Occurrence of Text in a String
To Get a Popup Message After Submit
Hide Menu Sidebar When Clicking Outside the Bar or the Button
Amount of All Goals of One Team (From Json)
Dynamically Change Background Color on Scroll
Trigger Event When Element Becomes Visible With Ngif
Import Image from Json Source (Create-React-App)
How to Compare Dates in Mongo Query
How to Allow Only English Letters in Input Fields
Regex to Replace Everything Except Numbers and a Decimal Point
Get List of Filenames in Folder With JavaScript
Check Whether an Input String Contains a Number in JavaScript
How to Vertically and Horizontally Center a Component in React
Angular: Toggle Text of Button Based on Boolean Value in Model
Save State When Refresh Page - Reactjs
Multiple Key Names, Same Pair Value
How to Read a Properties File in JavaScript from Project Directory