koishi-plugin-make-a-choice 1.0.0 → 1.0.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.
Files changed (3) hide show
  1. package/lib/index.js +22 -21
  2. package/package.json +1 -1
  3. package/src/index.ts +20 -20
package/lib/index.js CHANGED
@@ -8,32 +8,33 @@ 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*(.*)$/);
14
+ const handleChoice = async (payload) => {
15
+ const text = payload === null || payload === void 0 ? void 0 : payload.trim();
16
+ if (!text)
17
+ return '请按照“左边 还是 右边”的格式提供两个选项。';
18
+ if (!text.includes('还是'))
19
+ return '请用“左边 还是 右边”的格式,让我能拆分两个选项。';
20
+ const match = text.match(/^(.*?)\s*还是\s*(.*)$/);
26
21
  if (!match)
27
- return next();
22
+ return '没有找到两个可供选择的选项。';
28
23
  const left = match[1].trim();
29
24
  const right = match[2].trim();
30
25
  if (!left || !right)
31
- return next();
32
- if (left === right) {
33
- await session.send('我不知道是谁都选好了还在问我◖⁠⚆⁠ᴥ⁠⚆⁠◗');
34
- return;
35
- }
26
+ return '两个选项都要提供哦。';
27
+ if (left === right)
28
+ return '我不知道是谁都选好了还在问我◖⁠⚆⁠ᴥ⁠⚆⁠◗';
36
29
  const choice = Math.random() < 0.5 ? left : right;
37
- await session.send(`我建议${choice}`);
38
- });
30
+ return `我建议${choice}`;
31
+ };
32
+ for (const word of wakeWords) {
33
+ ctx
34
+ .command(`${word} <payload:text>`, '在“xx还是xx”之间随机选择')
35
+ .shortcut(word, { prefix: true })
36
+ .usage(`示例:${word} 可乐 还是 雪碧`)
37
+ .example(`${word} 猫 还是 狗`)
38
+ .action(async ({ session }, payload) => handleChoice(payload));
39
+ }
39
40
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-make-a-choice",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Pick between two options after a wake word.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
package/src/index.ts CHANGED
@@ -11,32 +11,32 @@ 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()
17
+ const handleChoice = async (payload: string | undefined) => {
18
+ const text = payload?.trim()
19
+ if (!text) return '请按照“左边 还是 右边”的格式提供两个选项。'
20
+ if (!text.includes('还是')) return '请用“左边 还是 右边”的格式,让我能拆分两个选项。'
20
21
 
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()
22
+ const match = text.match(/^(.*?)\s*还是\s*(.*)$/)
23
+ if (!match) return '没有找到两个可供选择的选项。'
29
24
 
30
25
  const left = match[1].trim()
31
26
  const right = match[2].trim()
32
- if (!left || !right) return next()
33
-
34
- if (left === right) {
35
- await session.send('我不知道是谁都选好了还在问我◖⁠⚆⁠ᴥ⁠⚆⁠◗')
36
- return
37
- }
27
+ if (!left || !right) return '两个选项都要提供哦。'
28
+ if (left === right) return '我不知道是谁都选好了还在问我◖⁠⚆⁠ᴥ⁠⚆⁠◗'
38
29
 
39
30
  const choice = Math.random() < 0.5 ? left : right
40
- await session.send(`我建议${choice}`)
41
- })
31
+ return `我建议${choice}`
32
+ }
33
+
34
+ for (const word of wakeWords) {
35
+ ctx
36
+ .command(`${word} <payload:text>`, '在“xx还是xx”之间随机选择')
37
+ .shortcut(word, { prefix: true })
38
+ .usage(`示例:${word} 可乐 还是 雪碧`)
39
+ .example(`${word} 猫 还是 狗`)
40
+ .action(async ({ session }, payload) => handleChoice(payload))
41
+ }
42
42
  }