-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathplugin.sticker.js
115 lines (99 loc) · 3.32 KB
/
plugin.sticker.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
114
115
'use strict';
const canvas = require('canvas');
const cwebp = require('cwebp');
const config = require('./config');
module.exports = (bot, event, playerEvent, env) => {
const stickerEvent = event((msg, match) => {
if (msg.sticker.is_animated) {
bot.sendDocument(
msg.chat.id,
bot.getFileStream(msg.sticker.file_id),
{},
{
filename: msg.sticker.file_id + '.gz',
contentType: 'application/gzip',
}
);
} else if (msg.sticker.is_video) {
bot.sendDocument(
msg.chat.id,
bot.getFileStream(msg.sticker.file_id),
{},
{
filename: msg.sticker.file_id + '.webm',
contentType: 'video/webm',
}
);
} else {
const decoder = new cwebp.DWebp(bot.getFileStream(msg.sticker.file_id));
decoder.toBuffer((err, buffer) => {
bot.sendDocument(
msg.chat.id,
buffer,
{},
{
filename: msg.sticker.file_id + '.png',
contentType: 'image/png',
}
);
});
}
}, -1);
const animationEvent = event((msg, match) => {
bot.sendDocument(
msg.chat.id,
bot.getFileStream(msg.animation.file_id),
{},
{
filename: msg.animation.file_id + '.m4v',
contentType: 'video/mp4',
}
);
}, -1);
const imageEvent = event((msg, match) => {
let bestWidth = 0;
let fileId = msg.document && msg.document.file_id;
for (const i in msg.photo) {
if (bestWidth < msg.photo[i].width) {
bestWidth = msg.photo[i].width;
fileId = msg.photo[i].file_id;
}
}
bot.getFileLink(fileId).then((link) => {
canvas.loadImage(link).then((bgImage) => {
const size = Math.max(bgImage.width, bgImage.height);
const image = canvas.createCanvas(bgImage.width * 512 / size, bgImage.height * 512 / size);
const ctx = image.getContext('2d');
ctx.drawImage(bgImage, 0, 0, image.width, image.height);
bot.sendDocument(
msg.chat.id,
image.toBuffer(),
{},
{
filename: fileId + '.png',
contentType: 'image/png',
}
);
});
});
}, -1);
bot.on('message', (msg) => {
if (config.ban[msg.from.id]) {
return;
}
if (msg.chat.id === msg.from.id && msg.sticker) {
stickerEvent(msg, []);
}
if (msg.chat.id === msg.from.id && msg.animation) {
animationEvent(msg, []);
}
if (msg.chat.id === msg.from.id && (msg.document || msg.photo)) {
imageEvent(msg, []);
}
});
env.info.addPluginHelp(
'sticker',
'<sticker> (私聊)获取表情文件\n'
+ '<image> (私聊)转换为表情文件'
);
};