Discord.js命令冷却+剩余时间 [英] Discord.js Command Cooldown + Time Remaining

查看:58
本文介绍了Discord.js命令冷却+剩余时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我希望做到这一点,这样冷却时间就可以显示用户需要等待多长时间才能再次工作。冷却起作用了,但我希望它显示剩余的时间,而不是说您需要等待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);
    }
}

}

推荐答案

遗憾的是,您当前的系统没有任何帮助。如果要使用用户的冷却时间计时,您必须存储的不仅仅是用户。

让我们为变量使用Map,这样我们就可以拥有键-值对。这将使跟踪我们需要的信息变得更容易

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

要使用户处于冷却状态,请使用Map.set()将该用户及其冷却时间应过期的时间添加到cooldowns。然后,在冷却时间应该结束时使用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(moment也是一个选项)。最后,我们可以发送所需的消息,让用户知道他们的冷却时间还有多少。

// Put this where you require your other dependencies...
const humanizeDuration = require('humanize-duration');

// Replace the  if (talkedRecently.has(...))  part with this...
const cooldown = cooldowns.get(message.author.id);
if (cooldown) {
  const remaining = humanizeDuration(cooldown - Date.now());

  return message.channel.send(`You have to wait ${remaining} before you can work again`)
    .catch(console.error);
}

这篇关于Discord.js命令冷却+剩余时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆