命令冷却 discord.js

时间:2021-02-17 21:22:59

标签: discord.js

我一直在努力更新我当前的命令冷却时间,当更新我的 message.js 文件时,冷却时间没有显示。

这是命令冷却文件中的当前代码

const botconfig  = require("../../botconfig.json");
const fs = require("fs")
const Discord = require('discord.js')
const cooldowns = new Map()
module.exports = async (bot, message) => { 
    if(message.author.bot || message.channel.type === "dm") return;
  let prefixes = JSON.parse(fs.readFileSync("././prefixes.json", "utf8"))
  
  if(!prefixes[message.guild.id]){
    prefixes[message.guild.id] = {
      prefixes: botconfig.prefix
    };
  }
  
  let prefix = prefixes[message.guild.id].prefixes;

    let args = message.content.slice(prefix.length).trim().split(/ +/g);
    let cmd = args.shift().toLowerCase();

    if(!message.content.startsWith(prefix)) return;
    let commandfile = bot.commands.get(cmd) || bot.commands.get(bot.aliases.get(cmd))
    if(commandfile) commandfile.run(bot, message, args)

    if(!cooldowns.has(commandfile.name)){
      cooldowns.set(commandfile.name, new Discord.Collection());
    }

    const current_time = Date.now();
    const time_stamps = cooldowns.get(commandfile.name);
    const cooldown_amount = (commandfile.cooldown) * 1000;

    if(time_stamps.has(message.author.id)){
      const experation_time = time_stamps.get(message.author.id) + cooldown_amount;
      if (current_time < experation_time){
        const time_left = (experation_time - current_time) / 1000;
        return message.reply(`Please wait ${time_left.toFixed(1)} more seconds before using this command!`)
      }
    }

    time_stamps.set(message.author.id, current_time);
    setTimeout(() => time_stamps.delete(message.author.id), cooldown_amount);
    
}

在我的命令文件中

module.exports = {
  config:{
    name: "beg",
    aliases: [],
    cooldown: 5, // current cooldown in seconds
    category: "currency",
    description: "Gets you money",
    usage: "[command | alias]",
    accessableby: "Members"
  },

当我使用该命令时,该命令将起作用,但冷却时间不起作用,让我再次使用该命令。难道我做错了什么?我还想知道如果我停止运行机器人进行更新是否会保存冷却时间,例如我使用命令“!!daily”然后重新设置机器人进行更新,当它返回时 24 小时没有过去在线冷却保存并不断计数。

2 个答案:

答案 0 :(得分:0)

冷却时间应该是 Discord.Collection

const cooldowns = new Discord.Collection(); //that goes on top of your script

我第一次在这里评论,希望对你有帮助。

答案 1 :(得分:0)

这是一个顺序问题:

首先调用 if(commandfile) commandfile.run(bot, message, args),然后只有检查冷却时间是否存在。您需要像这样重新排列代码,将 .run 向下移动。

const botconfig  = require("../../botconfig.json");
const fs = require("fs")
const Discord = require('discord.js')
const cooldowns = new Discord.Collection();
// While this does not matter, we make a Collection to for consistentcy
module.exports = async (bot, message) => { 
    if(message.author.bot || message.channel.type === "dm") return;
    let prefixes = JSON.parse(fs.readFileSync("././prefixes.json", "utf8"))

    if(!prefixes[message.guild.id]){
            prefixes[message.guild.id] = {
                    prefixes: botconfig.prefix
            };
    }
  
    let prefix = prefixes[message.guild.id].prefixes;

    let args = message.content.slice(prefix.length).trim().split(/ +/g);
    let cmd = args.shift().toLowerCase();

    if(!message.content.startsWith(prefix)) return;
    let commandfile = bot.commands.get(cmd) || bot.commands.get(bot.aliases.get(cmd));
    if(!commandfile) return; 
    // return if no command Found, if one is found, proceed to check for cooldown

    if(!cooldowns.has(commandfile.name)){
      cooldowns.set(commandfile.name, new Discord.Collection());
    }

    const current_time = Date.now();
    const time_stamps = cooldowns.get(commandfile.name);
    const cooldown_amount = (commandfile.cooldown) * 1000;

    if(time_stamps.has(message.author.id)){
            const experation_time = time_stamps.get(message.author.id) + cooldown_amount;
            if (current_time < experation_time){
                    const time_left = (experation_time - current_time) / 1000;
                    return message.reply(`Please wait ${time_left.toFixed(1)} more seconds before using this command!`);
            }
    }
    // above here is the return, if the cooldown is still active, if so it returns with the message
    time_stamps.set(message.author.id, current_time);
    // here we set the time_stamp and now can run the command
    commandfile.run(bot, message, args);
}