码桶

发现社区成员的开源项目

奶狗 /

ng-webot

公开
main
index.js1.6 KB
/**
 * 内置插件:关键词回复 / 固定指令回复
 * --------------------------------------------------
 * 规则存储在 plugin_rules 表(按 bot_id 隔离):
 *   type = 'keyword'  -> 消息包含 match 关键词即回复 reply
 *   type = 'command'   -> 消息精确等于(或以前缀开头)match 指令即回复 reply
 * 安装该插件后,在「规则配置」里添加规则即可。
 */
const db = require('../../lib/db');

module.exports = {
  meta: {
    id: 'reply',
    name: '关键词/指令回复',
    version: '1.0.0',
    author: '奶狗',
    category: '消息处理',
    description: '根据配置的关键词或指令,自动回复固定内容。',
    entry: 'reply/index.js',
  },

  async onMessage(msg, ctx) {
    const botId = ctx.bot.id;
    const text = (msg.content || '').trim();
    if (!text) return;

    const rules = await db.rows(
      "SELECT type, match, reply FROM plugin_rules WHERE bot_id=? AND enabled=1",
      [botId]
    );
    if (!rules.length) return;

    for (const r of rules) {
      let hit = false;
      if (r.type === 'command') {
        // 指令:精确匹配或以 match 开头(支持带参数 "指令 参数")
        hit = (text === r.match) || text.startsWith(r.match + ' ') || text === r.match;
      } else {
        // 关键词:包含即命中
        hit = text.includes(r.match);
      }
      if (hit && r.reply) {
        await ctx.sendText(r.reply);
        return true; // 命中后停止后续规则
      }
    }
    return false;
  },
};