javascript - Discord.js Arguments With Spaces



So I'm trying to create my bot and I want there to be a command which adds information to a sqlite database

if (command === "addplayer") {
  let [name, crew, rank, weapon, df, talent, profession, other] = args;
  if(!name) return"Name argument is required!");
  let id = name.toLowerCase();
  if(!crew) {let crew = "Blank";}
  if(!rank) {let rank = "Blank";}
  if(!weapon) {let weapon = "Blank";}
  if(!df) {let df = "Blank";}
  if(!talent) {let talent = "Blank";}
  if(!profession) {let profession = "Blank";}
  if(!other) {let other = "Blank";}"INSERT INTO players (id, Name, Crew, Rank, Weapon, DF, 
Talent, Profession, Other) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", [id, 
name, crew, rank, weapon, df, talent, profession, other]);

All I want to know really is how to add like a space key to the arguments so I can do

>addplayer info with space, more info, even more, and so on

And when I do the other command it lists it out like this:

Info1: info with space Info2: more info Info3: even more Info4: and so on

2 Answers: 

Ok, so using your sample command as an example, let's say someone enters this, and you want to know how to handle it:

>addplayer jimmybenoit, Example Crew, Captain, Example Longsword, Example Fruit, Gainer, Cook, Other Information Goes here

I assume you have already figured out how to check the command prefix, and split the command from the args, but I'll show how I would do that below.

// get message content from user
let input = message.content;

// get command prefix ">"
//  -> probably check that this is valid, if you haven't done that already
let prefix = input[0];

// get command (1 word)
let command = input.substr(1).split(' ')[0];

// get args - looks like you've already done this
let args = command.substr( command.indexOf(' ') + 1 );

// Check if command is valid, as you have in your code
if (command === "addplayer") {
    // split args into an array, comma delimited
    // -> also map .trim() function to remove beginning / ending spaces
    args = args.split(',').map(elem => elem.trim());

    // split args into vars
    let [name, crew, rank, weapon, df, talent, profession, ...other] = args;

    // .. do the rest of your code

The key here, is you use .split(',') to turn the args string into an array, split on ',' character (and use .trim() to clear out whitespace from each element, THEN you assign all of your variables to the args array.

The final ...other] is used to catch any remaining arguments - so if your text 'Other information Goes here' had commas, all of those extra arguments would get stored (as an array) in the other variable. If you want to put them all back together, you can use .join(',') to separate them by comma.

Let me know if you have any questions!


I've just decided to go with multiple commands like

>edit EXAMPLE and then doing something like

>editname EXAMPLE1 setting the name to EXAMPLE1 instead of EXAMPLE