Error checking member role permissions

Error
if (member.roles.highest.position >= interaction.member.roles.highest.position) {
^

TypeError: Cannot read properties of undefined (reading 'highest')
if (member.roles.highest.position >= interaction.member.roles.highest.position) {
^

TypeError: Cannot read properties of undefined (reading 'highest')
Code
const { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits } = require("discord.js");

module.exports = {
data: new SlashCommandBuilder()
.setName("kick")
.setDescription("Kick a member from the server")
.setDMPermission(false)
.setDefaultMemberPermissions(PermissionFlagsBits.KickMembers)
.addUserOption(option =>
option.setName("member")
.setDescription("Select a member to kick")
.setRequired(true)
).addStringOption(option =>
option.setName("reason")
.setDescription("Provide a reason for the kick")
.setRequired(false)
),

async execute(interaction) {
const { options, client, guild } = interaction;

const user = options.getUser("member");
const member = await guild.members.fetch(user.id);
const clientMember = guild.members.me;

if (clientMember.permissions.has(PermissionFlagsBits.KickMembers)) {
console.log(member.roles);

if (member.roles.highest.position >= interaction.member.roles.highest.position) {
return interaction.reply({
embeds: [
new EmbedBuilder()
.setColor(client.config.colors.error)
.setDescription(`You can't take action on **${member.user.displayName}** because they have higher permissions.`)
]
});
}

// Can kick member
} else {
// Can't kick member
}
}
}
const { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits } = require("discord.js");

module.exports = {
data: new SlashCommandBuilder()
.setName("kick")
.setDescription("Kick a member from the server")
.setDMPermission(false)
.setDefaultMemberPermissions(PermissionFlagsBits.KickMembers)
.addUserOption(option =>
option.setName("member")
.setDescription("Select a member to kick")
.setRequired(true)
).addStringOption(option =>
option.setName("reason")
.setDescription("Provide a reason for the kick")
.setRequired(false)
),

async execute(interaction) {
const { options, client, guild } = interaction;

const user = options.getUser("member");
const member = await guild.members.fetch(user.id);
const clientMember = guild.members.me;

if (clientMember.permissions.has(PermissionFlagsBits.KickMembers)) {
console.log(member.roles);

if (member.roles.highest.position >= interaction.member.roles.highest.position) {
return interaction.reply({
embeds: [
new EmbedBuilder()
.setColor(client.config.colors.error)
.setDescription(`You can't take action on **${member.user.displayName}** because they have higher permissions.`)
]
});
}

// Can kick member
} else {
// Can't kick member
}
}
}
6 Replies
d.js toolkit
d.js toolkit16mo 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!
Spam
SpamOP16mo ago
- [email protected] - v18.16.0 I can't seem to get the member's highest role or even their roles. also pls ping on reply thanks
treble/luna
treble/luna16mo ago
you can call .getMember directly, that way you dont have to fetch
Spam
SpamOP16mo ago
options.getMember ?
treble/luna
treble/luna16mo ago
yes
Spam
SpamOP16mo ago
thank you
Want results from more Discord servers?
Add your server