Hydra2k
Hydra2k

Reputation: 61

Discord.JS how to transfer args

I need to transfer arguments from one command to another.

Command that I need to transfer args (Order) from:

const invite = await message.channel.createInvite()
    if(cmd === `${prefix}order`){
        
    if(!args) return message.reply("Please specify what you would like to order.")        
        console.log(args)
        
        
        baseOrderNumber++;
        var order = args.join(" ")
    let orderEmbed = new Discord.MessageEmbed()
    orderIcon = "https://i.imgur.com/Le0Eist.png"
    orderEmbed.setTitle("New Order")
    orderEmbed.setColor("#FF2D00")
    orderEmbed.setThumbnail(orderIcon)
    orderEmbed.addField("Order Number", baseOrderNumber)
    orderEmbed.addField("Order", order)
    orderEmbed.addField("Customer", message.author)
    orderEmbed.addField("Server Invite", invite)

     bot.channels.cache.get('723838675489914891').send(orderEmbed)    
     let eekowo = fs.writeFileSync('orderAuthors.txt', message.author.tag, order); 
    }

Command I need to transfer to:

if(cmd === `${prefix}deliver`){
        if(!args[1]) message.reply("Please provide an order number.")
        let eekowo2 = fs.readFileSync('orderAuthors.txt', 'utf8')
        deliverEmbed = new Discord.MessageEmbed()
        deliverIcon = message.guild.iconURL
        deliverEmbed.addField("Invite", invite)
        deliverEmbed.addField("Customer", eekowo2)
        deliverEmbed.addField("Items", orderEmbed.order)
          message.author.send(deliverEmbed)
      }

Is this possible? and if so; how?

Upvotes: 2

Views: 477

Answers (1)

ShadowGamer3
ShadowGamer3

Reputation: 49

You have a couple options:

  1. If you are using a command handler, then you can require() the other command file, and call the function containing it's code through module.exports, and pass in args as an parameter.
  2. If you're not using a command handler, and all commands are in your index(or bot).js file, take the code of the 2nd command, and copy it into a function in the global scope, then call the function in its place in the normal part of the if statement, and the first command, passing in args as a parameter to both, in addition to all other necessary information, such as "message".
  3. Copy the code you want to run into the first command. (not recommended, but possible)

My recommendation would be to take option 1, but it appears as though you aren't using a command handler, so here's a possible application of #2:

// 1st command
const invite = await message.channel.createInvite()
if (cmd === `${prefix}order`) {

  if (!args) return message.reply("Please specify what you would like to order.")
  console.log(args)


  baseOrderNumber++;
  var order = args.join(" ")
  let orderEmbed = new Discord.MessageEmbed()
  orderIcon = "https://i.imgur.com/Le0Eist.png"
  orderEmbed.setTitle("New Order")
  orderEmbed.setColor("#FF2D00")
  orderEmbed.setThumbnail(orderIcon)
  orderEmbed.addField("Order Number", baseOrderNumber)
  orderEmbed.addField("Order", order)
  orderEmbed.addField("Customer", message.author)
  orderEmbed.addField("Server Invite", invite)

  bot.channels.cache.get('723838675489914891').send(orderEmbed)
  let eekowo = fs.writeFileSync('orderAuthors.txt', message.author.tag, order);

  Deliver(message, Discord, args);
}


// 2nd command
if (cmd === `${prefix}deliver`) {
  Deliver(message, Discord, args);
}




// In the global scope
function Deliver(message, embed, args) {
  if (!args[1]) message.reply("Please provide an order number.")
  //  ^ If this is an error check, you may want to put return here, before the reply
  let eekowo2 = fs.readFileSync('orderAuthors.txt', 'utf8')
  deliverEmbed = new Discord.MessageEmbed()
  deliverIcon = message.guild.iconURL
  deliverEmbed.addField("Invite", invite)
  deliverEmbed.addField("Customer", eekowo2)
  deliverEmbed.addField("Items", orderEmbed.order)
  message.author.send(deliverEmbed)
}
}

Upvotes: 1

Related Questions