[英]How do I only allow a command to be toggled by users with certain permissions?
I have coded a bot that can format text - I need it so that not just randoms can access the command.我编写了一个可以格式化文本的机器人 - 我需要它以便不仅仅是随机数可以访问命令。 Here is my code - it throws no errors
这是我的代码 - 它不会引发任何错误
try {
if(message.member.guild.me.hasPermission('ADMINISTRATOR')) {
if (args[0] === 'off') {
christian[message.guild.id] = false
} else if (args[0] === 'on') {
christian[message.guild.id] = true
}
message.channel.send(`Christian Mode ${christian[message.guild.id] ? 'Active' : 'Inactive'}`)
return christian
} else {
message.channel.send('You do not have permission to use that command!')
}
} catch (error) {
console.log('Permission Error')
}
note: the command is toggled back to the index.js (main) by saying =Christian on/off this function works.注意:通过说 =Christian on/off 这个函数有效,命令被切换回 index.js (main)。
message.member.guild.me.hasPermission('ADMINISTRATOR')
is checking the bot's permissions, not the command executor's. message.member.guild.me.hasPermission('ADMINISTRATOR')
正在检查机器人的权限,而不是命令执行者的权限。 Call GuildMember#hasPermission()
on message.member
(see Message#member
).在
message.member
上调用GuildMember#hasPermission()
(参见Message#member
)。
For example...例如...
if (!message.member.hasPermission('ADMINISTRATOR')) {
// return an error to the user (and actually return)
}
// continue with command code
NOTE: The Discord.js documentation hyperlinked is for recently released v12.注意: Discord.js 文档超链接适用于最近发布的 v12。 If your Discord.js isn't up to date, switch to the correct version at the top of the page for accurate info.
如果您的 Discord.js 不是最新的,请切换到页面顶部的正确版本以获取准确信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.