kai 𓇢𓆸
kai 𓇢𓆸
DIAdiscord.js - Imagine an app
Created by kai 𓇢𓆸 on 7/20/2024 in #djs-questions
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.');
}
}
};
57 replies