Cannot read properties of undefined (reading 'roles')

I am trying to get the user's highest role and compare it to a role they are attemping to give another user through a slash command. The command worked perfectly until I tried to add this checker, and now it's breaking. The error I am getting is TypeError: Cannot read properties of undefined (reading 'roles') The line that is erroring is const senderHR = interaction.sender.roles.highest.position; Full code:
const { SlashCommandBuilder, PermissionFlagsBits, GuildMemberRoleManager } = require('discord.js');
const { execute } = require('./server');

module.exports = {
data: new SlashCommandBuilder()
.setName('giverole')
.setDescription('Adds the selected role to the user.')
.addUserOption(option =>
option
.setName('target')
.setDescription('The member to give the role to')
.setRequired(true))
.addRoleOption(option =>
option
.setName('role')
.setDescription('The role to add')
.setRequired(true))
.setDefaultMemberPermissions(PermissionFlagsBits.ManageRoles)
.setDMPermission(false),
async execute(interaction) {
const sender = interaction.sender
const target = interaction.options.getUser('target');
const giveRoleTo = interaction.guild.members.cache.get(target.id);
const role = interaction.options.getRole('role');
const senderHR = interaction.sender.roles.highest.position;
if (role.position <= senderHR);
try {
await giveRoleTo.roles.add(role)
await interaction.reply(`✅ | Successfully added ${role.name} to ${target.username}!`);
} catch (error) {
console.error(error);
await interaction.reply('❌ | There was an error giving the role.');
}
}
};
const { SlashCommandBuilder, PermissionFlagsBits, GuildMemberRoleManager } = require('discord.js');
const { execute } = require('./server');

module.exports = {
data: new SlashCommandBuilder()
.setName('giverole')
.setDescription('Adds the selected role to the user.')
.addUserOption(option =>
option
.setName('target')
.setDescription('The member to give the role to')
.setRequired(true))
.addRoleOption(option =>
option
.setName('role')
.setDescription('The role to add')
.setRequired(true))
.setDefaultMemberPermissions(PermissionFlagsBits.ManageRoles)
.setDMPermission(false),
async execute(interaction) {
const sender = interaction.sender
const target = interaction.options.getUser('target');
const giveRoleTo = interaction.guild.members.cache.get(target.id);
const role = interaction.options.getRole('role');
const senderHR = interaction.sender.roles.highest.position;
if (role.position <= senderHR);
try {
await giveRoleTo.roles.add(role)
await interaction.reply(`✅ | Successfully added ${role.name} to ${target.username}!`);
} catch (error) {
console.error(error);
await interaction.reply('❌ | There was an error giving the role.');
}
}
};
31 Replies
d.js toolkit
d.js toolkit5mo 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!
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
kai 𓇢𓆸
kai 𓇢𓆸OP5mo ago
i just replaced it with interaction.member and it still gave me the role even though it was higher than my previous highest role if that doesnt make sense i had member role, ran the command to give me the owner role and it gave me the role it isn't supposed to give me the role if my highest role is below the role i am trying to give
d.js docs
d.js docs5mo 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.
kai 𓇢𓆸
kai 𓇢𓆸OP5mo ago
thats not what i was saying
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
kai 𓇢𓆸
kai 𓇢𓆸OP5mo ago
no but im going to be using this bot in my server to give people roles like staff and things for when im lazy but what im trying to do is if my role is say for example the moderator role and i try to give myself the administrator role i dont want it to work but it does give me the role im trying to get it so the bot won't give me the role if my highest role is below the role im trying to give
d.js docs
d.js docs5mo ago
:mdn: Greater than or equal (>=) The greater than or equal (>=) operator returns true if the left operand is greater than or equal to the right operand, and false otherwise. :mdn: Less than or equal (<=) The less than or equal (<=) operator returns true if the left operand is less than or equal to the right operand, and false otherwise.
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
kai 𓇢𓆸
kai 𓇢𓆸OP5mo ago
it is but its set up correctly and the bot still gives the role
treble/luna
treble/luna5mo ago
because youre comparing your own role position
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
treble/luna
treble/luna5mo ago
not the one from the member also .getMember exists
kai 𓇢𓆸
kai 𓇢𓆸OP5mo ago
const { SlashCommandBuilder, PermissionFlagsBits, GuildMemberRoleManager } = require('discord.js');
const { execute } = require('./server');

module.exports = {
data: new SlashCommandBuilder()
.setName('giverole')
.setDescription('Adds the selected role to the user.')
.addUserOption(option =>
option
.setName('target')
.setDescription('The member to give the role to')
.setRequired(true))
.addRoleOption(option =>
option
.setName('role')
.setDescription('The role to add')
.setRequired(true))
.setDefaultMemberPermissions(PermissionFlagsBits.ManageRoles)
.setDMPermission(false),
async execute(interaction) {
const sender = interaction.member
const target = interaction.options.getUser('target');
const giveRoleTo = interaction.guild.members.cache.get(target.id);
const role = interaction.options.getRole('role');
const senderHR = interaction.member.roles.highest.position;
if (role.position >= senderHR);
interaction.reply('❌ | You cannot give roles higher than your highest role.')
try {
await giveRoleTo.roles.add(role)
await interaction.reply(`✅ | Successfully added ${role.name} to ${target.username}!`);
} catch (error) {
console.error(error);
await interaction.reply(`❌ | There was an error giving the role: ${error}`);
}
}
};
const { SlashCommandBuilder, PermissionFlagsBits, GuildMemberRoleManager } = require('discord.js');
const { execute } = require('./server');

module.exports = {
data: new SlashCommandBuilder()
.setName('giverole')
.setDescription('Adds the selected role to the user.')
.addUserOption(option =>
option
.setName('target')
.setDescription('The member to give the role to')
.setRequired(true))
.addRoleOption(option =>
option
.setName('role')
.setDescription('The role to add')
.setRequired(true))
.setDefaultMemberPermissions(PermissionFlagsBits.ManageRoles)
.setDMPermission(false),
async execute(interaction) {
const sender = interaction.member
const target = interaction.options.getUser('target');
const giveRoleTo = interaction.guild.members.cache.get(target.id);
const role = interaction.options.getRole('role');
const senderHR = interaction.member.roles.highest.position;
if (role.position >= senderHR);
interaction.reply('❌ | You cannot give roles higher than your highest role.')
try {
await giveRoleTo.roles.add(role)
await interaction.reply(`✅ | Successfully added ${role.name} to ${target.username}!`);
} catch (error) {
console.error(error);
await interaction.reply(`❌ | There was an error giving the role: ${error}`);
}
}
};
ik the if part is messy i tried another thing and it still gives the role
treble/luna
treble/luna5mo ago
const senderHR = interaction.member.roles.highest.position;
kai 𓇢𓆸
kai 𓇢𓆸OP5mo ago
that's the person that uses the command im checking if their role is higher than the role they're trying to give if its below the role theyre trying to give then it shouldnt work
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
kai 𓇢𓆸
kai 𓇢𓆸OP5mo ago
how i took my roles and im giving myself a role above the current role i have and its still working
d.js docs
d.js docs5mo ago
:method: RoleManager#comparePositions @14.15.3 Compares the positions of two roles.
can
can5mo ago
use this
kai 𓇢𓆸
kai 𓇢𓆸OP5mo ago
how do i check whether it returned positive or negative do i do something like if (compareRoles(role.position, senderHR) >= 1); give the role
can
can5mo ago
if (interaction.guild.roles.comparePositions(interaction.member.roles.highest, role) <= 0) {
// return error
}
if (interaction.guild.roles.comparePositions(interaction.member.roles.highest, role) <= 0) {
// return error
}
like this
kai 𓇢𓆸
kai 𓇢𓆸OP5mo ago
thankss is there a way to make it send different replies based on that i tried using else but it just gives me a constant red line under everything
Mark
Mark5mo ago
Yes, your syntax is likely incorrect if that's the result
kai 𓇢𓆸
kai 𓇢𓆸OP5mo ago
is this how i use it
No description
can
can5mo ago
you know basic javascript right?
kai 𓇢𓆸
kai 𓇢𓆸OP5mo ago
yes it worked tysm! :X_whitehearts:
can
can5mo ago
add return before interaction.reply and remove else that doing nothing
kai 𓇢𓆸
kai 𓇢𓆸OP5mo ago
i did and now its not responding when i run the command
can
can5mo ago
add "else" then
kai 𓇢𓆸
kai 𓇢𓆸OP5mo ago
it worked thankss
Want results from more Discord servers?
Add your server