add role

hi so ive been trying a lot to make this work but it just wont
if (command === "addrole") {
if (!message.member.permissions.has(PermissionsBitField.Flags.ManageRoles)) {
return message.channel.send('no perms')
}
if (!message.guild.members.me.permissionsIn(message.channel).has(PermissionsBitField.Flags.ManageRoles)) {
return message.channel.send('no perms')
}
if (!args[0]) return message.channel.send("error reply")
let nombrea = args[0]
let user = message.mentions.users.first() || client.users.cache.get(args[0]) || client.users.cache.find(user => user.username === nombrea)
let role = message.mentions.roles.first() || message.guild.roles.cache.find(r => r.id == args[1])
if (!user) return message.channel.send("errpr reply")
if (!args[1]) return message.channel.send("error reply")
if (!role) return message.channel.send("error reply")
if (!role.editable) return message.channel.send("error reply")
// if (user.roles.cache.has(role.id)) return message.channel.send("reply")

if (role.comparePositionTo(message.member.roles.highest) >= 0) return message.channel.send("No puedes añadir un rol igual o superior al tuyo!")
// if (message.member.roles.highest.comparePositionTo(user.roles.highest) <= 0) return message.channel.send("reply")
try {
user.roles.add(role)
} catch (error){
console.log(error)
console.log(user)
console.log(role)
return message.channel.send('error reply')
}
return message.channel.send(`reply`)
}
if (command === "addrole") {
if (!message.member.permissions.has(PermissionsBitField.Flags.ManageRoles)) {
return message.channel.send('no perms')
}
if (!message.guild.members.me.permissionsIn(message.channel).has(PermissionsBitField.Flags.ManageRoles)) {
return message.channel.send('no perms')
}
if (!args[0]) return message.channel.send("error reply")
let nombrea = args[0]
let user = message.mentions.users.first() || client.users.cache.get(args[0]) || client.users.cache.find(user => user.username === nombrea)
let role = message.mentions.roles.first() || message.guild.roles.cache.find(r => r.id == args[1])
if (!user) return message.channel.send("errpr reply")
if (!args[1]) return message.channel.send("error reply")
if (!role) return message.channel.send("error reply")
if (!role.editable) return message.channel.send("error reply")
// if (user.roles.cache.has(role.id)) return message.channel.send("reply")

if (role.comparePositionTo(message.member.roles.highest) >= 0) return message.channel.send("No puedes añadir un rol igual o superior al tuyo!")
// if (message.member.roles.highest.comparePositionTo(user.roles.highest) <= 0) return message.channel.send("reply")
try {
user.roles.add(role)
} catch (error){
console.log(error)
console.log(user)
console.log(role)
return message.channel.send('error reply')
}
return message.channel.send(`reply`)
}
im updating my code from v12.3.1 to 14.13.0 and im trying to update this code but there are 3 errors first one, on the first // there is an error with the 'has' second one, on the second //there is an error with 'highest' and finally, on user.roles.add(role) there is always the error 'cannot read properties of undefined reading 'add'' and all of the console.log give me a real role and a real member, also im testing on a test server so i have perms, the bot has perms, the user is below the bot, the role is below the bot role idk what to do
11 Replies
d.js toolkit
d.js toolkit13mo 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!
treble/luna
treble/luna13mo ago
users dont have roles, members do
Ferklen
Ferklen13mo ago
let user = message.mentions.users.first() client.users.cache.get(args[0]) client.users.cache.find(user => user.username === nombrea) should i just change that to member?
treble/luna
treble/luna13mo ago
yes and fetch instead of using cache you'd have to refactor to <Guild>.members
Ferklen
Ferklen13mo ago
if (command === "addrole") {
if (!message.member.permissions.has(PermissionsBitField.Flags.ManageRoles)) {
return message.channel.send('r')
}
if (!message.guild.members.me.permissionsIn(message.channel).has(PermissionsBitField.Flags.ManageRoles)) {
return message.channel.send('r')
}
if (!args[0]) return message.channel.send("r")
let nombrea = args[0]
let member = message.mentions.users.first()
let role = message.mentions.roles.first()
if (!member) return message.channel.send("r")
if (!args[1]) return message.channel.send("r")
if (!role) return message.channel.send("r")
if (!role.editable) return message.channel.send("r")
if (role.comparePositionTo(message.member.roles.highest) >= 0) return message.channel.send("r")
try {
member.roles.add(role)
} catch (error){
console.log(error)
console.log(member)
console.log(role)
return message.channel.send('r')
}
return message.channel.send(`r`)
}
if (command === "addrole") {
if (!message.member.permissions.has(PermissionsBitField.Flags.ManageRoles)) {
return message.channel.send('r')
}
if (!message.guild.members.me.permissionsIn(message.channel).has(PermissionsBitField.Flags.ManageRoles)) {
return message.channel.send('r')
}
if (!args[0]) return message.channel.send("r")
let nombrea = args[0]
let member = message.mentions.users.first()
let role = message.mentions.roles.first()
if (!member) return message.channel.send("r")
if (!args[1]) return message.channel.send("r")
if (!role) return message.channel.send("r")
if (!role.editable) return message.channel.send("r")
if (role.comparePositionTo(message.member.roles.highest) >= 0) return message.channel.send("r")
try {
member.roles.add(role)
} catch (error){
console.log(error)
console.log(member)
console.log(role)
return message.channel.send('r')
}
return message.channel.send(`r`)
}
i simplified the code by not using the cache and switched user to member and still got the same errors errors here // if (user.roles.cache.has(role.id)) return message.channel.send("reply") // if (message.member.roles.highest.comparePositionTo(user.roles.highest) <= 0) return message.channel.send("reply") and if i delete them there is still the error cannot read properties of add and it seemed weird to just switch user to member as in console.log(user/member) is the same thing
treble/luna
treble/luna13mo ago
you did not change it you just renamed your variable that wont magically change its type
Ferklen
Ferklen13mo ago
wdym change it? let member = message.mentions.users.first() what else could it be? and it gives me a member objet on the console
treble/luna
treble/luna13mo ago
thats not a member is it?
d.js docs
d.js docs13mo ago
property MessageMentions#members Any members that were mentioned (only in Guilds) (more...)
Ferklen
Ferklen13mo ago
thanks !
Want results from more Discord servers?
Add your server