koishi-plugin-codec-tools 1.0.9 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +42 -51
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -14,87 +14,78 @@ const path_1 = __importDefault(require("path"));
|
|
|
14
14
|
const yaml_1 = __importDefault(require("yaml"));
|
|
15
15
|
exports.name = 'codec-tools';
|
|
16
16
|
exports.using = ['i18n'];
|
|
17
|
-
exports.Config = koishi_1.Schema.object({})
|
|
18
|
-
|
|
19
|
-
'
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
exports.Config = koishi_1.Schema.object({}).i18n({ 'zh-CN': {} });
|
|
18
|
+
async function getImageUrl(session) {
|
|
19
|
+
console.log('session.elements:', JSON.stringify(session.elements, null, 2));
|
|
20
|
+
let imageUrl = null;
|
|
21
|
+
session.elements.forEach(el => {
|
|
22
|
+
if (el.type === 'image' && el.attrs?.src) {
|
|
23
|
+
imageUrl = el.attrs.src;
|
|
24
|
+
console.log('找到当前消息图片:', imageUrl);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
if (imageUrl)
|
|
28
|
+
return imageUrl;
|
|
22
29
|
if (session.quote) {
|
|
30
|
+
console.log('尝试获取引用消息:', session.quote.messageId);
|
|
23
31
|
try {
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
if (
|
|
28
|
-
|
|
32
|
+
const msg = await session.bot.getMessage(session.channelId, session.quote.messageId);
|
|
33
|
+
console.log('引用消息内容:', JSON.stringify(msg.elements, null, 2));
|
|
34
|
+
msg.elements.forEach(el => {
|
|
35
|
+
if (el.type === 'image' && el.attrs?.src) {
|
|
36
|
+
imageUrl = el.attrs.src;
|
|
37
|
+
console.log('找到引用消息图片:', imageUrl);
|
|
29
38
|
}
|
|
30
|
-
}
|
|
39
|
+
});
|
|
31
40
|
}
|
|
32
41
|
catch (e) {
|
|
33
|
-
console.
|
|
42
|
+
console.error('获取引用消息失败:', e);
|
|
34
43
|
}
|
|
35
44
|
}
|
|
36
|
-
|
|
37
|
-
if (currentImage) {
|
|
38
|
-
return currentImage.attrs.src;
|
|
39
|
-
}
|
|
40
|
-
return null;
|
|
45
|
+
return imageUrl;
|
|
41
46
|
}
|
|
42
47
|
function apply(ctx, config) {
|
|
43
48
|
const zhCNPath = path_1.default.join(__dirname, './locales/zh-CN.yml');
|
|
44
|
-
const
|
|
45
|
-
const zhCN = yaml_1.default.parse(zhCNContent);
|
|
49
|
+
const zhCN = yaml_1.default.parse(fs_1.default.readFileSync(zhCNPath, 'utf8'));
|
|
46
50
|
ctx.i18n.define('zh-CN', zhCN);
|
|
47
|
-
ctx.command('url-encode <text:text>')
|
|
48
|
-
|
|
49
|
-
ctx.command('
|
|
50
|
-
|
|
51
|
-
ctx.command('
|
|
52
|
-
|
|
53
|
-
ctx.command('
|
|
54
|
-
.action((_, text) => Buffer.from(text, 'base64').toString());
|
|
55
|
-
ctx.command('unicode-encode <text:text>')
|
|
56
|
-
.action((_, text) => text.split('').map(c => '\\u' + c.charCodeAt(0).toString(16).padStart(4, '0')).join(''));
|
|
57
|
-
ctx.command('unicode-decode <text:text>')
|
|
58
|
-
.action((_, text) => text.replace(/\\u([0-9a-fA-F]{4})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16))));
|
|
59
|
-
ctx.command('decode-code')
|
|
60
|
-
.action(async ({ session }) => {
|
|
51
|
+
ctx.command('url-encode <text:text>').action((_, text) => encodeURIComponent(text));
|
|
52
|
+
ctx.command('url-decode <text:text>').action((_, text) => decodeURIComponent(text));
|
|
53
|
+
ctx.command('base64-encode <text:text>').action((_, text) => Buffer.from(text).toString('base64'));
|
|
54
|
+
ctx.command('base64-decode <text:text>').action((_, text) => Buffer.from(text, 'base64').toString());
|
|
55
|
+
ctx.command('unicode-encode <text:text>').action((_, text) => text.split('').map(c => '\\u' + c.charCodeAt(0).toString(16).padStart(4, '0')).join(''));
|
|
56
|
+
ctx.command('unicode-decode <text:text>').action((_, text) => text.replace(/\\u([0-9a-fA-F]{4})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16))));
|
|
57
|
+
ctx.command('decode-code').action(async ({ session }) => {
|
|
61
58
|
if (!session)
|
|
62
|
-
return
|
|
63
|
-
const imageUrl = await
|
|
59
|
+
return;
|
|
60
|
+
const imageUrl = await getImageUrl(session);
|
|
61
|
+
console.log('最终获取到的图片URL:', imageUrl);
|
|
64
62
|
if (!imageUrl) {
|
|
65
|
-
|
|
63
|
+
await session.send(session.text('.no-image'), { quote: session.messageId });
|
|
64
|
+
return;
|
|
66
65
|
}
|
|
67
66
|
try {
|
|
68
67
|
const buffer = await ctx.http.get(imageUrl, { responseType: 'arraybuffer' });
|
|
69
68
|
const img = await jimp_1.default.read(Buffer.from(buffer));
|
|
70
|
-
if (img.bitmap.width > 1000 || img.bitmap.height > 1000)
|
|
69
|
+
if (img.bitmap.width > 1000 || img.bitmap.height > 1000)
|
|
71
70
|
img.scaleToFit(800, 800);
|
|
72
|
-
}
|
|
73
71
|
img.contrast(0.2);
|
|
74
|
-
const imageData = {
|
|
75
|
-
data: new Uint8ClampedArray(img.bitmap.data),
|
|
76
|
-
width: img.bitmap.width,
|
|
77
|
-
height: img.bitmap.height
|
|
78
|
-
};
|
|
72
|
+
const imageData = { data: new Uint8ClampedArray(img.bitmap.data), width: img.bitmap.width, height: img.bitmap.height };
|
|
79
73
|
const qrResult = (0, jsqr_1.default)(imageData.data, imageData.width, imageData.height);
|
|
80
74
|
if (qrResult) {
|
|
81
|
-
|
|
75
|
+
await session.send(session.text('.qr-content', { content: qrResult.data }), { quote: session.messageId });
|
|
76
|
+
return;
|
|
82
77
|
}
|
|
83
78
|
const hints = new Map();
|
|
84
|
-
hints.set(library_1.DecodeHintType.POSSIBLE_FORMATS, [
|
|
85
|
-
library_1.BarcodeFormat.QR_CODE, library_1.BarcodeFormat.CODE_128, library_1.BarcodeFormat.EAN_13,
|
|
86
|
-
library_1.BarcodeFormat.EAN_8, library_1.BarcodeFormat.UPC_A, library_1.BarcodeFormat.UPC_E,
|
|
87
|
-
library_1.BarcodeFormat.CODE_39, library_1.BarcodeFormat.ITF
|
|
88
|
-
]);
|
|
79
|
+
hints.set(library_1.DecodeHintType.POSSIBLE_FORMATS, [library_1.BarcodeFormat.QR_CODE, library_1.BarcodeFormat.CODE_128, library_1.BarcodeFormat.EAN_13, library_1.BarcodeFormat.EAN_8, library_1.BarcodeFormat.UPC_A, library_1.BarcodeFormat.UPC_E, library_1.BarcodeFormat.CODE_39, library_1.BarcodeFormat.ITF]);
|
|
89
80
|
const reader = new library_1.MultiFormatReader();
|
|
90
81
|
const luminanceSource = new library_1.RGBLuminanceSource(imageData.data, imageData.width, imageData.height);
|
|
91
82
|
const binaryBitmap = new library_1.BinaryBitmap(new library_1.HybridBinarizer(luminanceSource));
|
|
92
83
|
const barcodeResult = reader.decode(binaryBitmap, hints);
|
|
93
|
-
|
|
84
|
+
await session.send(session.text('.barcode-content', { content: barcodeResult.text }), { quote: session.messageId });
|
|
94
85
|
}
|
|
95
86
|
catch (e) {
|
|
96
87
|
console.error('解析失败:', e);
|
|
97
|
-
|
|
88
|
+
await session.send(session.text('.no-code'), { quote: session.messageId });
|
|
98
89
|
}
|
|
99
90
|
});
|
|
100
91
|
}
|