There are a few things wrong with this code. First of all, Message.author
returns a User
, which does not have a nickname
. Since nicknames can be modified per server, you’re looking for a GuildMember
.
Also, GuildMember.nickname
will return undefined if the GuildMember
doesn’t actually have a nickname. There are a few ways you can fix it. You can either use GuildMember.displayName
, which will try to display the nickname but will display the username if none is found, or you could simply handle it with the logical OR operator ||
// method 1:
.addField('Nickname', message.author.displayName, true)
// method 2:
.addField('Nickname', message.author.nickname || 'This user has no nickname', true)
Speaking of GuildMembers
and Users
, you are using a very overcomplicated way to get the GuildMember
instance of the author at this line:
message.guild.member(message.author);
There’s actually an easier way to use this: Message.member
. It’s the same thing as Message.author
, but returns a GuildMember
. Also, I’m not sure why you’re using the _roles
getter instead of the much more efficient roles
.
You have the same problems with message.author.permissions
, as permissions are modified per guild and do not have one global value. Even then, it’s going to return [object Object]
. And even if it didn’t, it would probably return a bitfield representation which would not be legable by the average discord user.
Instead, you should use the Permissions.toArray()
method, which will return an array of bitfield names based on the bits available (all of the member’s permissions). It might look a bit like this:
[
CREATE_INSTANT_INVITE,
ADD_REACTIONS,
STREAM,
VIEW_CHANNEL,
SEND_MESSAGES,
EMBED_LINKS,
ATTACH_FILES,
READ_MESSAGE_HISTORY,
USE_EXTERNAL_EMOJIS,
CONNECT,
SPEAK,
USE_VAD,
CHANGE_NICKNAME,
];
You can use Array.prototype.join()
to join these elements by a line break, or a comma and space, etc.
Also, you shouldn’t be using template literals unless you want to add a variable to a string with other characters/template literals, or you need to coerce the variable’s data type to string. message.author.presence.status
for example, is already a string, so:
- `${ message.author.presence.status}`
+ message.author.presence.status
Presence.status
is returning ‘offline’ because you don’t have the GUILD_PRESENCES
intent enabled
CLICK HERE to find out more related problems solutions.