Space in command

So I want to allow a user to use spaces in a command. Like: buildwall, build wall, Build wall, build Wall, etc.
So I know how to allow the user to type with big letters but how do I allow them to use spaces?

You can check if the value is “buildwall”.

But before that you have pass the string though a regex that deletes all spaces (/\s/g for example), then String .toLowerCase() so it detects buildwall, build wall, Build wall, and build Wall.

You can use a regular expression:

message.content = message.content || "";

const buildWallRegex = /^build ?wall/gi;
if (buildWallRegex.test(message.content))
{
  const argString = message.content.replace(buildWallRegex, "").trim()
}

Explanation:

  • // are used instead of quotes to put around a regex
  • ^build means “build at start of string”: the caret sign denotes the beginning of the string
  • ? means optional space - the ? means optional, and it’s modifying the space in front of it.
  • gi - global and case insensitive flags

I have this:
let args = message.content.split(" ");
let command = message.content.toLowerCase().split(" ")[0];

Should I edit or add anything? When I use a space in a command it doesn’t work

Here’s an example of how you can make a command handler that allows spaces in commands:


// Define commands:
let commands = [
	
	{
		name: "helloworld",
		trigger: /^hellow ?world/gi, // helloworld|hello world
		async run (message, ...args)
		{
			await message.reply("Hello World.");
		}
	},
	
	{
		name: "buildwall",
		trigger: /^build ?wall/gi, // buildwall|build wall
		async run (message, ...args)
		{
			await message.reply("Building a wall.");
		}
	},
	
	{
		name: "sayhello",
		trigger: /^say ?hello/gi, //sayhello|say hello
		async run (message, ...args)
		{
			await message.reply("Hello!");
		}
	}
	
];

// Listen for messages.
bot.on("message", (message) => {
	
	// Run an async function.
	(async () => {
		
		// Make sure that the message actually has content,
		// and that it was not sent by another bot.
		if (!message.content || message.author.bot) return;
		
		// The command prefix.
		const prefix = /^\!/;
		
		// Make sure that the message contains the prefix.
		if (!prefix.test(message.content)) return;
		
		// Remove the prefix from the message.
		const content = message.content.replace(prefix, "").trim();
		
		// Loop through each command.
		for (let command of commands)
		{
			
			// Check if the message contains a trigger.
			if (command.trigger.test(content))
			{
				
				// Remove the trigger from the message,
				// and split the message into arguments.
				const args = content.replace(command.trigger, "").trim().split(/\s+/);
				
				// Run the command.
				await command.run(message, ...args);
				
				// Return as there is nothing more to do.
				return;
			}
		}
	})().catch(console.error);
});