forked from hczhcz/telegram-kuso-bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.oreo.js
96 lines (77 loc) · 2.62 KB
/
plugin.oreo.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
'use strict';
const canvas = require('canvas');
const config = require('./config');
module.exports = (bot, event, playerEvent, env) => {
const images = {};
// cache images
for (const i in config.oreoImageMap) {
const item = config.oreoImageMap[i];
if (item.front) {
canvas.loadImage('oreo/' + item.front).then((image) => {
images[item.front] = image;
});
}
if (item.back) {
canvas.loadImage('oreo/' + item.back).then((image) => {
images[item.back] = image;
});
}
}
bot.onText(/^\/((?:o|re|and)+)(@\w+)?$/, event((msg, match) => {
const items = [];
let offset = 0;
let width = 0;
let height = 0;
for (let i = 0; i < match[1].length && items.length < config.oreoMaxLength;) {
const head = match[1].slice(i);
let done = false;
for (const j in config.oreoImageMap) {
if (head.startsWith(j)) {
const item = config.oreoImageMap[j];
if (items.length) {
items.push([item.back, offset, item.width, item.height]);
} else {
items.push([item.front, offset, item.width, item.height]);
}
width = Math.max(width, item.width);
height = Math.max(height, offset + item.height);
offset += item.delta;
i += j.length;
done = true;
break;
}
}
if (!done) {
throw Error(JSON.stringify(match[1]));
}
}
if (width > 0 && height > 0) {
const image = canvas.createCanvas(width, height);
const ctx = image.getContext('2d');
items.reverse();
for (const i in items) {
if (items[i][0]) {
ctx.drawImage(
images[items[i][0]],
(width - items[i][2]) / 2,
items[i][1],
items[i][2],
items[i][3]
);
}
}
bot.sendPhoto(
msg.chat.id,
image.toBuffer(),
{
reply_to_message_id: msg.message_id,
}
);
}
}, 2));
env.info.addPluginHelp(
'oreo',
'/oreo 生成奥利奥图片\n'
+ '/oreandoreoreore 生成奥利和奥利奥利奥利图片'
);
};