我使用此代码检查了我的漫游器中的角色,但是由于某种原因,我得到了一个错误。
client.on("message", message => {
if (message.member.roles.cache.some(role => role.name == "ROLE NAME")) return;
// code...
});
错误如下:
TypeError: Cannot read property 'roles' of null
有人有解决方案吗?
答案 0 :(得分:0)
看来message.member
是undefined
,您可能想检查一下是否在行会中完成。如果它在行会中,它将返回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...
});