koishi-plugin-make-a-choice 1.0.0 → 1.0.2

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 (3) hide show
  1. package/lib/index.js +24 -26
  2. package/package.json +4 -4
  3. package/src/index.ts +24 -26
package/lib/index.js CHANGED
@@ -8,32 +8,30 @@ exports.Config = koishi_1.Schema.object({
8
8
  wakeWords: koishi_1.Schema.array(koishi_1.Schema.string()).default(['选择', '帮选', '决定']).description('触发插件的唤醒词列表。'),
9
9
  });
10
10
  function apply(ctx, config) {
11
- const wakeWords = config.wakeWords.map(word => word.trim()).filter(Boolean);
11
+ const wakeWords = Array.from(new Set(config.wakeWords.map(word => word.trim()).filter(Boolean)));
12
12
  if (!wakeWords.length)
13
13
  return;
14
- ctx.middleware(async (session, next) => {
15
- var _a;
16
- const content = (_a = session.content) === null || _a === void 0 ? void 0 : _a.trim();
17
- if (!content)
18
- return next();
19
- const hit = wakeWords.find(word => content.startsWith(word));
20
- if (!hit)
21
- return next();
22
- const payload = content.slice(hit.length).trim();
23
- if (!payload.includes('还是'))
24
- return next();
25
- const match = payload.match(/^(.*?)\s*还是\s*(.*)$/);
26
- if (!match)
27
- return next();
28
- const left = match[1].trim();
29
- const right = match[2].trim();
30
- if (!left || !right)
31
- return next();
32
- if (left === right) {
33
- await session.send('我不知道是谁都选好了还在问我◖⁠⚆⁠ᴥ⁠⚆⁠◗');
34
- return;
35
- }
36
- const choice = Math.random() < 0.5 ? left : right;
37
- await session.send(`我建议${choice}`);
38
- });
14
+ const handleChoice = async (payload) => {
15
+ const text = payload === null || payload === void 0 ? void 0 : payload.trim();
16
+ if (!text)
17
+ return '请按照“A 还是 B 还是 C”的格式提供多个选项。';
18
+ if (!text.includes('还是'))
19
+ return '请用“A 还是 B”的格式,让我能拆分多个选项。';
20
+ const options = text.split(/\s*还是\s*/).map(opt => opt.trim()).filter(Boolean);
21
+ if (options.length < 2)
22
+ return '请提供至少两个有效的选项。';
23
+ const uniqueOptions = new Set(options);
24
+ if (uniqueOptions.size === 1)
25
+ return '我不知道是谁都选好了还在问我◖⁠⚆⁠ᴥ⁠⚆⁠◗';
26
+ const choice = options[Math.floor(Math.random() * options.length)];
27
+ return `我建议${choice}`;
28
+ };
29
+ for (const word of wakeWords) {
30
+ ctx
31
+ .command(`${word} <payload:text>`, '在多个选项之间随机选择')
32
+ .shortcut(word, { prefix: true })
33
+ .usage(`示例:${word} 选项A 还是 选项B 还是 选项C`)
34
+ .example(`${word} 猫 还是 狗 还是 狐狸`)
35
+ .action(async ({ session }, payload) => handleChoice(payload));
36
+ }
39
37
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-make-a-choice",
3
- "version": "1.0.0",
4
- "description": "Pick between two options after a wake word.",
3
+ "version": "1.0.2",
4
+ "description": "Pick between multiple options after a wake word.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
7
7
  "type": "commonjs",
@@ -31,8 +31,8 @@
31
31
  ],
32
32
  "koishi": {
33
33
  "description": {
34
- "en": "Pick between two options after a wake word",
35
- "zh-CN": "设定唤醒词后,在“xx还是xx”之间随机选择"
34
+ "en": "Pick between multiple options after a wake word",
35
+ "zh-CN": "设定唤醒词后,在“xx还是xx还是xx......”之间随机选择"
36
36
  }
37
37
  },
38
38
  "peerDependencies": {
package/src/index.ts CHANGED
@@ -11,32 +11,30 @@ export const Config: Schema<Config> = Schema.object({
11
11
  })
12
12
 
13
13
  export function apply(ctx: Context, config: Config) {
14
- const wakeWords = config.wakeWords.map(word => word.trim()).filter(Boolean)
14
+ const wakeWords = Array.from(new Set(config.wakeWords.map(word => word.trim()).filter(Boolean)))
15
15
  if (!wakeWords.length) return
16
16
 
17
- ctx.middleware(async (session, next) => {
18
- const content = session.content?.trim()
19
- if (!content) return next()
20
-
21
- const hit = wakeWords.find(word => content.startsWith(word))
22
- if (!hit) return next()
23
-
24
- const payload = content.slice(hit.length).trim()
25
- if (!payload.includes('还是')) return next()
26
-
27
- const match = payload.match(/^(.*?)\s*还是\s*(.*)$/)
28
- if (!match) return next()
29
-
30
- const left = match[1].trim()
31
- const right = match[2].trim()
32
- if (!left || !right) return next()
33
-
34
- if (left === right) {
35
- await session.send('我不知道是谁都选好了还在问我◖⁠⚆⁠ᴥ⁠⚆⁠◗')
36
- return
37
- }
38
-
39
- const choice = Math.random() < 0.5 ? left : right
40
- await session.send(`我建议${choice}`)
41
- })
17
+ const handleChoice = async (payload: string | undefined) => {
18
+ const text = payload?.trim()
19
+ if (!text) return '请按照“A 还是 B 还是 C”的格式提供多个选项。'
20
+ if (!text.includes('还是')) return '请用“A 还是 B”的格式,让我能拆分多个选项。'
21
+
22
+ const options = text.split(/\s*还是\s*/).map(opt => opt.trim()).filter(Boolean)
23
+ if (options.length < 2) return '请提供至少两个有效的选项。'
24
+
25
+ const uniqueOptions = new Set(options)
26
+ if (uniqueOptions.size === 1) return '我不知道是谁都选好了还在问我◖⁠⚆⁠ᴥ⁠⚆⁠◗'
27
+
28
+ const choice = options[Math.floor(Math.random() * options.length)]
29
+ return `我建议${choice}`
30
+ }
31
+
32
+ for (const word of wakeWords) {
33
+ ctx
34
+ .command(`${word} <payload:text>`, '在多个选项之间随机选择')
35
+ .shortcut(word, { prefix: true })
36
+ .usage(`示例:${word} 选项A 还是 选项B 还是 选项C`)
37
+ .example(`${word} 猫 还是 狗 还是 狐狸`)
38
+ .action(async ({ session }, payload) => handleChoice(payload))
39
+ }
42
40
  }