You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.js 冷却脚本未正常执行问题求助

Discord机器人冷却脚本未生效的修复方案

问题根源

你的冷却逻辑无法正常工作的核心问题是:每次处理交互时,若当前命令还没有对应的冷却集合,你新建了一个Collection但没有把它存回全局的cooldowns集合,导致用户的冷却状态在每次请求后都会丢失,相当于根本没记录冷却。

修复后的完整代码

const { Events, Collection } = require('discord.js');
const cooldowns = new Collection();

module.exports = {
    name: Events.InteractionCreate,
    async execute(interaction) {
        if (!interaction.isChatInputCommand()) {
            console.log('Not a chat input command, skipping.');
            return;
        }

        const command = interaction.client.commands.get(interaction.commandName);

        if (!command) {
            console.error(`No command matching ${interaction.commandName} was found.`);
            return;
        }

        const now = Date.now();
        // 获取对应命令的冷却集合,不存在则新建并存入全局
        let commandCooldowns = cooldowns.get(command.data.name);
        if (!commandCooldowns) {
            commandCooldowns = new Collection();
            cooldowns.set(command.data.name, commandCooldowns);
        }
        const defaultCooldownDuration = 3;
        const cooldownAmount = (command.cooldown ?? defaultCooldownDuration) * 1000;

        if (commandCooldowns.has(interaction.user.id)) {
            const expirationTime = commandCooldowns.get(interaction.user.id) + cooldownAmount;

            if (now < expirationTime) {
                const expiredTimestamp = Math.round(expirationTime / 1000);
                return interaction.reply({ 
                    content: `请稍等,你正在执行\`${command.data.name}\`的冷却,可再次使用时间:<t:${expiredTimestamp}:R>。`, 
                    ephemeral: true 
                });
            } else {
                // 可选:冷却到期后删除记录,避免集合冗余
                commandCooldowns.delete(interaction.user.id);
            }
        }

        commandCooldowns.set(interaction.user.id, now);

        try {
            await command.execute(interaction);
        }
        catch (error) {
            console.error(`Error executing ${interaction.commandName}`);
            console.error(error);
        }
    },
};

关键改动说明

  1. 新增全局集合存储逻辑:当新建commandCooldowns后,必须通过cooldowns.set(command.data.name, commandCooldowns)将其绑定到全局集合,这样后续同命令的交互才能读取到之前的冷却记录。
  2. 可选优化:清理过期记录:当用户冷却时间到期后,主动删除其ID记录,避免commandCooldowns集合无限膨胀。

修复后的冷却核心片段

const now = Date.now();
// 获取对应命令的冷却集合,不存在则新建并存入全局
let commandCooldowns = cooldowns.get(command.data.name);
if (!commandCooldowns) {
    commandCooldowns = new Collection();
    cooldowns.set(command.data.name, commandCooldowns);
}
const defaultCooldownDuration = 3;
const cooldownAmount = (command.cooldown ?? defaultCooldownDuration) * 1000;

if (commandCooldowns.has(interaction.user.id)) {
    const expirationTime = commandCooldowns.get(interaction.user.id) + cooldownAmount;

    if (now < expirationTime) {
        const expiredTimestamp = Math.round(expirationTime / 1000);
        return interaction.reply({ 
            content: `请稍等,你正在执行\`${command.data.name}\`的冷却,可再次使用时间:<t:${expiredTimestamp}:R>。`, 
            ephemeral: true 
        });
    } else {
        commandCooldowns.delete(interaction.user.id);
    }
}

commandCooldowns.set(interaction.user.id, now);

内容的提问来源于stack exchange,提问作者NomadRedz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 07:26:28