Userinfo Command

if (check) {
const user = await args.pick("member").catch(() => message.member.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));
console.log(user.roles.cache);

let sortedRoles;
if (roles.length > 0) sortedRoles = roles.join(", ");
else sortedRoles = "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: `${sortedRoles}`,
inline: false,
}
);
embed.setFooter({ text: footer });
}
if (check) {
const user = await args.pick("member").catch(() => message.member.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));
console.log(user.roles.cache);

let sortedRoles;
if (roles.length > 0) sortedRoles = roles.join(", ");
else sortedRoles = "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: `${sortedRoles}`,
inline: false,
}
);
embed.setFooter({ text: footer });
}
So basically I have this code but for some reason it logs this error: TypeError: Cannot read properties of undefined (reading 'cache') at UserInfoCommand.messageRun (C:\Users\cmart\Desktop\Discord Bots\Sapphire\src\commands\Utility\userinfo.js:71:32) also if there is a better way of sorting roles in order please tell me
9 Replies
d.js toolkit
d.js toolkit•16mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Nansess
Nansess•16mo ago
add error handling to narrow down the issue
-Carlos👑
-Carlos👑OP•16mo ago
it's not the full code full code is inside a try and catch
Nansess
Nansess•16mo ago
yes but u can add something like
if (!user.roles || !user.roles.cache) {
console.log("cant get the roles lol");
return;
}

let roles = user.roles.cache
.filter((role) => role.id !== message.guild.id)
.map((role) => role.toString())
.sort((a, b) => a.localeCompare(b));
if (!user.roles || !user.roles.cache) {
console.log("cant get the roles lol");
return;
}

let roles = user.roles.cache
.filter((role) => role.id !== message.guild.id)
.map((role) => role.toString())
.sort((a, b) => a.localeCompare(b));
to see what the issue is it can be multiple things user might not even be defined
-Carlos👑
-Carlos👑OP•16mo ago
@Nan just a question does pm2 consume resources from my pc?
Nansess
Nansess•16mo ago
no lol its always running the node tho pm2 stop all or pm2 status
-Carlos👑
-Carlos👑OP•16mo ago
alr will it work even if im hosting my bot on a hosting provider
Nansess
Nansess•16mo ago
yeah pm2 is a node mananager it keeps nodes online and logs errors and outputs u dont run ur bot full time with node example.js
-Carlos👑
-Carlos👑OP•16mo ago
alr
Want results from more Discord servers?
Add your server