Discord.JS如何传递args

时间:2020-06-22 01:57:52

标签: javascript node.js discord discord.js

我需要将参数从一个命令传递到另一个命令。

我需要从以下命令转移args(命令)的命令:

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); 
    }

我需要转移到的命令:

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)
      }

这可能吗?如果是这样;怎么样?

1 个答案:

答案 0 :(得分:1)

您有两种选择:

  1. 如果使用命令处理程序,则可以require()其他命令文件,并通过module.exports调用包含其代码的函数,并将args作为参数传递。
  2. 如果您不使用命令处理程序,并且所有命令都在index(或bot).js文件中,请获取第二条命令的代码,然后将其复制到全局范围内的函数中,然后调用函数在if语句和第一个命令的常规部分中代替其位置,除了所有其他必要的信息(例如“消息”)之外,还将args作为参数传递给这两个参数。
  3. 将要运行的代码复制到第一个命令中。 (不推荐,但可以)

我的建议是采用选项1,但看起来好像您没有使用命令处理程序,因此这是#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)
}
}