如何将此javascript代码转换为函数?

时间:2019-10-06 16:40:04

标签: javascript function discord.js

代码-

const Discord = require("discord.js")
const db = require('quick.db')
var today = new Date();
var date = today.getFullYear()+""+(today.getMonth()+1)+today.getDate()+""+ today.getHours()+""+ today.getMinutes()+"" + today.getSeconds()+"" + today.getMilliseconds();

exports.run = async (client, message, args) => {
  
 
db.set(`ticket_${message.author.id}`, date);
  
  let yes = await db.fetch( `ticket_${message.author.id}`, date) || 'error'
  
  message.channel.send(`${yes}`)

每次运行命令的时间都不会更新,但是如果我将其放入函数中,它将可以。如何将这段代码转换为函数?

1 个答案:

答案 0 :(得分:0)

您的date变量位于导出的run函数外部,只需将其移至run函数内部,即可在调用时对其进行“重新计算”。

const Discord = require("discord.js")
const db = require('quick.db')

exports.run = async (client, message, args) => {    
    var today = new Date();
    var date = today.getFullYear()+""+(today.getMonth()+1)+today.getDate()+""+today.getHours()+""+ today.getMinutes()+"" + today.getSeconds()+"" + today.getMilliseconds();

    db.set(`ticket_${message.author.id}`, date);

      let yes = await db.fetch( `ticket_${message.author.id}`, date) || 'error'

      message.channel.send(`${yes}`)