File size: 3,514 Bytes
481eba0
 
 
0800168
02fc7f7
481eba0
0800168
 
af5a34b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
481eba0
 
 
 
 
 
 
0800168
481eba0
 
af5a34b
 
 
 
 
 
 
 
 
0800168
af5a34b
 
 
 
 
481eba0
af5a34b
 
 
 
 
 
 
 
 
481eba0
 
af5a34b
 
 
481eba0
126f9dd
af5a34b
 
 
 
 
0800168
 
af5a34b
0800168
481eba0
af5a34b
 
0800168
481eba0
0800168
481eba0
0800168
481eba0
 
0800168
af5a34b
 
481eba0
 
 
af5a34b
 
481eba0
 
0800168
 
481eba0
af5a34b
 
02fc7f7
0800168
 
 
 
 
 
 
 
 
481eba0
 
02fc7f7
481eba0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
let Common
try {
  Common = (await import("#miao")).Common
} catch {}

const stateArr = {}
const SymbolTimeout = Symbol("Timeout")
const SymbolResolve = Symbol("Resolve")

export default class plugin {
  /**
   * @param name 插件名称
   * @param dsc 插件描述
   * @param handler handler配置
   * @param handler.key handler支持的事件key
   * @param handler.fn handler的处理func
   * @param namespace namespace,设置handler时建议设置
   * @param event 执行事件,默认message
   * @param priority 优先级,数字越小优先级越高
   * @param rule
   * @param rule.reg 命令正则
   * @param rule.fnc 命令执行方法
   * @param rule.event 执行事件,默认message
   * @param rule.log  false时不显示执行日志
   * @param rule.permission 权限 master,owner,admin,all
   * @param task
   * @param task.name 定时任务名称
   * @param task.cron 定时任务cron表达式
   * @param task.fnc 定时任务方法名
   * @param task.log  false时不显示执行日志
   */
  constructor({
    name = "your-plugin",
    dsc = "无",
    handler,
    namespace,
    event = "message",
    priority = 5000,
    task = { name: "", fnc: "", cron: "" },
    rule = []
  }) {
    /** 插件名称 */
    this.name = name
    /** 插件描述 */
    this.dsc = dsc
    /** 监听事件,默认message https://oicqjs.github.io/oicq/#events */
    this.event = event
    /** 优先级 */
    this.priority = priority
    /** 定时任务,可以是数组 */
    this.task = task
    /** 命令规则 */
    this.rule = rule

    if (handler) {
      this.handler = handler
      this.namespace = namespace || ""
    }
  }

  /**
   * @param msg 发送的消息
   * @param quote 是否引用回复
   * @param data.recallMsg 群聊是否撤回消息,0-120秒,0不撤回
   * @param data.at 是否at用户
   */
  reply(msg = "", quote = false, data = {}) {
    if (!this.e?.reply || !msg) return false
    return this.e.reply(msg, quote, data)
  }

  conKey(isGroup = false) {
    return `${this.name}.${this.self_id || this.e.self_id}.${isGroup ? (this.group_id || this.e.group_id) : (this.user_id || this.e.user_id)}`
  }

  /**
   * @param type 执行方法
   * @param isGroup 是否群聊
   * @param time 操作时间
   * @param timeout 操作超时回复
   */
  setContext(type, isGroup, time = 120, timeout = "操作超时已取消") {
    const key = this.conKey(isGroup)
    if (!stateArr[key]) stateArr[key] = {}
    stateArr[key][type] = this.e
    if (time) stateArr[key][type][SymbolTimeout] = setTimeout(() => {
      if (stateArr[key][type]) {
        const resolve = stateArr[key][type][SymbolResolve]
        delete stateArr[key][type]
        resolve ? resolve(false) : this.reply(timeout, true)
      }
    }, time * 1000)
    return stateArr[key][type]
  }

  getContext(type, isGroup) {
    if (type) return stateArr[this.conKey(isGroup)]?.[type]
    return stateArr[this.conKey(isGroup)]
  }

  finish(type, isGroup) {
    const key = this.conKey(isGroup)
    if (stateArr[key]?.[type]) {
      clearTimeout(stateArr[key][type][SymbolTimeout])
      delete stateArr[key][type]
    }
  }

  awaitContext(...args) {
    return new Promise(resolve => this.setContext("resolveContext", ...args)[SymbolResolve] = resolve)
  }

  resolveContext(context) {
    this.finish("resolveContext")
    context[SymbolResolve](this.e)
  }

  async renderImg(plugin, tpl, data, cfg) {
    return Common.render(plugin, tpl, data, { ...cfg, e: this.e })
  }
}