Change user nickname

Good morning. I want to change the nickname of the user whose user id is entered in the command option, except I did some research and I found a line of code but it does not work, an idea ? Error :
C:\Users\Elève\Desktop\Project\HeartOfBot\Commands\testvalidation.js:147
message.guild.members.get(val[0].User).setNickname(`${val[0].King}`)
^

TypeError: message.guild.members.get is not a function
at Query.<anonymous> (C:\Users\Elève\Desktop\Project\HeartOfBot\Commands\testvalidation.js:147:43)
at processTicksAndRejections (node:internal/process/task_queues:96:5)

Node.js v17.0.0
C:\Users\Elève\Desktop\Project\HeartOfBot\Commands\testvalidation.js:147
message.guild.members.get(val[0].User).setNickname(`${val[0].King}`)
^

TypeError: message.guild.members.get is not a function
at Query.<anonymous> (C:\Users\Elève\Desktop\Project\HeartOfBot\Commands\testvalidation.js:147:43)
at processTicksAndRejections (node:internal/process/task_queues:96:5)

Node.js v17.0.0
20 Replies
d.js toolkit
d.js toolkit2y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
Danial
Danial2y ago
I think you wanna di guild.members.cache.get(), but not all members are cached so you wanna fetch instead and resolve the promise it returns
🖤 • Nelson • 🖤
I'm not quite sure I understand
Danial
Danial2y ago
What do you not understand?
🖤 • Nelson • 🖤
I'm not really sure to understand what I'm going to have to do to change the user's name thanks to his ID, I'm going to have to use a .cache()?
Danial
Danial2y ago
You would do that if you were sure that member is cached, but I'd recommend to fetch instead as the member might not be cached
🖤 • Nelson • 🖤
Can I retrieve it using its ID? What I want to do is that I have store the ID in a database with the new username and other information, and I would like to suddenly retrieve the ID and the name store then change the nickname of the user which corresponds to the ID retrieved from the database by the nickname also retrieved from the database. But I don't really see how to do it because it doesn't seem like you can do fetch() on an ID, and if it's not in the cache, I don't really see a way out...
Danial
Danial2y ago
Yes, you can, guild.members.fetch("id")
🖤 • Nelson • 🖤
And I just need to end with .setNickname("nickname")?
Danial
Danial2y ago
fetch() returns a promise so you'd need to resolve it first though
🖤 • Nelson • 🖤
And how do I use it and resolve it?
d.js docs
d.js docs2y ago
guide Additional Information: How do Promises work? read more
🖤 • Nelson • 🖤
A (await message.guild.members.fetch(val[0].User)).user should allow me to retrieve the user?
MrMythical
MrMythical2y ago
1. If you are using slash commands, just use a user option 2. Use <Guild>.members.edit(id, { nick })
🖤 • Nelson • 🖤
This way of proceeding seems much simpler.
Danial
Danial2y ago
Didn't know about that, noted for the future, thank you Myth
🖤 • Nelson • 🖤
A question, DiscordAPIError[50013]: Missing Permissions this error occurs, if the bot has the Administrator permissions it must still have the Change a member's nickname permissions while it is an administrator?
d.js docs
d.js docs2y ago
- Bots cannot moderate (kick/ban/nickname/...) a target with a higher or equally high highest role or the guild owner. - Bots cannot modify (edit/add/remove) roles that are higher or equally high compared to the bot's highest role. - The Administrator permission does not skip these checks.
🖤 • Nelson • 🖤
Oh I didn't know that he can't change the nickname of the server owner, thank you, that's a thing to know indeed ^^'
Danial
Danial2y ago
Of course
Want results from more Discord servers?
Add your server