命令冷却 discord.js

Posted

技术标签:

【中文标题】命令冷却 discord.js【英文标题】:Command Cooldown discord.js 【发布时间】:2021-05-20 19:49:28 【问题描述】:

我一直在努力更新我当前的命令冷却时间,更新我的 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 小时没有过去在线冷却保存并不断计数。

【问题讨论】:

为什么要在封装在模块中的第一个代码中进行多项检查?为什么不将其设置为跟踪 discord.js guide recommends 等消息的默认客户端? 【参考方案1】:

Cooldown 应该是 Discord.Collection

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

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

【讨论】:

Discord.Collection 正在扩展 Map,在这种情况下它并没有增加很多重要的东西。【参考方案2】:

这是一个顺序问题:

首先你打电话给 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);

【讨论】:

以上是关于命令冷却 discord.js的主要内容,如果未能解决你的问题,请参考以下文章

命令冷却 discord.js

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

Discord Python:如何在事件下添加冷却命令

discord.py - 如何为特定用户重置命令的冷却时间?

Discord.js 命令冷却时间

我如何为 client = discord.Client() 使用 discordpy 冷却命令 [关闭]