簡體   English   中英

如何將此 javascript 代碼轉換為 function?

[英]How do I turn this javascript code into a function?

代碼 -

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

每次運行命令時時間都不會更新,但是如果我將其放入 function 中,它會。 如何將這段代碼變成 function?

您的date變量在導出的run function 之外,只需將其移動到run function 內部即可使其在調用時“重新計算”。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM