args.pick issue

Code:
if (check) {
user = await args.pick("member").catch(() => message.member.user);
console.log(user);
const joinedAt = Math.round(user.joinedAt / 1000);

let roles =
user.roles.cache
.filter((role) => role.id !== message.guild.id)
.map((role) => role.toString())
.sort((a, b) => a.localeCompare(b)) || "User has no roles.";

embed.addFields(
{
name: `Joined At`,
value: `<t:${joinedAt}:f> (<t:${joinedAt}:R>)`,
inline: false,
},
{
name: `Roles [${user.roles.cache.size - 1}]`,
value: `${roles}`,
inline: false,
}
);
embed.setFooter({ text: footer });
}
if (check) {
user = await args.pick("member").catch(() => message.member.user);
console.log(user);
const joinedAt = Math.round(user.joinedAt / 1000);

let roles =
user.roles.cache
.filter((role) => role.id !== message.guild.id)
.map((role) => role.toString())
.sort((a, b) => a.localeCompare(b)) || "User has no roles.";

embed.addFields(
{
name: `Joined At`,
value: `<t:${joinedAt}:f> (<t:${joinedAt}:R>)`,
inline: false,
},
{
name: `Roles [${user.roles.cache.size - 1}]`,
value: `${roles}`,
inline: false,
}
);
embed.setFooter({ text: footer });
}
Error: TypeError: Cannot read properties of undefined (reading 'cache') idk why it isn't updating the user variable, I logged user inside the if and it returns a User object
11 Replies
Lioness100
Lioness100•14mo ago
The default shouldn't be message.member.user (which is the same thing as message.author, by the way), it should be message.member Member objects are specific to guilds and thus have roles, nicknames, etc. Users are global.
-Carlos🎃
-Carlos🎃•14mo ago
was it updated or something? it was working without any problem some weeks ago
Lioness100
Lioness100•14mo ago
It was definitely not updated maybe you just never had it use the fallback arg before
-Carlos🎃
-Carlos🎃•14mo ago
now if the user provided has no roles it will just return my roles (or the roles of the person that ran the command)
Lioness100
Lioness100•14mo ago
That looks to be what the code does. What are you trying to make it do?
-Carlos🎃
-Carlos🎃•14mo ago
to show the roles of the provided user if no user is provided then the roles of the author but wouldn't user already do that
Lioness100
Lioness100•14mo ago
now if the user provided has no roles it will just return my roles (or the roles of the person that ran the command)
It sounds like that fits your description?
-Carlos🎃
-Carlos🎃•14mo ago
no? it should say "User has no roles." instead
Lioness100
Lioness100•14mo ago
You need to convert the value to a string before the || "..." otherwise it's just randomArray || "...", and the array is always truthy what's not truthy is an empty string, so convert it to a strip first with .toString() or .join(',')
-Carlos🎃
-Carlos🎃•14mo ago
let roles =
user.roles.cache
.filter((role) => role.id !== message.guild.id)
.map((role) => role.toString())
.sort((a, b) => a.localeCompare(b))
.toString() || "User has no roles.";
let roles =
user.roles.cache
.filter((role) => role.id !== message.guild.id)
.map((role) => role.toString())
.sort((a, b) => a.localeCompare(b))
.toString() || "User has no roles.";
like this? didn't work so changed the args.pick to a message.guild.members.cache.get(user.id) last question, do you know how to order the roles in the order that are in the server rn .sort((a, b) => a.localeCompare(b)) sorts them alphabetically
Lioness100
Lioness100•14mo ago
discord.js
discord.js
discord.js is a powerful Node.js module that allows you to interact with the Discord API very easily. It takes a much more object-oriented approach than most other JS Discord libraries, making your bot's code significantly tidier and easier to comprehend.
Want results from more Discord servers?
Add your server