Javascript Discord.js v12,在执行命令后读取参数时可能出现拆分错误

Javascript Discord.js v12,在执行命令后读取参数时可能出现拆分错误,javascript,discord.js,Javascript,Discord.js,因此,我收到但找不到的错误是在空格后接受任何参数作为有效命令。我相信这可能是一个.split()错误,因为如果您完全匹配参数,它将产生不同的输出。现在,如果使用未列出的参数,它仍将生成原始命令,!qa=!qa软体动物 当参数通过但不存在,但不存在时,它应该返回一个错误。 以下是我的索引以及与复制相关的所有内容: const fs = require('fs'); const Discord = require('discord.js'); const { prefix, token } = r

因此,我收到但找不到的错误是在空格后接受任何参数作为有效命令。我相信这可能是一个
.split()
错误,因为如果您完全匹配参数,它将产生不同的输出。现在,如果使用未列出的参数,它仍将生成原始命令,
!qa
=
!qa软体动物

当参数通过但不存在,但不存在时,它应该返回一个错误。 以下是我的索引以及与复制相关的所有内容:

const fs = require('fs');
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');

const featureFiles = fs.readdirSync('./commands/features').filter(file => file.endsWith('.js'));
for (const file of featureFiles) {
    const command = require(`./commands/features/${file}`);
    client.commands.set(command.name, command);
}
    
client.on('message', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;
//.trim() is removed, see notes below on why
    const args = message.content.slice(prefix.length).split(/ +/g);
    const commandName = args.shift().toLowerCase();

    const command = client.commands.get(commandName)
        || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));

    if (!command) return;

    if (command.guildOnly && message.channel.type !== 'text') {
        return message.reply('That command cannot be used inside a DM');
    }

    if (command.args && !args.length) {
        let reply = `You didn't provide any arguments!`;

        if (command.usage) {
            reply += `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
        }

        return message.channel.send(reply);
    }

    try {
        command.execute(message, client, args);
    } catch (error) {
        console.error(error);
        message.channel.send('Error trying to execute command');
    }});
client.login(token);
我删除了
.trim()
,因为它正在读取前缀和命令名之间的空格,这是我不想要的,因此可以在前缀和命令之间使用100个空格,它将执行它。 这是我正在构建的模块:

const Discord = require('discord.js');

module.exports ={
  name: 'qa',
  description: 'Find members who have the QA role and search by specialty.',
  usage: '[OptionalArg]',
  execute(message, client, args) {
    if(message.channel.type === 'dm')  {
      message.channel.send('Command can\'t be used here')
    }

    try{

      let roleID = "738530035526402100";
      let membersWithRole = message.guild.roles.cache.get(roleID).members.map(m=>m.user.tag).join('\n');

      const embed = new Discord.MessageEmbed()
      .setColor('#008798')
      .setTitle('Qualified Advice')
      .setDescription(`${membersWithRole}`)
      .setTimestamp(new Date)

      const testing = new Discord.MessageEmbed()
      .setColor('#008798')
      .setTitle('Test QA')
      .setDescription(`test`)
      .setTimestamp(new Date)

      const data =[embed];

      if (args[0] === 'test') {
        return message.channel.send(testing)
      }
      message.channel.send(data, 'To see focus types, type `!qa [arg]`, example `!qa test`', {split: true});
    } catch(e) {
      console.log(e)
    }
  },
};
我认为它可以在
.split()
中找到,对吗?这让我很困惑,也许我忽略了它,它对没有任何参数的常规命令也做了同样的事情。这让我相信这在
索引中。如果进行了其他未指定为arg的输入(如
?qa-alksjdkalsjd
),我希望它只返回。
Discord.js=v12

您要做的是重新构建您的if,如下所示:

if(args.length === 0) { // this runs everytime there are no args provided
    return message.channel.send(data, 'To see focus types, type `!qa [arg]`, example `!qa test`', { split: true });
}
if (args[0] === 'test') {
    return message.channel.send(testing)
}
/* ........... */
return message.channel.send(error); // send a errror here if the args were not handled by the above cases

这成功了!虽然我做了修改,所以它会发送后,但这样一个简单的错误对我来说,谢谢
if(args.length==0){message.channel.send(data,{split:true}).then(message.channel.send('要查看焦点类型,请键入
!qa[arg]
,示例
!qa test
);}
这并不适用于所有命令,但无论是否使用args,错误仍然存在,例如一个简单的?info命令,它只在这个模块中起作用,并且以与上面应用的格式完全相同的方式将它应用到其他带有args的模块中,仍然允许在该命令之后进行其他输入。我不太理解上面两条注释的意思,请您澄清一下好吗?另外,请将此答案标记为已接受答案,因为它回答了您的问题,如果其他问题不起作用,请创建一个新问题。