forked from rabbitkiller-dev/message_bridge_rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
151 lines (145 loc) · 5.02 KB
/
server.ts
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
import * as http from 'http';
import { markdownEngine as markdown, htmlTag } from 'discord-markdown';
const server = http.createServer(async (req, res) => {
console.log('ok');
const url = req.url?.split('?')[0];
const bodyStr = await new Promise<string>((res, rej) => {
let bodyStr = '';
req.on('data', (chunk) => {
bodyStr += chunk
})
req.on('end', () => {
res(bodyStr);
})
})
console.log(bodyStr);
if (url === '/parse-discord-markdown') {
const data = parseDiscrodMarkdown(bodyStr);
console.log(data);
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(data));
return;
}
})
function parseDiscrodMarkdown(message: string): any {
const ast = markdown.parserFor({
atDC: bridgeRule.atDC,
atQQ: bridgeRule.atQQ,
atKHL: bridgeRule.atKHL,
DiscordAtUser: bridgeRule.DiscordAtUser,
DiscordAtEveryone: bridgeRule.DiscordAtEveryone,
DiscordAtHere: bridgeRule.DiscordAtHere,
DiscordEmoji: bridgeRule.DiscordEmoji,
Plain: bridgeRule.Plain,
})(message) as Array<{ type: 'DiscordAtUser' | 'DiscordEmoji', [prop: string]: any }>;
return ast;
}
export const bridgeRule = {
atDC: {
order: 0,
match: source => /^@\[DC\] [^\n]+?#\d\d\d\d/.exec(source),
parse: function (capture, parse, state) {
console.log(capture);
return { type: 'At', username: capture[0] };
},
html: function (node, output, state) {
return '{{atDc}}';
},
},
atKHL: {
order: 0,
match: source => /^@\[KHL\] ([^\n#]+)#(\d\d\d\d)/.exec(source),
parse: function (capture, parse, state) {
console.log(capture);
return { type: 'At', source: 'KHL', username: capture[1], discriminator: capture[2] };
},
html: function (node, output, state) {
return '{{atDc}}';
},
},
atQQ: {
order: 0,
match: source => /^@\[QQ\] [^\n]+?\([0-9]+\)/.exec(source),
parse: function (capture, parse, state) {
console.log(capture);
return { type: 'At', username: capture[0] };
},
html: function (node, output, state) {
return '{{atDc}}';
},
},
Plain: Object.assign({}, markdown.defaultRules.text, {
match: source => /^[\s\S]+?(?=[^0-9A-Za-z\s\u00c0-\uffff-]|\n\n|\n|\w+:\S|$)/.exec(source),
parse: function (capture, parse, state) {
return { type: 'Plain', text: capture[0] };
},
html: function (node, output, state) {
if (state.escapeHTML)
return markdown.sanitizeText(node.content);
return node.content;
},
}),
DiscordAtUser: {
order: markdown.defaultRules.strong.order,
match: source => /^<@!?([0-9]*)>/.exec(source),
parse: function (capture) {
return {
id: capture[1]
};
},
html: function (node, output, state) {
return htmlTag('span', state.discordCallback.user(node), { class: 'd-mention d-user' }, state);
}
},
DiscordAtEveryone: {
order: markdown.defaultRules.strong.order,
match: source => /^@everyone/.exec(source),
parse: function () {
return { };
},
html: function (node, output, state) {
return htmlTag('span', state.discordCallback.everyone(node), { class: 'd-mention d-user' }, state);
},
},
DiscordAtHere: {
order: markdown.defaultRules.strong.order,
match: source => /^@here/.exec(source),
parse: function () {
return { };
},
html: function (node, output, state) {
return htmlTag('span', state.discordCallback.here(node), { class: 'd-mention d-user' }, state);
}
},
DiscordEmoji: {
order: markdown.defaultRules.strong.order,
match: source => /^<(a?):(\w+):(\d+)>/.exec(source),
parse: function (capture) {
return {
animated: capture[1] === "a",
name: capture[2],
id: capture[3],
};
},
html: function (node, output, state) {
return htmlTag('img', '', {
class: `d-emoji${node.animated ? ' d-emoji-animated' : ''}`,
src: `https://cdn.discordapp.com/emojis/${node.id}.${node.animated ? 'gif' : 'png'}`,
alt: `:${node.name}:`
}, false, state);
}
},
khlEveryone: {
order: markdown.defaultRules.strong.order,
match: source => /\(met\)all\(met\)/.exec(source),
parse: function () {
return { type: 'AtAll' };
},
html: function (node, output, state) {
return htmlTag('span', state.discordCallback.everyone(node), { class: 'd-mention d-user' }, state);
},
},
};
parseDiscrodMarkdown(`@[DC] 6uopdong#4700
!绑定 qq 1261972160 asd`);
server.listen(3000)