forked from takayama-lily/oicq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
113 lines (102 loc) · 2.62 KB
/
util.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
/**
* 常用工具包
*/
"use strict";
const { genCQMsg } = require("./lib/message/parser");
const segment = {};
const cqcode = {};
/**
* @type {{[k: string]: string[]}}
*/
const elem_map = {
text: ["text"],
at: ["qq", "text", "dummy"],
face: ["id", "text"],
sface: ["id", "text"],
bface: ["file", "text"],
rps: ["id"],
dice: ["id"],
image: ["file", "cache", "timeout", "headers"],
flash: ["file", "cache", "timeout", "headers"],
record: ["file", "cache", "timeout", "headers"],
location: ["lat", "lng", "address", "id"],
music: ["type", "id"],
json: ["data"],
xml: ["data", "type"],
share: ["url", "title", "image", "content"],
shake: [],
poke: ["type", "id"],
reply: ["id"],
node: ["id"],
anonymous: ["ignore"],
};
for (const [type, params] of Object.entries(elem_map)) {
segment[type] = (...args) => {
const data = {};
for (let i = 0; i < params.length; ++i) {
if (Reflect.has(args, i)) {
data[params[i]] = args[i];
}
}
return {
type, data,
};
};
cqcode[type] = (...args) => {
return genCQMsg(segment[type](...args));
};
}
/**
* @param {import("./index").MessageElem | import("./index").MessageElem[]} arg
*/
segment.toCqcode = (arg) => {
if (typeof arg === "string")
return arg;
if (typeof arg[Symbol.iterator] === "function") {
let str = "";
for (let elem of arg) {
str += genCQMsg(elem);
}
return str;
} else {
return genCQMsg(arg);
}
};
// function test() {
// console.log(segment.text("aaa"));
// console.log(cqcode.text("aaa"));
// console.log(segment.image("/aaa/bbb"));
// console.log(cqcode.image("/aaa/bbb"));
// console.log(segment.image("/aaa/bbb",1));
// console.log(cqcode.image("/aaa/bbb",true));
// console.log(segment.music("qq",123));
// console.log(cqcode.music("163"));
// console.log(segment.json({"a": 1}));
// console.log(cqcode.json("{\"a\": 1}"));
// console.log(segment.toCqcode({
// type: "at",
// data: {
// qq: "all",
// text: "@全体成员"
// }
// }));
// console.log(segment.toCqcode([
// {
// type: "at",
// data: {
// qq: "all",
// text: "@全体成员"
// }
// },
// {
// type: "image",
// data: {
// file: "[123456&&&,,,]"
// }
// },
// ]));
// }
// test();
module.exports = {
segment, cqcode,
};