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

为Twitch自动回复脚本添加Cooldown冷却功能的技术求助

为Twitch自动回复脚本添加冷却功能

修改需求

当观众发送包含hello的消息时,脚本自动回复Hi!,且在2-3分钟的随机冷却时间内不再因相同触发词重复发送回复,避免刷屏。

核心修改点

  1. 新增冷却时间存储对象,记录每个触发动作的最后发送时间
  2. 生成2-3分钟的随机冷却时长(120000ms ~ 180000ms)
  3. 修正原脚本中函数参数顺序错误的问题
  4. 增加冷却判断逻辑,只有当当前时间超过冷却时间时才发送回复

修改后的完整代码

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Twitch Auto-Reply with Cooldown</title>
</head>

<body>
    <script src="https://github.com/tmijs/tmi.js/releases/download/v1.8.5/tmi.min.js"></script>

    <script> 
        const client = new tmi.Client({
            options: { 
                debug: false,
                skipMembership: true, // 不接收 JOIN/PART 訊息
                skipUpdatingEmotesets: true,
            },
            connection: {
                reconnect: true,
                secure: true
            },
            identity: {
                username: 'your_twitch_username',            // [TODO]: 填入你的Twitch用户名
                password: 'generated_oath_password'         // [TODO]: 填入生成的Oauth密码
                                                             // 1. 前往Twitch授权工具生成
                                                             // 2. 点击"Connect"
                                                             // 3. 获取以"oauth..."开头的密码
                                                             // 4. 复制完整密码粘贴到此处
            },
            channels: [ 'target_channel_name' ] // [TODO]: 填入要监听的频道名称
        });

        // 存储每个触发动作的最后发送时间,支持多触发词扩展
        const cooldownRecords = {};
        // 定义冷却时间范围:2分钟(120000ms)到3分钟(180000ms)
        const MIN_COOLDOWN = 120 * 1000;
        const MAX_COOLDOWN = 180 * 1000;

        function sendWithCooldown(client, channel, message, triggerKey) {
            const now = Date.now();
            // 获取当前触发词的最后发送时间,默认0(从未发送过)
            const lastSentTime = cooldownRecords[triggerKey] || 0;
            // 生成随机冷却时长
            const cooldown = Math.floor(Math.random() * (MAX_COOLDOWN - MIN_COOLDOWN + 1)) + MIN_COOLDOWN;

            // 检查是否已过冷却时间
            if (now - lastSentTime > cooldown) {
                client.say(channel, message);
                // 更新最后发送时间
                cooldownRecords[triggerKey] = now;
                console.log(`已发送回复,冷却${cooldown/1000}秒`);
            } else {
                console.log(`触发词${triggerKey}仍在冷却中,剩余${Math.ceil((lastSentTime + cooldown - now)/1000)}秒`);
            }
        }

        client.connect().catch(console.error);

        client.on('message', (channel, tags, message, self) => {
            // 跳过自己发送的消息,避免无限循环
            if (self) return;

            // 检测包含"hello"的消息(不区分大小写)
            if (message.toLowerCase().includes("hello")) {
                // 使用"hello"作为触发键,确保相同触发词共用冷却
                sendWithCooldown(client, channel, "Hi!", "hello");
            }

            // 如需添加其他触发词,可复制上方逻辑
            // 示例:检测"goodbye"回复"See you!"
            // if (message.toLowerCase().includes("goodbye")) {
            //     sendWithCooldown(client, channel, "See you!", "goodbye");
            // }
        });
    </script>
</body>

</html>

关键说明

  • cooldownRecords对象:用来记录每个触发词的最后发送时间,方便扩展多个触发词的独立冷却
  • 随机冷却时间:通过Math.random()生成2-3分钟之间的随机值,避免固定冷却显得机械
  • triggerKey参数:将触发词作为唯一标识,确保相同触发动作共用冷却逻辑
  • 修正了原脚本中函数参数顺序错误的问题(原调用时参数顺序和定义不一致)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 01:45:08