Hello all im getting the following error
`
memberTarget.setNickname("nickname");
^
TypeError: Cannot read properties of undefined (reading 'setNickname')
`
for code
`
const memberTarget = client.users.cache.get("930654978777948160");
memberTarget.setNickname("nickname");
`
I have searched and there's not much information on this error, Things i have double checked is the bot permissions and it has the correct permissions to manage other users nicknames i'm currently stumped with this one
UserManager#cache.get() returns a User, while you need to get a Member (a user in a server). To do this, you can use the following:
const serverID = "39883983933980"; // this is the ID of your server
// it can be message.guild.id for example
client.guilds.fetch(serverID).then((server) => {
server.members.fetch("930654978777948160").then((memberTarget) => {
memberTarget.setNickname("nickname");
});
});
Note: this has to be added in the ready event of your code
Related
I get this Error:
TypeError: Cannot read property 'displayName' of null
This is the code that should console-log the command and the username of the user who used it:
client.on('message', (message) => {
const { content, member } = message
const autor = member.displayName || member.username
aliases.forEach((alias) => {
const command = `${prefix}${alias}`
if (content.startsWith(`${command}`) || content === command) {
console.log(`${autor} used: ${command}`)
callback(message)
}
})
})
In the console I get the username but it still gives an error. It only gives this error if I use a specific command. The command sends an embed of the message. Then it sends a copy of the message to the user who sent it.
if (message.content.includes('...'))
{message.delete({timeout: 800})
.then(message.member.send(message.content))
message.channel.send(embed)
Thank you for your help
If you want to use the nickname primarily, you can make your bot default to the username if the nickname is unavailable.
For your first section, you can use a ternary operator.
const autor = member ? member.displayName : message.author.username
If member returns null, it'll instead use the username property of the author which is never null.
For the second part of your code, you might as well replace message.member.send() with message.author.send() since then former provides no advantages over the latter and instead only leads to instances such as this one.
As from the documentation about message.member:
Represents the author of the message as a guild member. Only available if the message comes from a guild where the author is still a member
That means the error you are having may happen if the user is not a member of the server/guild anymore, or is a private message.
You can use message.author if you just want the username. Or you can return the function when message.member is null.
I am trying to make a leaderboard for how long people are in voice chat but when people leave the server and are still in the database it causes a type error as it cannot read that user's id in the server and so i tried
if (!client.guilds.cache.get(serverid).members.cache.get(user)){
continue
}
to skip out people who were not in the server but this caused another type error: TypeError: Cannot read property '281377425215062016' of undefined and i'm not sure how to fix this, any help would be appreciated.
for (const user in VoiceDatabase[serverid]){
if (!client.guilds.cache.get(serverid).members.cache.get(user)){continue}
if (user == "Servername"){continue}
templeaderboard.push(VoiceDatabase[serverid][user].TotalXP)
Use guildMemberRemove to get when user left the server
client.on('guildMemberRemove', async member => {
});
Such as get member when left console.log -> read username of user left
client.on('guildMemberRemove', async member => {
console.log(member.user.username)
});
Reference
GuildMember
GuildMemberRemove
I am trying to make my bot's role a pink color for the current server using the Client.on("ready") However whenever I run the bot with what I currently have the console returns:
(node:6504) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'cache' of undefined
Here is my code I am currently using. I know I need to for loop on every guild but I'm not sure how I would do that, I could use a for or just a .forEach however I can't find the correct way to do such.
const role = client.guild.roles.cache.get('Aiz Basic+');
role.edit({ name: 'Aiz Basic+', color: '#FFC0CB' });
In advance thank you to anybody who replies and helps with my message.
Client#Guild Isn‘t existing. Only Client#Guilds. You can fix this problem by looping through the cache of all your guilds with:
client.guilds.cache.forEach((g) => { })
And inside the loop you can use your code after fixing the .get function, because the get function needs a role id not a role name. You could find the role via the name by writing:
const role = <Guild>.roles.cache.find((r) => r.name === /* The role name */);
Make sure you replaced <Guild> with your guild variable.
Try this code if client.guild is not defined, it could also be because you are using bot instead of client
let role = message.guild.roles.cache.find(role => role.name === "Aiz Basic+");
My Goal
My goal is to figure out why Collection#find returns undefined when I try to find a user in my server, but they're offline and have no roles.
Expectation
Usually the console logs an array of all the properties of the user
Actual Result
The console logs Collection#find, as undefined if the user in my server is offline and has no roles
What I've Tried
I've tried using Collection#get, but it turns out that it returns the same response. I've tried searching this up on Google, but no one has asked this question before.
Reproduction Steps
const Discord = require('discord.js');
const client = new Discord.Client();
const {prefix, token} = require('./config.json');
client.once('ready', () => {
console.log('Client is online');
const user = client.users.cache.find(user => user.tag === 'Someone#1234');
console.log(user);
};
client.login(token);
Make sure that whoever is helping you, whether it's an alt account or your friend, that they have no roles, and they're completely offline in your server
Output:
Client is online undefined
I had the same problem. I don't know why this happens, but I used this line to fix that:
((await m.guild.members.fetch()).filter(mb => mb.id === "The ID")).first()`
Basically, this collect all the members, then filters them with the property you want, and the first() at the end is to make it a single object instead of a collection.
Instead of the user.tag property, try using the user.username and the user.discriminator properties. This worked for me
const user = client.users.cache.find(user => user.username === 'Someone' && user.discriminator === '1234');
Also check spelling, capitalization, ect.
A simple solution to my problem is just create an async function and use await guild.members.fetch() to cache the users and then use Collection#find to get the user.
I'm trying to write an announce command which when used sends an announcement to the announcements channel, but only if you have a certain role, that works, but it shows an error in my console.
The error is TypeError: client.fetchGuild is not a function
if (await client.fetchGuild(message.guild.id).fetchMember(message.author.id).hasPermission("MENTION_EVERYONE") && message.content.startsWith(adminPrefix + 'announce')) {
const trueMessage = message.content.substr(10)
client.channels.get('545343689216491541').send('#everyone, ' + trueMessage)
}
How do I make it send no errors
P.s. I'm new to this, very new.
fetchGuild
is not a function. Use
client.guilds.get(id)
or
message.guild
as you already have the guild attached to the message object
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get
https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=guilds
In order to verify if a member has a role, you'd better use this:
if(message.member.roles.cache.some(role => role.name === 'role name'))
then send the message inside the if statement