dev jaden
dev jaden

Reputation: 35

How would I change the nickname of a user with a message in Discord.js?

I'm trying to change a user's nickname in Discord.js v12, yet for some reason I get the error:

TypeError: fn is not a function

Here is my code:

message.guild.members.cache.find(message.author.id).setNickname(username)

The message is just a Message object from a .on("message").

Upvotes: 2

Views: 192

Answers (1)

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23189

Unlike .get(), .find() accepts a function, so you can use it like this:

message.guild.members.cache
  .find(member => member.id === message.author.id)
  .setNickname()

You could also use .get():

message.guild.members.cache
  .get(message.author.id)
  .setNickname()

Or even better, use .fetch():

message.guild.members.fetch(message.author.id)
  .then(member => member.setNickname())

Or, you can get the member from the message itself using the member property that represents the author of the message as a guild member:

message.member.setNickName()

Upvotes: 3

Related Questions