Reputation: 81
if (message.content.startsWith(`${prefix2}red`)){
if (message.member.roles.some(role => role.name === 'Red')) return message.channel.send(`You already has that role `)
let role = message.guild.roles.find(r => r.name === "Red");
let member = message.member;
message.delete(1)
member.addRole(role).catch(console.error)
}
})
What do I need to change? for it to work?
the error is
if (message.member.roles.some(role => role.name === 'Red')) return message.channel.send(`You already has that role `)
TypeError: message.member.roles.some is not a function
Upvotes: 3
Views: 4250
Reputation: 11
Well i got this problem when i wasnt checking if the array wasn't in a array format which was like this before
if (this.user?.roles.some((r) => this.appHasRole.includes(r))) {
this.viewContainerRef.createEmbeddedView(this.templateRef);
} else {
this.viewContainerRef.clear();
}
then i changed the code to do more checks which is as below
if (
this.user &&
Array.isArray(this.user.roles) &&
this.user.roles.some((r) => this.appHasRole.includes(r))
) {
this.viewContainerRef.createEmbeddedView(this.templateRef);
} else {
this.viewContainerRef.clear();
}
which then worked. try applying more checks before executing the code and it may work!
Upvotes: 0
Reputation: 297
It seems that message.member is undefined, you might want to check if this is done in a guild or not. If it is in a guild it will return the member property, while if it isn't, it won't. What you want to do is check if the message was sent from a guild or not, try the code below:
client.on("message", message => {
// `!` means `non-existent` or `is not`, and if the user sends the message from a guild
// this will not be triggered, since we know they are in, rather than not in, but, if
// it was sent outside of a guild, say a DM, then it will return the command, not trigerring
// any errors or such.
if (!message.guild) return;
// This will not allow this command to be triggered by the bot itself, since it may
// return a loop.
if (message.author === client.user) return;
// If the author of the message is a bot, then return, since bots can be used to spam
// and this will also spam your bot's API request. Webhooks work the same way.
// `||` means `or` if you didn't know.
if (message.author.bot || message.webhookID) return;
// Checks if the member has the role "ROLE NAME", and if they do, return.
if (message.member.roles.cache.some(role => role.name == "ROLE NAME")) return;
// code...
});
Upvotes: 0
Reputation: 5174
I am assuming you are using discord.js v12 and that's why your code won't work.
Try using message.member.roles.cache.some(role => role.name === 'Red')
instead of message.member.roles.some(role => role.name === 'Red')
Upvotes: 3