LUKSTTR
LUKSTTR

Reputation: 25

Adding a space to the prefix

I would like to add for example as prefix to my bot discord his name + , + a space and command to be something like

bot, weather Dubai

With

bot,weather Dubai

It`s works but with space not. This is my command run file

if(message.author.bot || message.channel.type === "dm") return;
let prefix = 'bot, ';//botconfig.prefix;
let messageArray = message.content.split(" ")
let cmd = messageArray[0].toLowerCase();
console.log(messageArray);
//let args = messageArray.slice(1);
let args = messageArray.slice(1);
if(!message.content.startsWith(prefix)) return;
let commandfile = bot.commands.get(cmd.slice(prefix.length)) || bot.commands.get(bot.aliases.get(cmd.slice(prefix.length)))
if(commandfile) commandfile.run(bot, message, args);
console.log(commandfile)

So how can I get the command to go with all that space? Is there a way to add it or/

Upvotes: 1

Views: 280

Answers (1)

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23189

You just need to remove the prefix first then split the remaining string to an array. The first element will be the command, the rest is the arguments.

Try to run the snippet below:

let message = {
  content: 'bot, weather Dubai'
}
let prefix = 'bot, '

// create an args variable that slices off the prefix and splits it into an array
let args = message.content.slice(prefix.length).split(/ +/);
// create a command variable by taking the first element in the array
// and removing it from args
let command = args.shift().toLowerCase();

console.log({
  args,
  command
})

And your code would look like this:

let prefix = 'bot, ';
if (
  message.author.bot ||
  message.channel.type === 'dm' ||
  !message.content.startsWith(prefix)
)
  return;

let args = message.content.slice(prefix.length).split(/ +/);
let command = args.shift().toLowerCase();

let commandfile =
  bot.commands.get(command) || bot.commands.get(bot.aliases.get(command));
if (commandfile) commandfile.run(bot, message, args);
console.log(commandfile);

Upvotes: 1

Related Questions