如何解决我正在尝试为Discord机器人做出具有角色权限的明确命令 Discord.js
我已经有一些代码,但是我将如何实现一些其他代码以使该命令仅由具有MANAGE_MESSAGES权限的用户访问?
我自己尝试做的事情:
else if (message.content.startsWith(`${prefix}clear`)) {
const amount = parseInt(args[0]);;
if (isNaN(amount)) {
return message.reply('that doesn\'t seem to be a valid number.');
} else if (amount <= 0 || amount > 100) {
return message.reply('you need to input a number between 1 and 100.');
}
message.channel.bulkDelete(amount,true).catch(err => {
console.error(err);
message.channel.send('Uh oh! Something went wrong!');
}).catch(() => {
if (!message.member.hasPermission(['MANAGE_MESSAGES'])) {
message.reply("you do not have permission to use this command!");
}
});
}
最后没有多余的地方:
else if (message.content.startsWith(`${prefix}clear`)) {
const amount = parseInt(args[0]);;
if (isNaN(amount)) {
return message.reply('that doesn\'t seem to be a valid number.');
} else if (amount <= 0 || amount > 100) {
return message.reply('you need to input a number between 1 and 100.');
}
message.channel.bulkDelete(amount,true).catch(err => {
console.error(err);
message.channel.send('Uh oh! Something went wrong!');
});
}
解决方法
尝试一下:
else if (message.content.startsWith(`${prefix}clear`)) {
// put this at the very top
if (!message.member.hasPermission("MANAGE_MESSAGES")) {
return message.reply("you do not have permission to use this command!");
const amount = parseInt(args[0]);
if (isNaN(amount))
return message.reply("that doesn't seem to be a valid number.");
if (amount <= 0 || amount > 100)
return message.reply("you need to input a number between 1 and 100.");
message.channel
.bulkDelete(amount,true)
.catch((err) => {
console.error(err);
message.channel.send("Uh oh! Something went wrong!");
})
.catch((err) => console.log(err));
}
}
我认为问题在于,如果该成员没有所需的权限,您就无法return
,因此您的代码只能继续正常运行。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。