首页 > 解决方案 > 检查某人是否具有 Discord.js 的角色

问题描述

我正在尝试使用 discord.js 制作一个不和谐的机器人,但我遇到了这个错误。我知道还有其他关于此的文章,但我无法让它发挥作用。在运行命令之前,我需要检查用户是否具有 admin/mod 角色。

        case 'clear':
        if(!author.roles.cache.has('686001018277855308'))  return msg.reply('you dont have the permissions to do this.');
        else if(author.roles.cache.has('686001018277855308')){

            if(!args[1]) return msg.reply('please add the number of messages to clear in the command');
            else{
                msg.channel.bulkDelete(args[1]);
                msg.reply(args[1] + ' messages cleared!');
            }
        }

    break;

我只是无法让它工作。我收到以下错误:

C:\Program Files\nodejs\node.exe index.js froggo 上线啦!c:\Users\frenc\OneDrive\Documents\GitHub\froggobot\index.js:53 if(!author.roles.cache.has('686001018277855308')) return msg.reply('you don't have the permission to do this .'); ^

TypeError:无法读取未定义的属性“缓存”

如果您需要任何其他信息,请在评论中询问。

还有一个问题,是否可以将此过程保存为单个命令并在其他文件中使用它而无需重新键入整个内容?

标签: javascriptnode.jsdiscorddiscord.js

解决方案


我的建议是,您只需尝试检查用户是否可以通过权限检查删除其他人发送的消息。

这是我要做的:

case 'clear':
    if(!msg.member.permissions.has('MANAGE_MESSAGES')) return msg.reply('you dont have the permissions to do this.');
    if(!args[1]) return msg.reply('please add the number of messages to clear in the command');

    msg.channel.bulkDelete(args[1]);
    msg.reply(args[1] + ' messages cleared!');
break;

推荐阅读