onebots
Version:
基于icqq的多例oneBot实现
178 lines (177 loc) • 7.17 kB
JavaScript
"use strict";
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _Adapter_logger;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Adapter = void 0;
const events_1 = require("events");
const onebot_1 = require("./onebot");
class Adapter extends events_1.EventEmitter {
constructor(app, platform, config) {
super();
this.app = app;
this.platform = platform;
this.config = config;
this.oneBots = new Map();
_Adapter_logger.set(this, void 0);
this.on("message.receive", (uin, ...args) => {
this.getOneBot(uin).emit("message.receive", ...args);
});
this.on("notice.receive", (uin, ...args) => {
this.getOneBot(uin).emit("notice.receive", ...args);
});
this.on("request.receive", (uin, ...args) => {
this.getOneBot(uin).emit("request.receive", ...args);
});
}
materialize(content) {
return content
.replace(/&(?!(amp|#91|#93|#44);)/g, "&")
.replace(/\[/g, "[")
.replace(/]/g, "]")
.replace(/,/g, ",");
}
toCqcode(version, messageArr) {
return []
.concat(messageArr)
.map(item => {
if (typeof item === "string")
return item;
if (item.type === "text")
return item.data?.text || item.text;
let dataStr;
if (typeof item.data === "string") {
dataStr = [`data=${this.materialize(item.data)}`];
}
else {
dataStr = Object.entries(item.data || item).map(([key, value]) => {
// is Buffer
if (value instanceof Buffer)
return `${key}=${value.toString("base64")}`;
// is Object
if (value instanceof Object)
return `${key}=${JSON.stringify(value, (_, v) => (typeof v === "bigint" ? v.toString() : v))}`;
// is Array
if (value instanceof Array)
return `${key}=${value.map(v => JSON.stringify(v, (_, v) => (typeof v === "bigint" ? v.toString() : v))).join(",")}`;
// is String
return `${key}=${item.data?.[key] || item[key]}`;
});
}
return `[CQ:${item.type},${dataStr.join(",")}]`;
})
.join("");
}
fromCqcode(version, message) {
const regExpMatchArray = message.match(/\[CQ:([a-z]+),([^\]]+)]/);
if (!regExpMatchArray)
return [
{
type: "text",
data: {
text: message,
},
},
];
const result = [];
while (message.length) {
const [match] = message.match(/\[CQ:([a-z]+),([^\]]+)]/) || [];
if (!match)
break;
const prevText = message.substring(0, message.indexOf(match));
if (prevText) {
result.push({
type: "text",
data: {
text: prevText,
},
});
message = message.substring(prevText.length);
}
const [type, ...valueArr] = match.substring(1, match.length - 1).split(",");
result.push({
type: type.split(":").at(-1),
data: Object.fromEntries(valueArr.map(item => {
const [key, ...values] = item.split("=");
const value = values.join("=");
return [key, type === "reply" && key === "id" ? +value : value];
})),
});
message = message.substring(match.length);
}
if (message.length) {
result.push({
type: "text",
data: {
text: message,
},
});
}
return result;
}
transformMessage(uin, version, message) {
const onebot = this.getOneBot(uin);
const instance = onebot.instances.find(V => V.version === version);
return instance.config.post_message_format === "string"
? this.toCqcode(version, this.toSegment(version, message))
: this.toSegment(version, message);
}
getOneBot(uin) {
return this.oneBots.get(uin);
}
get logger() {
return (__classPrivateFieldSet(this, _Adapter_logger, __classPrivateFieldGet(this, _Adapter_logger, "f") || this.app.getLogger(this.platform), "f"));
}
get info() {
return {
platform: this.platform,
config: this.config,
icon: this.icon,
bots: [...this.oneBots.values()].map(bot => {
return bot.info;
}),
};
}
async setOnline(uin) { }
async setOffline(uin) { }
getLogger(uin, version) {
if (!version)
return this.app.getLogger(`${this.platform}-${uin}`);
return this.app.getLogger(`${this.platform}-${version}(${uin})`);
}
createOneBot(uin, protocol, versions) {
const oneBot = new onebot_1.OneBot(this, uin, versions);
this.oneBots.set(uin, oneBot);
return oneBot;
}
async start(uin) {
const startOneBots = [...this.oneBots.values()].filter(oneBot => {
return uin ? oneBot.uin === uin : true;
});
for (const oneBot of startOneBots) {
await oneBot.start();
}
this.app.emit("adapter.start", this.platform);
}
async stop(uin, force) {
const stopOneBots = [...this.oneBots.values()].filter(oneBot => {
return uin ? oneBot.uin === uin : true;
});
for (const oneBot of stopOneBots) {
await oneBot.stop(force);
await this.setOffline(oneBot.uin);
}
this.app.emit("adapter.stop", this.platform);
}
}
exports.Adapter = Adapter;
_Adapter_logger = new WeakMap();