discord.js - 命令权限
问题描述
我正在寻找开发个人网络机器人,我需要一个简单的“mod”、“admin”和其他审核命令。我已经完成了命令,我只需要弄清楚如何发送错误并在成员没有“admin”、“mod”或其他审核角色时不执行命令。这是我到目前为止所拥有的:
if (!message.member.roles.some(r=>[Tester].includes(r.name)) )
return message.channel.send("error_here").catch(console.error);
我希望代码包含一条错误消息:
message.channel.send(message.author + " you do not have permission to use this command!")
我还希望在成员没有“admin”、“mod”或其他审核角色时不执行该命令。
解决方案
我认为您走在正确的轨道上,只需添加一些代码即可进行额外验证。此代码检查用户是否具有任何审核角色,如果是,则执行该命令。
// Just some test role names
let modRoles = ['admin', 'moderator', 'helper'];
if (command === '<Some mod/admin command here>') {
if (!message.member.roles.some(r=>modRoles.includes(r.name))) {
// User does not have any moderation roles. User is not allowed to execute command
return message.channel.send(message.author + ' you do not have permission to execute this command!');
.catch(console.error);
}
// User does have a moderation role. User can execute command
// Write code here for the command
}
推荐阅读
- mongodb - 如何制作与时间相关的 mongodb 视图管道?
- json - 将 instagram 位置 ID 转换为纬度和经度,以便我可以将它们覆盖在 Google 地图上
- javascript - javascript中的新Effect.toggle(参数)?
- python - 如何使用beautifulsoup 抓取手机号码
- c# - 如何为从未登录过的新创建的 Windows 用户加载注册表配置单元?
- php - 使用 DataFire 的 JS 表单,如何使其重定向或仅发送成功?
- r - 我有一个数据框,其中有一列是向量。我想获取最后一个元素并将其存储在一个新列中。我该怎么做?
- reactjs - 反应 - 错误:尝试导入错误/应用程序'不包含默认导出(导入为'应用程序'
- ios - 如何构建包含 Mac Catalyst 的 Fat Framework?
- elasticsearch - ElasticSearch 6.7 无痛,如何访问嵌套文档