codeduck
codeduck

Reputation: 51

is there a way to include spaces in the command? discord.js

I want my command to include a space, for example

!example command (note the space)

But that doesn't seem to work, and I usually have to just do

!example_command instead.

This would be my command handler, but spaces don't work.

if(command === 'example_command'){
  client.commands.get('example command').execute(message, args);

In a separate file I would have this:

module.exports = {
  name: 'example_command',
  description: 'an example.',
  execute(message, args){
    message.channel.send('response here!');
  }
}

Upvotes: 0

Views: 1189

Answers (2)

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23161

A quick and dirty solution would be to check if the first two words are a valid command and if not, the first word is one.

Check the code below:

// example.js
module.exports = {
  name: 'example',
  description: 'An example!',
  execute(message, args) {
    message.channel.send('The one-word command worked... 🎉');
    message.channel.send(`The args are: \`[${args.join(', ')}]\``);
  },
};
// example-command.js
module.exports = {
  name: 'example command',
  description: 'Example only!',
  execute(message, args) {
    message.channel.send('The command with a space worked... 🎉');
    message.channel.send(`The args are: \`[${args.join(', ')}]\``);
  },
};
// index.js
const Discord = require('discord.js');
const fs = require('fs');

const client = new Discord.Client();
const prefix = '!';
const { TOKEN } = process.env;

client.commands = new Discord.Collection();

const commandFiles = fs
  .readdirSync('./src/cmds')
  .filter((file) => file.endsWith('.js'));

for (const file of commandFiles) {
  const command = require(`./cmds/${file}`);
  client.commands.set(command.name, command);
}

client.on('message', (message) => {
  if (!message.content.startsWith(prefix) || message.author.bot) return;

  let args = message.content.slice(prefix.length).trim().split(/ +/);
  let commandWithoutSpace = args.shift().toLowerCase();
  let commandWithSpace = `${commandWithoutSpace} ${args[0]}`;
  let command = null;

  // check with the space first
  if (client.commands.has(commandWithSpace)) {
    command = commandWithSpace;
    // chop the first item of args as it's part of the command
    args.shift();
  } else if (client.commands.has(commandWithoutSpace)) {
    command = commandWithoutSpace;
  }

  if (!command) return;

  try {
    client.commands.get(command).execute(message, args);
  } catch (error) {
    console.error(error);
    message.reply('there was an error trying to execute that command!');
  }
});

client.login(TOKEN);

client.once('ready', () => {
  console.log('Ready!');
});

enter image description here

Upvotes: 1

AnanthDev
AnanthDev

Reputation: 1808

This is because your command arguments is an array of words, in this command !example command the arguments would be ['example', 'command'] and you're checking for commands based on the first element of the array, if you followed the code from djs guide, the arguments are added to the array with each space as a breakpoint.

So the solution to your question would be

const args = message.content.substring(prefix.length).split(" "); //replace it with your prefix
//args is an array of worlds excluding prefix
if(args[0].toLowerCase() === 'example' && args[1].toLowerCase() == 'command'){
        client.commands.get('example_command').execute(message, args);

Upvotes: 0

Related Questions