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

Discord JS命令独立冷却实现问题求助

解决Discord.js机器人命令冷却共享问题

你的问题出在冷却存储的键只用到了用户ID,导致所有命令共用同一条冷却记录。要实现每个命令独立冷却,只需要把冷却存储的键改成用户ID+命令名的组合,让每个用户的每个命令都有单独的冷却条目。

修改后的代码示例:

// 假设used是你之前定义的Map,比如const used = new Map();
if (command === 'ферма1') {
    // 用用户ID+命令名作为唯一键
    const cooldownKey = `${message.author.id}-${command}`;
    const cooldown = used.get(cooldownKey);
    if (cooldown) {
        const remaining = Duration(cooldown - Date.now(), {units: ["m", "s"], round: true});
        message.channel.send({
            embeds: [new EmbedBuilder()
                .setDescription(`Осталось ждать ${remaining}`)
                .setColor(0x0099FF)]
        });
    } else {
        const roles = message.member.roles.cache;
        if (!roles.some((role) => role.name === 'Плохая ферма')) {
            return message.channel.send({
                embeds: [new EmbedBuilder()
                    .setDescription('У тебя нет плохой фермы.')
                    .setColor(0x0099FF)]
            });
        }
        
        message.channel.send({
            embeds: [new EmbedBuilder()
                .setDescription('Ты получил 30000 евро за слабую ферму.')
                .setColor(0x0099FF)]
        });
        unb.editUserBalance(guildID, userID, { cash: +30000 });
        // 用组合键存储冷却时间
        used.set(cooldownKey, Date.now() + 3600000);
        setTimeout(() => used.delete(cooldownKey), 3600000);
    }
}    
if (command === 'ферма2') {
    // 同样使用用户ID+命令名作为键
    const cooldownKey = `${message.author.id}-${command}`;
    const cooldown = used.get(cooldownKey);
    if (cooldown) {
        const remaining = Duration(cooldown - Date.now(), {units: ["m", "s"], round: true});
        message.channel.send({
            embeds: [new EmbedBuilder()
                .setDescription(`Осталось ждать ${remaining}`)
                .setColor(0x0099FF)]
        });
    } else {
        const roles = message.member.roles.cache;
        if (!roles.some((role) => role.name === 'Нормальная ферма')) {
            return message.channel.send({
                embeds: [new EmbedBuilder()
                    .setDescription('У тебя нет нормальной фермы.')
                    .setColor(0x0099FF)]
            });
        }
        message.channel.send({
            embeds: [new EmbedBuilder()
                .setDescription('Ты получил 50000 евро за нормальную ферму.')
                .setColor(0x0099FF)]
        });
        unb.editUserBalance(guildID, userID, { cash: +50000 });
        used.set(cooldownKey, Date.now() + 3600000);
        setTimeout(() => used.delete(cooldownKey), 3600000);
    }
}

核心修改点:

  • 每个命令内定义cooldownKey = ${message.author.id}-${command}``,将用户与命令绑定为唯一标识
  • 所有操作used Map的方法(get/set/delete)都替换为使用这个组合键

修改后,每个命令的冷却状态完全独立,执行ферма1不会影响ферма2的冷却,反之亦然。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 19:15:33