【发布时间】:2020-03-18 23:27:52
【问题描述】:
我正在为 Discord 制作一个机器人。要在用户键入命令时加载命令,我需要验证命令是否存在,但现在我有三倍几乎相同的 if 语句。有没有办法只用一个表达式来获取所有内容?
if (!client.commands[cat]) return;
if (client.commands[cat][command]) {
if (client.commands[cat][command].conf) {
if (client.elevationManager.calculateGuildElevation(message.author.id, message.guild) < client.commands[cat][command].conf.elevation) return message.reply(`you don't have the required elevation to use this command.`);
if (client.commands[cat][command].conf.accountRequired)
if (!(await client.accountsManager.findById(message.author.id))) return message.reply(`you must have an account to use this command ! Type \`,,account create\` to get one.`);
return client.commands[cat][command].run(client, args, message);
} else if (!args[0]) {
if (client.elevationManager.calculateGuildElevation(message.author.id, message.guild) < client.commands[cat][command].conf.elevation) return message.reply(`you don't have the required elevation to use this command.`);
if (client.commands[cat][command].conf.accountRequired)
if (!(await client.accountsManager.findById(message.author.id))) return message.reply(`you must have an account to use this command ! Type \`,,account create\` to get one.`);
return client.commands[cat][command].run(client, args, message);
} else if (!client.commands[cat][args[0]]) {
if (client.elevationManager.calculateGuildElevation(message.author.id, message.guild) < client.commands[cat][command].conf.elevation) return message.reply(`you don't have the required elevation to use this command.`);
if (client.commands[cat][command].conf.accountRequired)
if (!(await client.accountsManager.findById(message.author.id))) return message.reply(`you must have an account to use this command ! Type \`,,account create\` to get one.`);
return client.commands[cat][command].run(client, args, message);
} else { ...
【问题讨论】:
-
顺便说一句,第二个 if 没有必要,因为第一个返回另一个状态
-
哦,没错,你
标签: javascript node.js discord.js