KuryLok
KuryLok

Reputation: 21

Use a name for prefix discord.js

how can I use the bot name as a space prefix to use a command?

 let prefix = 'test '; //botconfig.prefix;
  let messageArray = message.content.split(" ")
  let cmd = messageArray[0].toLowerCase();
  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);

Upvotes: 0

Views: 137

Answers (1)

SomePerson
SomePerson

Reputation: 1309

Sounds like this?

You want to use the bot username as the prefix, so you can use message.client.user.

Then for your arguments, I tweaked it a little.

This is how each variable indexes the argument.

//messageArray
bot do this
0   1  2
//args
bot do this
n/a 0  1
//n/a, because args sliced that element out.

It also looks like messageArray just exists for the sole purpose of helping args, so you can make your code this:

  let prefix = message.client.user.username.toLowerCase();
  let args = message.content.split(/\s+/).slice(1);
  if(!message.content.toLowerCase().startsWith(prefix)) return;
  let commandfile = bot.commands.get(args[0].toLowerCase()) || 
  bot.commands.get(bot.aliases.get(args[0].toLowerCase()));
  if(commandfile) commandfile.run(bot, message, args);

I added some toLowerCases because if the bot finds something like BOT, and it does if("BOT".startsWith("bot"), it returns false, basically so the command isn't case sensitive.

Make sure your bot name doesn't have spaces, or you may have to change the indexes a little bit, or remove spaces from the username.

You can always refer to the docs here.

Upvotes: 1

Related Questions