【问题标题】:Discord.js v12, potential split error reading args after command and executing itDiscord.js v12,潜在的拆分错误,在命令后读取 args 并执行它
【发布时间】:2020-09-10 04:09:35
【问题描述】:

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

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

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

【问题讨论】:

    标签: javascript discord.js


    【解决方案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
    

    【讨论】:

    • 这成功了!虽然我确实进行了修改,所以它会在之后发送,但我的错误如此简单,谢谢! if (args.length === 0) { message.channel.send(data, {split: true}).then(message.channel.send('To see focus types, type !qa [arg], example !qa test')); }
    • 虽然这不适用于所有命令,但无论有没有 args,错误仍然存​​在,例如简单的 ?info 命令它仅在此模块中有效,并通过 args 将其应用于其他模块,在与上面应用的格式完全相同,仍然允许在命令之后进行其他输入。
    • 我不太明白你说的上面两个cmets是什么意思,你能澄清一下吗?
    • 另外请将此答案标记为已接受,因为它回答了您的问题,如果其他问题不起作用,请创建一个新问题。
    猜你喜欢
    • 1970-01-01
    • 2021-02-27
    • 2021-06-01
    • 2020-12-08
    • 2021-01-01
    • 2021-02-18
    • 1970-01-01
    • 2021-01-18
    • 2021-04-14
    相关资源
    最近更新 更多