0
votes

Starting to think this truly is a .split() error, it applies to all of my commands, with or without arguments. Thought I had found a solution but it was not it. Here's a simple ping command: enter image description here

Here's my index.js:

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

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 the !ping command:

const {prefix} = require('../../config.json')

module.exports = {
    name: 'ping',
    description: 'Shows latency',
    usage: ' ',
    execute(message) {
        if (message.content.startsWith(prefix + "ping")) {
            var ping = Date.now() - message.createdTimestamp + " ms";
            message.channel.send(`${Date.now() - message.createdTimestamp}` + " ms");
        
        }
    },
};

Any way to get rid of this problem of it still executing with other input after a space across all of my commands would be great, as I simply want it to return; past whatever args or command name I specify. I am stumped. I'm wanting to think this is caused by .split() in my index.js.

1
I don't quite understand what you're asking for, do you mean that if the command has no args, and if they run the command with args, you want to return? - Mineko Kayui
Fairly close, some commands have args, like if (args[0] === 'test') {do something}, and if someone inputs !test test arg1 arg2, it will still execute "do something" even though there's "arg1 arg2" afterwards. Practically I want it to not execute, or return; if there's "arg1" or so forth that's not specified in the if statement, but also throughout regular commands like the !ping example shown above. Hopefully this helps, thanks :) - gerapallo
Ah, I see I'll get on with the answer then - Mineko Kayui

1 Answers

0
votes

You might want to check if it has args, and how many args it has, and compare it to the args in the args of the command. This might require your command args to exist and be an array, otherwise it won't work, try the following code:


index.js:

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

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;
  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) {
    // If no arguments were provided, then...

    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);
  } else if (command.args && (command.args.length > args.length || commands.args.length < args.length)) {
    // If the `args` is too many or too little, then...

    let reply = `You inserted either too many or too little 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);

ping.js:

const {prefix} = require('../../config.json')

module.exports = {
  name: 'ping',
  description: 'Shows latency',
  args: [], // Array, because we need it's length, you can also add to it, for example
            // the args required is `args_1`, then add `["args_1"]`, for 2 args, add it
            // to the array `["args_1", "args_2"]`
  usage: ' ',
  execute(message, client, args) { // If you don't, this might return an error
    if (message.content.startsWith(prefix + "ping")) {
      var ping = Date.now() - message.createdTimestamp + " ms";
      message.channel.send(`${Date.now() - message.createdTimestamp}` + " ms");
    }
  },
};

For more about this question, check out these references: