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

为Twitch机器人实现触发词的多形式匹配

修改Twitch机器人触发匹配规则

修改后的代码

// 记录上次发送帮助消息的时间
let lastHelpTime = 0

client.on('message', (channel, tags, message, self) => {
    // 将消息转为小写,实现大小写不敏感匹配
    const lowerCaseMessage = message.toLowerCase();
    // 判断消息是否包含任意大小写形式的!help,支持带后续内容的消息
    const send = lowerCaseMessage.includes('!help');

    if (!send ) return;

    const timeNow = new Date().getTime()
    // 检查是否距离上次发送已超过1分钟
    if (timeNow - lastHelpTime > 60 * 1000) {
        lastHelpTime = timeNow
        client.say(channel, `This is the help message`)
    }

    console.log(`${tags['display-name']}: ${message}`);
});

关键修改说明

  • 大小写不敏感处理:通过message.toLowerCase()将用户输入的消息统一转为小写,不管输入!HELP、!HeLp还是!help,都会被识别为相同的触发指令。
  • 支持包含触发词的消息:使用includes('!help')替代原有的精确匹配,只要消息中出现!help(任意大小写),比如!help bot chat这类带后续内容的消息,都会触发逻辑。
    • 如果需要限制为仅以!help开头的消息(避免消息中间出现!help也触发),可以将includes('!help')替换为startsWith('!help')。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 18:15:01