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)
}
})
我需要更改什么?为它工作?
错误是
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
答案 0 :(得分:2)
我假设您正在使用discord.js v12,这就是为什么您的代码无法正常工作的原因。
尝试使用message.member.roles.cache.some(role => role.name === 'Red')
代替message.member.roles.some(role => role.name === 'Red')
答案 1 :(得分:0)
似乎message.member是未定义的,您可能要检查是否在行会中完成。如果它在公会中,则将返回member属性,如果它不在公会中,则不会。您要做的是检查邮件是否是从公会发送的,请尝试以下代码:
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...
});