Discord.js v12,潜在的拆分错误,在命令执行后读取args并执行它

时间:2020-09-09 16:45:29

标签: javascript discord.js

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

enter image description here

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

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()中找到了它?这让我感到很困惑,也许是我忽略了它,它对于没有任何参数的常规命令也有相同的作用。这使我相信这是index中的内容。我希望它仅在未指定为arg的其他输入(例如?qa alksjdkalsjd)时返回。 Discord.js = v12

1 个答案:

答案 0 :(得分:1)

您想要做的就是重组您的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