Skip to content
This repository was archived by the owner on Feb 22, 2026. It is now read-only.

Latest commit

 

History

History
73 lines (66 loc) · 1.47 KB

File metadata and controls

73 lines (66 loc) · 1.47 KB

YunzaiJS

☞ 点击阅读文档了解更多

yarn add yunzaijs

src/index.ts

import { Application, applicationOptions, useEvent } from 'yunzaijs'
import * as Apps from './apps.js'
export default () => {
  // 预先存储
  const Rules: {
    reg: RegExp | string
    key: string
  }[] = []
  // options
  return applicationOptions({
    create() {
      // created
      for (const key in Apps) {
        // 推类型
        const app: typeof Application.prototype = new Apps[key]()
        // 用  reg 和 key 连接起来。
        // 也可以进行自由排序
        for (const rule of app.rule) {
          Rules.push({
            reg: rule.reg,
            key: key
          })
        }
      }
    },
    async mounted(e) {
      // 存储
      const Data = []
      // 如果key不存在
      const Cache = {}
      // 使用event以确保得到正常类型
      await useEvent(
        e => {
          for (const Item of Rules) {
            // 匹配正则
            // 存在key
            // 第一次new
            if (
              new RegExp(Item.reg).test(e.msg) &&
              Apps[Item.key] &&
              !Cache[Item.key]
            ) {
              Cache[Item.key] = true
              Data.push(new Apps[Item.key]())
            }
          }
        },
        // 推倒为message类型的event
        [e, 'message']
      )
      // back
      return Data
    }
  })
}

src/apps/index.ts