koishi-plugin-eula-regen 1.0.4 → 1.0.5
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 +36 -8
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -38,8 +38,16 @@ class Eula extends koishi_1.Service {
|
|
|
38
38
|
// 使用 js-yaml 解析 YAML 文件
|
|
39
39
|
const zhContent = (0, fs_1.readFileSync)((0, path_1.join)(localesDir, 'zh.yaml'), 'utf8');
|
|
40
40
|
const enContent = (0, fs_1.readFileSync)((0, path_1.join)(localesDir, 'en.yaml'), 'utf8');
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
const zhData = yaml.load(zhContent);
|
|
42
|
+
const enData = yaml.load(enContent);
|
|
43
|
+
// 注册基础语言
|
|
44
|
+
ctx.i18n.define('zh', zhData);
|
|
45
|
+
ctx.i18n.define('en', enData);
|
|
46
|
+
// 注册语言变体(zh-CN, zh-TW 等映射到 zh)
|
|
47
|
+
ctx.i18n.define('zh-CN', zhData);
|
|
48
|
+
ctx.i18n.define('zh-TW', zhData);
|
|
49
|
+
ctx.i18n.define('zh-Hans', zhData);
|
|
50
|
+
ctx.i18n.define('zh-Hant', zhData);
|
|
43
51
|
}
|
|
44
52
|
catch (err) {
|
|
45
53
|
this.log.error('Failed to load locales:', err);
|
|
@@ -73,22 +81,42 @@ class Eula extends koishi_1.Service {
|
|
|
73
81
|
jsx_runtime_1 = mod.default || mod;
|
|
74
82
|
}
|
|
75
83
|
const session = argv.session;
|
|
76
|
-
|
|
84
|
+
// 获取用户语言,处理语言变体(zh-CN -> zh)
|
|
85
|
+
let userLocale = session.user.locale || session.user.locales[0] || 'zh';
|
|
86
|
+
if (userLocale.startsWith('zh')) {
|
|
87
|
+
// 将 zh-CN, zh-TW 等映射到 zh
|
|
88
|
+
userLocale = 'zh';
|
|
89
|
+
}
|
|
90
|
+
else if (userLocale.startsWith('en')) {
|
|
91
|
+
userLocale = 'en';
|
|
92
|
+
}
|
|
77
93
|
const accept = koishi_1.Random.pick(this.configs.accept) ?? session.text('eula.defaultAccept');
|
|
78
|
-
|
|
94
|
+
try {
|
|
95
|
+
await session.send((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("message", { id: "{0}", children: (0, jsx_runtime_1.jsx)("i18n", { path: "eula.eulaMessage.title", children: [this.configs.alias] }) }), (0, jsx_runtime_1.jsxs)("message", { forward: this.configs.forwardMessgae, children: [(0, jsx_runtime_1.jsx)("message", { id: "{0}", children: this.configs.content[userLocale] ?? this.configs.content['zh'] }), (0, jsx_runtime_1.jsx)("message", { id: "{0}", children: (0, jsx_runtime_1.jsx)("i18n", { path: "eula.eulaMessage.confirm", children: [accept] }) })] })] }));
|
|
96
|
+
}
|
|
97
|
+
catch (err) {
|
|
98
|
+
this.log.error('Failed to send eula message:', err);
|
|
99
|
+
return '发送协议内容时出错,请稍后重试';
|
|
100
|
+
}
|
|
79
101
|
const prompt = await session.prompt(this.configs.waitTime * 1000);
|
|
80
|
-
this.log.info(`[
|
|
102
|
+
this.log.info(`[platform: ${session.platform}] user ${session.userId} reply to (${prompt}) eula`);
|
|
81
103
|
if (prompt) {
|
|
82
104
|
const promptEle = koishi_1.h.parse(prompt);
|
|
83
|
-
if (promptEle[0].type === 'at' && promptEle[0].attrs.id === session.bot.selfId)
|
|
105
|
+
if (promptEle.length > 0 && promptEle[0].type === 'at' && promptEle[0].attrs.id === session.bot.selfId) {
|
|
84
106
|
promptEle.shift(); // remove `at` element
|
|
85
|
-
|
|
107
|
+
}
|
|
108
|
+
if (promptEle.length === 0) {
|
|
109
|
+
return session.text('eula.timeout');
|
|
110
|
+
}
|
|
111
|
+
const promptText = (promptEle[0].attrs?.content || prompt).replace(/^\//g, '').trim();
|
|
112
|
+
const accredit = promptText === accept || promptText === '同意';
|
|
86
113
|
session.user.eula = accredit;
|
|
87
114
|
this.ctx.emit('eula/update', session, accredit);
|
|
88
115
|
return session.text(`${accredit ? 'eula.acceptedMessage' : 'eula.rejectMessage'}`, [this.configs.alias]);
|
|
89
116
|
}
|
|
90
|
-
else
|
|
117
|
+
else {
|
|
91
118
|
return session.text('eula.timeout');
|
|
119
|
+
}
|
|
92
120
|
}
|
|
93
121
|
/**
|
|
94
122
|
*
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koishi-plugin-eula-regen",
|
|
3
3
|
"description": "EULA(End-user licence agreement) for your koishi bot.",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.5",
|
|
5
5
|
"repository": "https://github.com/Lipraty/koishi-plugin-eula",
|
|
6
6
|
"author": "Lipraty <i@lonay.me>",
|
|
7
7
|
"main": "lib/index.js",
|