1

how do I read args in discord.js? I am trying to create a support bot and I want to have an !help {topic} command. how do I do that? my current code is very basic

const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = ("!")
const token = ("removed")

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', msg => {
  if (msg.content === 'ping') {
    msg.reply('pong');
  }
  if (msg.content === 'help') {
    msg.reply('type -new to create a support ticket');
  }

});

client.login(token);
0

6 Answers 6

5

You can make use of a prefix and arguments like so...

const prefix = '!'; // just an example, change to whatever you want

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

  const args = message.content.trim().split(/ +/g);
  const cmd = args[0].slice(prefix.length).toLowerCase(); // case INsensitive, without prefix

  if (cmd === 'ping') message.reply('pong');

  if (cmd === 'help') {
    if (!args[1]) return message.reply('Please specify a topic.');
    if (args[2]) return message.reply('Too many arguments.');

    // command code
  }
});
1
  • 2
    Once you start to have more commands, you might better off with a switch statement than if statements for checking the commands. Or, a much better choice you can start with now, use a command handler. :)
    – slothiful
    Apr 14, 2019 at 0:26
0

you can use Switch statement instead of if (command == 'help') {} else if (command == 'ping') {}

client.on ('message', async message => {
  var prefix = "!";
  var command = message.content.slice (prefix.length).split (" ")[0],
      topic = message.content.split (" ")[1];
  switch (command) {
    case "help":
      if (!topic) return message.channel.send ('no topic bro');
      break;
    case "ping":
      message.channel.send ('pong!');
      break;
  }
});
0
let args = msg.content.split(' ');
let command = args.shift().toLowerCase();

this is the simplified answer from @slothiful.

usage

if(command == 'example'){
  if(args[0] == '1'){
  console.log('1');
 } else {
  console.log('2');
0

You can create a simple command/arguments thing (I don't know how to word it correctly)

client.on("message", message => {
  let msgArray = message.content.split(" "); // Splits the message content with space as a delimiter
  let prefix = "your prefix here";
  let command = msgArray[0].replace(prefix, ""); // Gets the first element of msgArray and removes the prefix
  let args = msgArray.slice(1); // Remove the first element of msgArray/command and this basically returns the arguments

  // Now here is where you can create your commands
  if(command === "help") {
    if(!args[0]) return message.channel.send("Please specify a topic.");
    if(args[1]) return message.channel.send("Too many arguments.");

    // do your other help command stuff...
  }
});
0

You can do

            const args =
                message.content.slice(prefix.length).trim().split(' ');
            const cmd = args.shift().toLocaleLowerCase();
0

Word of advice, use a command handler and slash commands - this will solve both the need for a help command and reading arguments. Also helps with readability.

Anyways...

message.content.split(' '): This will split your string into an array of sub-strings, then return a new array.

.shift(): This will remove the first index in the array.

Combining this will get you your arguments: const args = message.content.split(' ').shift()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.