Discord.js命令冷却+剩余时间

问题描述 投票:0回答:1

好的,我希望这样做,以便冷却时间显示用户需要等待多长时间才能再次工作。冷静的工作,但我希望它显示时间保持不变,而不是说您需要等待15分钟才能输入此命令。可能吗?

const { RichEmbed } = require("discord.js");
const { stripIndents } = require("common-tags");
const { prefix } = require("../../botconfig.json");
const db = require('quick.db')
let bal = require("../../database/balance.json");
let works = require('../../database/works.json');
const fs = require('fs');
const talkedRecently = new Set();

//Set cooldown


module.exports = {
    name: "work",
    aliases: [],
    category: "economy",
    description: "Gets you money",
    usage: "[command | alias]",
    run: async (client, message, args) => {
       if (talkedRecently.has(message.author.id)) {
 message.channel.send("You have to wait TIME minutes before you can work again")

    } else {
if(!bal[message.author.id]){
    bal[message.author.id] = {
      balance: 0
    };
  } 
  if(!works[message.author.id]) {
    works[message.author.id] = {
     work: 0
    };
  } 

  const Jwork = require('../../work.json');
  const JworkR = Jwork[Math.floor(Math.random() * Jwork.length)];
  var random = Math.floor(Math.random() * 20) + 3;
  let curBal = bal[message.author.id].balance 
  bal[message.author.id].balance = curBal + random;
  let curWork = works[message.author.id].work
  works[message.author.id].work = curWork + 1;
  fs.writeFile('././database/works.json', JSON.stringify(works, null, 2), (err) => {
    if (err) console.log(err)
    })
  fs.writeFile('././database/balance.json', JSON.stringify(bal, null, 2), (err) => {
    let embed = new RichEmbed() 
    .setColor("RANDOM") 
    .setDescription(`
    **\💼 | ${message.author.username}**, ${JworkR} 💴 **${random}**
    `) 
    message.channel.send(embed)
    if (err) console.log(err)
  });

        // Adds the user to the set so that they can't talk for a minute
        talkedRecently.add(message.author.id);
        setTimeout(() => {
          // Removes the user from the set after a minute
          talkedRecently.delete(message.author.id);
        }, 900000);
    }
}

}
node.js json command bots discord.js
1个回答
0
投票

不幸的是,您当前的系统没有任何帮助。如果要使用其冷却时间,则您不仅需要存储用户,还可以存储更多信息。

让我们为变量使用Map,以便我们可以有键值对。这将使跟踪所需信息变得更加容易]

// Replace talkedRecently's declaration with this...
const cooldowns = new Map();

要使用户处于冷却状态,请使用Map.set()添加用户以及其冷却时间应过期至Map.set()。然后,当冷却时间用尽时,请使用cooldowns以允许用户再次访问该命令。

Map.delete()

为了确定冷却时间剩余的时间,我们必须从冷却时间到期时中减去当前时间。但是,这将给我们几毫秒的时间,使我们无法读取该值。将持续时间转换为单词的简单方法是使用Map.delete() ([// Replace the talkedRecently.add(...) section with this... cooldowns.set(message.author.id, Date.now() + 900000); setTimeout(() => cooldowns.delete(message.author.id), 900000); 也是一个选项)。最后,我们可以发送所需的消息,让用户知道他们的冷却时间还剩下多少时间。

humanize-duration
© www.soinside.com 2019 - 2024. All rights reserved.