koishi-plugin-codec-tools 1.0.8 → 1.1.0

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.
Files changed (2) hide show
  1. package/lib/index.js +40 -61
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -14,91 +14,70 @@ 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
- .i18n({
19
- 'zh-CN': {},
20
- });
21
- async function extractImageFromSession(session) {
17
+ exports.Config = koishi_1.Schema.object({}).i18n({ 'zh-CN': {} });
18
+ async function getImageUrl(session) {
19
+ let imageUrl = null;
20
+ session.elements.forEach(el => {
21
+ if (el.type === 'image' && el.attrs?.src) {
22
+ imageUrl = el.attrs.src;
23
+ }
24
+ });
25
+ if (imageUrl)
26
+ return imageUrl;
22
27
  if (session.quote) {
23
28
  try {
24
- const quotedMsg = await session.bot.getMessage(session.channelId, session.quote.messageId);
25
- if (quotedMsg && quotedMsg.elements) {
26
- const quotedImage = quotedMsg.elements.find(el => el.type === 'image');
27
- if (quotedImage) {
28
- return quotedImage.attrs.src;
29
+ const msg = await session.bot.getMessage(session.channelId, session.quote.messageId);
30
+ msg.elements.forEach(el => {
31
+ if (el.type === 'image' && el.attrs?.src) {
32
+ imageUrl = el.attrs.src;
29
33
  }
30
- }
31
- }
32
- catch (e) {
33
- console.warn('获取引用消息失败:', e);
34
+ });
34
35
  }
36
+ catch (e) { }
35
37
  }
36
- const currentImage = session.elements.find(el => el.type === 'image');
37
- if (currentImage) {
38
- return currentImage.attrs.src;
39
- }
40
- return null;
38
+ return imageUrl;
41
39
  }
42
40
  function apply(ctx, config) {
43
41
  const zhCNPath = path_1.default.join(__dirname, './locales/zh-CN.yml');
44
- const zhCNContent = fs_1.default.readFileSync(zhCNPath, 'utf8');
45
- const zhCN = yaml_1.default.parse(zhCNContent);
42
+ const zhCN = yaml_1.default.parse(fs_1.default.readFileSync(zhCNPath, 'utf8'));
46
43
  ctx.i18n.define('zh-CN', zhCN);
47
- ctx.command('url-encode <text:text>')
48
- .action((_, text) => encodeURIComponent(text));
49
- ctx.command('url-decode <text:text>')
50
- .action((_, text) => decodeURIComponent(text));
51
- ctx.command('base64-encode <text:text>')
52
- .action((_, text) => Buffer.from(text).toString('base64'));
53
- ctx.command('base64-decode <text:text>')
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 }) => {
44
+ ctx.command('url-encode <text:text>').action((_, text) => encodeURIComponent(text));
45
+ ctx.command('url-decode <text:text>').action((_, text) => decodeURIComponent(text));
46
+ ctx.command('base64-encode <text:text>').action((_, text) => Buffer.from(text).toString('base64'));
47
+ ctx.command('base64-decode <text:text>').action((_, text) => Buffer.from(text, 'base64').toString());
48
+ ctx.command('unicode-encode <text:text>').action((_, text) => text.split('').map(c => '\\u' + c.charCodeAt(0).toString(16).padStart(4, '0')).join(''));
49
+ ctx.command('unicode-decode <text:text>').action((_, text) => text.replace(/\\u([0-9a-fA-F]{4})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16))));
50
+ ctx.command('decode-code').action(async ({ session }) => {
61
51
  if (!session)
62
52
  return;
63
- const replyWithQuote = (content) => {
64
- return session.send([
65
- (0, koishi_1.h)('quote', { id: session.messageId }),
66
- content
67
- ]);
68
- };
69
- const imageUrl = await extractImageFromSession(session);
70
- if (!imageUrl)
71
- return replyWithQuote(session.text('.no-image'));
53
+ const imageUrl = await getImageUrl(session);
54
+ if (!imageUrl) {
55
+ await session.send(session.text('.no-image'), { quote: session.messageId });
56
+ return;
57
+ }
72
58
  try {
73
59
  const buffer = await ctx.http.get(imageUrl, { responseType: 'arraybuffer' });
74
60
  const img = await jimp_1.default.read(Buffer.from(buffer));
75
- if (img.bitmap.width > 1000 || img.bitmap.height > 1000) {
61
+ if (img.bitmap.width > 1000 || img.bitmap.height > 1000)
76
62
  img.scaleToFit(800, 800);
77
- }
78
63
  img.contrast(0.2);
79
- const imageData = {
80
- data: new Uint8ClampedArray(img.bitmap.data),
81
- width: img.bitmap.width,
82
- height: img.bitmap.height
83
- };
64
+ const imageData = { data: new Uint8ClampedArray(img.bitmap.data), width: img.bitmap.width, height: img.bitmap.height };
84
65
  const qrResult = (0, jsqr_1.default)(imageData.data, imageData.width, imageData.height);
85
- if (qrResult)
86
- return replyWithQuote(session.text('.qr-content', { content: qrResult.data }));
66
+ if (qrResult) {
67
+ await session.send(session.text('.qr-content', { content: qrResult.data }), { quote: session.messageId });
68
+ return;
69
+ }
87
70
  const hints = new Map();
88
- hints.set(library_1.DecodeHintType.POSSIBLE_FORMATS, [
89
- library_1.BarcodeFormat.QR_CODE, library_1.BarcodeFormat.CODE_128, library_1.BarcodeFormat.EAN_13,
90
- library_1.BarcodeFormat.EAN_8, library_1.BarcodeFormat.UPC_A, library_1.BarcodeFormat.UPC_E,
91
- library_1.BarcodeFormat.CODE_39, library_1.BarcodeFormat.ITF
92
- ]);
71
+ 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]);
93
72
  const reader = new library_1.MultiFormatReader();
94
73
  const luminanceSource = new library_1.RGBLuminanceSource(imageData.data, imageData.width, imageData.height);
95
74
  const binaryBitmap = new library_1.BinaryBitmap(new library_1.HybridBinarizer(luminanceSource));
96
75
  const barcodeResult = reader.decode(binaryBitmap, hints);
97
- return replyWithQuote(session.text('.barcode-content', { content: barcodeResult.text }));
76
+ await session.send(session.text('.barcode-content', { content: barcodeResult.text }), { quote: session.messageId });
98
77
  }
99
78
  catch (e) {
100
- console.error('解析失败:', e);
101
- return replyWithQuote(session.text('.no-code'));
79
+ console.error(e);
80
+ await session.send(session.text('.no-code'), { quote: session.messageId });
102
81
  }
103
82
  });
104
83
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-codec-tools",
3
3
  "description": "编码&解码 URL/Base64/Unicode 和条形码/二维码的 Koishi 插件",
4
- "version": "1.0.8",
4
+ "version": "1.1.0",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [