yas
yas

Reputation: 17

message.author.nickname returns 'undefined' - discord.js

I made a command called callsign, when a user does it, they enter their callsign and the bot is supposed to put their callsign into their nickname, example;

!callsign 004

Then it changes their nickname from the one they had to 004 | nickname, for example;

AshieReflex > after command > 004 | AshieReflex

but what it does, it just returns undefined: 004 | undefined.

Here's my code

var nickname = message.content.split (" ").slice (1).join (" ");

if (message.content.startsWith (prefix + 'callsign')) {
    message.member.setNickname (`${nickname} | ${message.author.nickname}`);
}

Upvotes: 0

Views: 1305

Answers (2)

Itamar S
Itamar S

Reputation: 1565

The user object does not have a nickname property. What you're looking for, instead, is the GuildMember object of the message's author - This can be collected using the member property of the Message object.

Quick suggestion: It is better to use the displayName property of the GuildMember object.

Final Code

var nickname = message.content.split (" ").slice (1).join (" ");
if (message.content.startsWith (prefix + 'callsign')) {
  message.member.setNickname (`${nickname} | ${message.member.displayName}`);
}

Upvotes: 1

Joe Moore
Joe Moore

Reputation: 2023

message#author#nickname is not a function. What you're looking for is username

var nickname = message.content.split (" ").slice (1).join (" ");
if (message.content.startsWith (prefix + 'callsign')) {
  message.member.setNickname (`${nickname} | ${message.author.username}`);
}

Upvotes: 0

Related Questions