javascript - 检查某人是否具有 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:无法读取未定义的属性“缓存”
如果您需要任何其他信息,请在评论中询问。
还有一个问题,是否可以将此过程保存为单个命令并在其他文件中使用它而无需重新键入整个内容?
解决方案
我的建议是,您只需尝试检查用户是否可以通过权限检查删除其他人发送的消息。
这是我要做的:
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;
推荐阅读
- bash - Mac OS 终端脚本文件变量 (.command)
- c++ - MSVC++ 2019 中的内存分配
- kubernetes - 基于 Istio 入口网关子域路由
- dictionary - 地图的奇怪行为(可以为空的表达式不能用作条件)
- reactjs - 该项目未从 react-leaflet-markercluster 中完全删除
- sql - 我们可以在部署 SQL DACPAC 文件时忽略架构级别过滤吗?
- qt - 在 Qt 中知道布局名称时查找布局
- vba - 在 Powerpoint VBA 中,如何让嵌入的视频开始播放?
- python - Python:在列表中标记重复项
- python - conda create --copy 正在使用符号链接创建环境