File size: 1,381 Bytes
af5a34b |
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 |
/**
* cq码转换Msg
* @param {*} cq
*/
function CQToMsg(cq) {
let msg = [];
let matches = cq.matchAll(/(\[CQ:(.*?),(.*?)\]|.)/gs);
let text = '';
for (const match of matches) {
if (match[2]) {
if (text) {
msg.push({
type: 'text',
data: {
text
}
});
text = '';
}
let type = match[2];
let data = {};
let pairs = match[3].split(',');
for (const pair of pairs) {
let [key, value] = pair.split('=');
data[key] = value;
}
msg.push({ type, data });
} else {
text += match[0];
}
}
if (text) {
msg.push({
type: 'text',
data: {
text
}
});
}
return msg;
}
/**
* msg转换cq码
* @param {*} msg
* @returns
*/
function MsgToCQ(msg) {
let cq = '';
for (const item of msg) {
if (item.type === 'text') {
cq += item.data.text;
} else {
let data = Object.entries(item.data).map(([key, value]) => `${key}=${value}`).join(',');
cq += `[CQ:${item.type},${data}]`;
}
}
return cq;
}
export {
CQToMsg,
MsgToCQ
} |