gerapallo
gerapallo

Reputation: 35

Discord.js v12, potential split error reading args after command and executing it

So the error I'm receiving but cannot find is accepting any arguments as a valid command after a space. I believe this might be a .split() error, as if you match the arguments exactly it will produce a different output. Now if you use an argument that is not listed, it will still produce the original command, !qa = !qa mollusk

enter image description here

It should return an error, when an argument passes but is not present, but does not do so. Here is my index and everything associated for replication:

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);

I removed .trim() as it was reading whitespaces in between the prefix and command name, which I did not want, so one could use 100 spaces between the prefix and command, and it will execute it. Here is my module I am building:

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)
    }
  },
};

Am I right for thinking it's found in .split()? This has me stumped, maybe I'm overlooking it, it also does the same thing with regular commands that do not have any arguments. This has lead to believe me this is in the index. I want it to simply return if other input is made (like ?qa alksjdkalsjd) that is not specified as an arg. Discord.js = v12

Upvotes: 1

Views: 195

Answers (1)

PLASMA chicken
PLASMA chicken

Reputation: 2785

What you want to do is restructure your if like this:

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

Upvotes: 1

Related Questions