-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
182 lines (163 loc) · 5.92 KB
/
index.js
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import log4js from "log4js";
import _ from "lodash";
import fs from "fs";
import path from "path";
import { loadFileAsJson, writeFileSync, writeConfigSync } from "./lib/file-system.js";
import { parseArgs } from "./lib/parse-args.js";
import { Hooks } from "./lib/hooks.js";
import { createClient } from "oicq";
const hookBus = new Hooks();
const botInfo = loadFileAsJson("data/account.json");
let botEnv = loadFileAsJson("data/bot-env.json");
const bot = createClient(botInfo.account, {
platform: 5
})
// 日志记录配置
log4js.configure({
appenders: {
fileout: {
type: 'dateFile',
//文件名 = filename + pattern, 设置为alwaysIncludePattern:true
filename: './logs/log',
pattern: 'yyyy-MM-dd.log',
// compress: true,
//包含模型
alwaysIncludePattern: true,
}
},
categories: {
default: { appenders: ["fileout"], level: "info" }
}
});
bot.on("system.login.qrcode", function (e) {
this.logger.mark("扫码后按Enter完成登录") //通过扫码二维码登录
process.stdin.once("data", () => {
this.login()
})
}).on("system.login.error", function (e) {
if (e.code < 0)
this.login()
}).login()
/* =========== Plugins Load =========== */
bot.once("system.online", async function (e) {
await loadPlugin(); // 加载插件 读取插件列表并更新bot环境中插件列表
botEnv = loadFileAsJson("data/bot-env.json");
// 初始化黑名单
if (loadFileAsJson("data/config/blacklist.json") == null)
writeConfigSync("blacklist.json", "[]");
// 相关插件初始化
onCreate(bot);
});
// 群消息处理
bot.on("message.group.normal", async function (e) {
// 处理错误信息并汇报给主人
await onMessage(e).catch(err => {
this.logger.error(err);
this.sendPrivateMsg(botInfo["owner"], err.message);
})
})
// 私聊消息处理
bot.on("message.private", async function (e) {
// 处理错误信息并汇报给主人
await onMessage(e).catch(err => {
this.logger.error(err);
this.sendPrivateMsg(botInfo["owner"], err.message);
})
})
// 群通知类插件
bot.on("notice.group", async function (e) {
// 处理错误信息并汇报给主人
await onNotice(e).catch(err => {
this.logger.error(err);
this.sendPrivateMsg(botInfo["owner"], err.message);
})
})
function onCreate(bot) {
hookBus.invoke('onCreate', bot);
}
let messageDic = {};
async function onMessage(data) {
let cmd = "";
let rawArgs = "";
let flag = "";
let rawMsg = (data.raw_message + "").trim();
if (botEnv.cmdFlag.indexOf(rawMsg[0]) !== -1 || botEnv.adminCmdFlag.indexOf(rawMsg[0]) !== -1) {
flag = rawMsg.slice(0, 1);
if (rawMsg.indexOf(" ") === -1) {
cmd = rawMsg.slice(1);
} else {
cmd = rawMsg.slice(1, rawMsg.indexOf(" "));
rawArgs = rawMsg.slice(rawMsg.indexOf(" ") + 1);
}
}
let options = { "unknown": (err) => { return `未知参数${err}` } };
for (const cmdd in botEnv.configPath) {
if (cmdd.split("|").indexOf(cmd) !== -1) {
options = loadFileAsJson(botEnv["configPath"][cmdd]).options;
options["unknown"] = (err) => { return `未知参数${err}` };
break;
}
}
const args = parseArgs(rawArgs.split(" "), options);
// 记录用户消息对应的bot消息id
function recordMid(func) {
return function (content, quote = false) {
let result = func.call(this, content, quote);
result.then(res => {
messageDic[data.message_id] = res.message_id;
})
return result;
}
}
data.reply = recordMid(data.reply);
await hookBus.invokePromise('onMessage', { "bot": bot, "data": data, "flag": flag, "cmd": cmd, "rawArgs": rawArgs, "args": args })
.catch(err => { throw err });
}
async function onNotice(data) {
data["messageDic"] = messageDic;
await hookBus.invokePromise('onNotice', { "bot": bot, "data": data })
.catch(err => { throw err });
}
function hook(name, fn) {
hookBus.add(name, fn);
}
async function loadPlugin() {
let num = 0;
let pluginsList = [];
let cmdList = [];
let configPath = [];
let isOn = [];
let plugins = fs.readdirSync(path.resolve('plugins'))
.filter(p => !p.startsWith("_"));
for (const plugin of plugins) {
let pluginEntry = fs.readdirSync(path.resolve(`plugins/${plugin}`))
.filter(item => /index.js/.test(item));
if (pluginEntry.length == 0) {
let subPluginEntry = fs.readdirSync(path.resolve(`plugins/${plugin}`))
.filter(item => !(/config.json/.test(item)))
.filter(item => !item.startsWith("_"));
for (const sub of subPluginEntry) {
cmdList.push(loadFileAsJson(`./plugins/${plugin}/${sub}/config.json`).cmd.join("|"));
configPath.push(`./plugins/${plugin}/${sub}/config.json`);
let p = await import(`./plugins/${plugin}/${sub}/index.js`);
p.apply(hook);
}
} else {
cmdList.push(loadFileAsJson(`./plugins/${plugin}/config.json`).cmd.join("|"));
configPath.push(`./plugins/${plugin}/config.json`);
let p = await import(`./plugins/${plugin}/${pluginEntry[0]}`);
p.apply(hook);
}
num++;
let cfg = loadFileAsJson(`plugins/${plugin}/config.json`);
if (cfg !== null) {
if (cfg?.pluginName !== "") pluginsList.push(cfg.pluginName)
else pluginsList.push(plugin);
isOn.push(cfg.default);
}
}
console.log(`[INFO] 已成功加载${num}个插件`);
botEnv["pluginsList"] = _.zipObject(pluginsList, isOn);
botEnv["configPath"] = _.zipObject(cmdList, configPath);
writeFileSync("data/bot-env.json", JSON.stringify(botEnv, null, '\t'), true);
}