问题
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
回答1:
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')
回答2:
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...
});
来源:https://stackoverflow.com/questions/60983028/typeerror-message-member-roles-some-is-not-a-function