koishi-plugin-codec-tools 1.1.1 → 1.1.3

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Minecraft_1314
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/lib/index.d.ts CHANGED
@@ -2,6 +2,7 @@ import { Context, Schema } from 'koishi';
2
2
  export declare const name = "codec-tools";
3
3
  export declare const using: readonly ["i18n"];
4
4
  export interface Config {
5
+ maxInputLength?: number;
5
6
  }
6
7
  export declare const Config: Schema<Config>;
7
8
  export declare function apply(ctx: Context, config: Config): void;
package/lib/index.js CHANGED
@@ -1,91 +1,119 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.Config = exports.using = exports.name = void 0;
7
4
  exports.apply = apply;
8
5
  const koishi_1 = require("koishi");
9
- const jimp_1 = __importDefault(require("jimp"));
10
- const jsqr_1 = __importDefault(require("jsqr"));
11
- const library_1 = require("@zxing/library");
12
- const fs_1 = __importDefault(require("fs"));
13
- const path_1 = __importDefault(require("path"));
14
- const yaml_1 = __importDefault(require("yaml"));
6
+ const fs_1 = require("fs");
7
+ const path_1 = require("path");
8
+ const yaml_1 = require("yaml");
15
9
  exports.name = 'codec-tools';
16
10
  exports.using = ['i18n'];
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;
29
- if (session.quote) {
30
- console.log('尝试获取引用消息:', session.quote.messageId);
31
- try {
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);
38
- }
39
- });
11
+ exports.Config = koishi_1.Schema.object({
12
+ maxInputLength: koishi_1.Schema.natural()
13
+ .default(10000)
14
+ .description('最大输入长度(字符数)'),
15
+ });
16
+ function validateInput(text, maxLen) {
17
+ if (text.length === 0)
18
+ return '输入不能为空';
19
+ if (text.length > maxLen)
20
+ return `输入过长,最大允许 ${maxLen} 个字符`;
21
+ return null;
22
+ }
23
+ function encodeUrl(text) {
24
+ return encodeURIComponent(text);
25
+ }
26
+ function decodeUrl(text) {
27
+ return decodeURIComponent(text);
28
+ }
29
+ function encodeBase64(text) {
30
+ return Buffer.from(text, 'utf-8').toString('base64');
31
+ }
32
+ function decodeBase64(text) {
33
+ return Buffer.from(text, 'base64').toString('utf-8');
34
+ }
35
+ function encodeUnicode(text) {
36
+ let result = '';
37
+ for (const char of text) {
38
+ const cp = char.codePointAt(0);
39
+ if (cp > 0xffff) {
40
+ result += '\\u{' + cp.toString(16) + '}';
40
41
  }
41
- catch (e) {
42
- console.error('获取引用消息失败:', e);
42
+ else {
43
+ result += '\\u' + cp.toString(16).padStart(4, '0');
43
44
  }
44
45
  }
45
- return imageUrl;
46
+ return result;
47
+ }
48
+ function decodeUnicode(text) {
49
+ return text.replace(/\\u\{([0-9a-fA-F]{1,6})\}|\\u([0-9a-fA-F]{4})/g, (_, brace, fixed) => String.fromCodePoint(parseInt(brace || fixed, 16)));
46
50
  }
47
51
  function apply(ctx, config) {
48
- const zhCNPath = path_1.default.join(__dirname, './locales/zh-CN.yml');
49
- const zhCN = yaml_1.default.parse(fs_1.default.readFileSync(zhCNPath, 'utf8'));
52
+ const maxLen = config.maxInputLength ?? 10000;
53
+ const zhCNPath = (0, path_1.join)(__dirname, './locales/zh-CN.yml');
54
+ const zhCNContent = (0, fs_1.readFileSync)(zhCNPath, 'utf8');
55
+ const zhCN = (0, yaml_1.parse)(zhCNContent);
50
56
  ctx.i18n.define('zh-CN', zhCN);
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 }) => {
58
- if (!session)
59
- return;
60
- const imageUrl = await getImageUrl(session);
61
- console.log('最终获取到的图片URL:', imageUrl);
62
- if (!imageUrl) {
63
- await session.send(session.text('.no-image'), { quote: session.messageId });
64
- return;
57
+ ctx.command('url-encode <text:text>').action((_, text) => {
58
+ const err = validateInput(text, maxLen);
59
+ if (err)
60
+ return err;
61
+ try {
62
+ return encodeUrl(text);
63
+ }
64
+ catch {
65
+ return 'URL 编码失败:输入包含无效字符';
65
66
  }
67
+ });
68
+ ctx.command('url-decode <text:text>').action((_, text) => {
69
+ const err = validateInput(text, maxLen);
70
+ if (err)
71
+ return err;
72
+ try {
73
+ return decodeUrl(text);
74
+ }
75
+ catch {
76
+ return 'URL 解码失败:输入格式不正确';
77
+ }
78
+ });
79
+ ctx.command('base64-encode <text:text>').action((_, text) => {
80
+ const err = validateInput(text, maxLen);
81
+ if (err)
82
+ return err;
83
+ return encodeBase64(text);
84
+ });
85
+ ctx.command('base64-decode <text:text>').action((_, text) => {
86
+ const err = validateInput(text, maxLen);
87
+ if (err)
88
+ return err;
89
+ try {
90
+ const result = decodeBase64(text);
91
+ if (result.length === 0)
92
+ return 'Base64 解码结果为空';
93
+ return result;
94
+ }
95
+ catch {
96
+ return 'Base64 解码失败:输入格式不正确';
97
+ }
98
+ });
99
+ ctx.command('unicode-encode <text:text>').action((_, text) => {
100
+ const err = validateInput(text, maxLen);
101
+ if (err)
102
+ return err;
103
+ return encodeUnicode(text);
104
+ });
105
+ ctx.command('unicode-decode <text:text>').action((_, text) => {
106
+ const err = validateInput(text, maxLen);
107
+ if (err)
108
+ return err;
66
109
  try {
67
- const buffer = await ctx.http.get(imageUrl, { responseType: 'arraybuffer' });
68
- const img = await jimp_1.default.read(Buffer.from(buffer));
69
- if (img.bitmap.width > 1000 || img.bitmap.height > 1000)
70
- img.scaleToFit(800, 800);
71
- img.contrast(0.2);
72
- const imageData = { data: new Uint8ClampedArray(img.bitmap.data), width: img.bitmap.width, height: img.bitmap.height };
73
- const qrResult = (0, jsqr_1.default)(imageData.data, imageData.width, imageData.height);
74
- if (qrResult) {
75
- await session.send(session.text('.qr-content', { content: qrResult.data }), { quote: session.messageId });
76
- return;
77
- }
78
- const hints = new Map();
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]);
80
- const reader = new library_1.MultiFormatReader();
81
- const luminanceSource = new library_1.RGBLuminanceSource(imageData.data, imageData.width, imageData.height);
82
- const binaryBitmap = new library_1.BinaryBitmap(new library_1.HybridBinarizer(luminanceSource));
83
- const barcodeResult = reader.decode(binaryBitmap, hints);
84
- await session.send(session.text('.barcode-content', { content: barcodeResult.text }), { quote: session.messageId });
110
+ const result = decodeUnicode(text);
111
+ if (result.length === 0)
112
+ return 'Unicode 解码结果为空';
113
+ return result;
85
114
  }
86
- catch (e) {
87
- console.error('解析失败:', e);
88
- await session.send(session.text('.no-code'), { quote: session.messageId });
115
+ catch {
116
+ return 'Unicode 解码失败:输入格式不正确';
89
117
  }
90
118
  });
91
119
  }
@@ -1,22 +1,15 @@
1
- commands:
2
- url-encode:
3
- description: URL 编码
4
- url-decode:
5
- description: URL 解码
6
- base64-encode:
7
- description: Base64 编码
8
- base64-decode:
9
- description: Base64 解码
10
- unicode-encode:
11
- description: Unicode 编码
12
- unicode-decode:
13
- description: Unicode 解码
14
- decode-code:
15
- description: 识别二维码/条形码
16
- messages:
17
- no-image: 未找到图片!请发送图片或引用包含图片的消息后重试。
18
- qr-content: 二维码内容:{{content}}
19
- barcode-content: 条形码内容:{{content}}
20
- no-code: 未识别到二维码/条形码!
21
-
1
+ commands:
2
+ url-encode:
3
+ description: URL 编码
4
+ url-decode:
5
+ description: URL 解码
6
+ base64-encode:
7
+ description: Base64 编码
8
+ base64-decode:
9
+ description: Base64 解码
10
+ unicode-encode:
11
+ description: Unicode 编码
12
+ unicode-decode:
13
+ description: Unicode 解码
14
+
22
15
  _config: {}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-codec-tools",
3
- "description": "编码&解码 URL/Base64/Unicode 和条形码/二维码的 Koishi 插件",
4
- "version": "1.1.1",
3
+ "description": "编码&解码 URL/Base64/Unicode Koishi 插件",
4
+ "version": "1.1.3",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
package/readme.md CHANGED
@@ -7,7 +7,6 @@
7
7
  - 支持 URL 编码/解码
8
8
  - 支持 Base64 编码/解码
9
9
  - 支持 Unicode 编码/解码
10
- - 支持从图片中解析二维码/条形码内容
11
10
 
12
11
  ### English
13
12
  This is an encoding/decoding tool plugin developed for the Koishi chatbot framework. This plugin has the following features:
@@ -27,7 +26,6 @@ This is an encoding/decoding tool plugin developed for the Koishi chatbot framew
27
26
  | base64-decode <文本> | 对指定 Base64 编码文本进行解码 |
28
27
  | unicode-encode <文本> | 对指定文本进行 Unicode 编码 |
29
28
  | unicode-decode <文本> | 对指定 Unicode 编码文本进行解码 |
30
- | decode-code | 解析附带图片中的二维码 / 条形码内容 |
31
29
 
32
30
  ### English
33
31
  | Command | Description |
@@ -38,7 +36,6 @@ This is an encoding/decoding tool plugin developed for the Koishi chatbot framew
38
36
  | base64-decode <text> | Decode the specified Base64-encoded text |
39
37
  | unicode-encode <text> | Encode the specified text with Unicode encoding |
40
38
  | unicode-decode <text> | Decode the specified Unicode-encoded text |
41
- | decode-code | Parse QR code/barcode content from the attached image |
42
39
 
43
40
  ## 项目贡献者 (Contributors)
44
41
  | 贡献者 (Contributor) | 贡献内容 (Contribution) |
@@ -1,22 +1,15 @@
1
- commands:
2
- url-encode:
3
- description: URL 编码
4
- url-decode:
5
- description: URL 解码
6
- base64-encode:
7
- description: Base64 编码
8
- base64-decode:
9
- description: Base64 解码
10
- unicode-encode:
11
- description: Unicode 编码
12
- unicode-decode:
13
- description: Unicode 解码
14
- decode-code:
15
- description: 识别二维码/条形码
16
- messages:
17
- no-image: 未找到图片!请发送图片或引用包含图片的消息后重试。
18
- qr-content: 二维码内容:{{content}}
19
- barcode-content: 条形码内容:{{content}}
20
- no-code: 未识别到二维码/条形码!
21
-
1
+ commands:
2
+ url-encode:
3
+ description: URL 编码
4
+ url-decode:
5
+ description: URL 解码
6
+ base64-encode:
7
+ description: Base64 编码
8
+ base64-decode:
9
+ description: Base64 解码
10
+ unicode-encode:
11
+ description: Unicode 编码
12
+ unicode-decode:
13
+ description: Unicode 解码
14
+
22
15
  _config: {}