koishi-plugin-make-a-choice 1.0.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.
package/lib/index.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { Context, Schema } from 'koishi';
2
+ export declare const name = "make-a-choice";
3
+ export interface Config {
4
+ wakeWords: string[];
5
+ }
6
+ export declare const Config: Schema<Config>;
7
+ export declare function apply(ctx: Context, config: Config): void;
package/lib/index.js ADDED
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Config = exports.name = void 0;
4
+ exports.apply = apply;
5
+ const koishi_1 = require("koishi");
6
+ exports.name = 'make-a-choice';
7
+ exports.Config = koishi_1.Schema.object({
8
+ wakeWords: koishi_1.Schema.array(koishi_1.Schema.string()).default(['选择', '帮选', '决定']).description('触发插件的唤醒词列表。'),
9
+ });
10
+ function apply(ctx, config) {
11
+ const wakeWords = config.wakeWords.map(word => word.trim()).filter(Boolean);
12
+ if (!wakeWords.length)
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
+ });
39
+ }
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "koishi-plugin-make-a-choice",
3
+ "version": "1.0.0",
4
+ "description": "Pick between two options after a wake word.",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "type": "commonjs",
8
+ "scripts": {
9
+ "build": "tsc"
10
+ },
11
+ "keywords": [
12
+ "koishi",
13
+ "plugin",
14
+ "choice",
15
+ "random",
16
+ "chatbot"
17
+ ],
18
+ "author": "MuxYang <tlnkmc.b@gmail.com>",
19
+ "license": "MIT",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/MuxYang/koishi-plugin-make-a-choice.git"
23
+ },
24
+ "bugs": {
25
+ "url": "https://github.com/MuxYang/koishi-plugin-make-a-choice/issues"
26
+ },
27
+ "homepage": "https://github.com/MuxYang/koishi-plugin-make-a-choice#readme",
28
+ "files": [
29
+ "lib",
30
+ "src"
31
+ ],
32
+ "koishi": {
33
+ "description": {
34
+ "en": "Pick between two options after a wake word",
35
+ "zh-CN": "设定唤醒词后,在“xx还是xx”之间随机选择"
36
+ }
37
+ },
38
+ "peerDependencies": {
39
+ "koishi": "^4.17.0"
40
+ },
41
+ "devDependencies": {
42
+ "typescript": "^5.0.0",
43
+ "koishi": "^4.17.0"
44
+ }
45
+ }
package/src/index.ts ADDED
@@ -0,0 +1,42 @@
1
+ import { Context, Schema } from 'koishi'
2
+
3
+ export const name = 'make-a-choice'
4
+
5
+ export interface Config {
6
+ wakeWords: string[]
7
+ }
8
+
9
+ export const Config: Schema<Config> = Schema.object({
10
+ wakeWords: Schema.array(Schema.string()).default(['选择', '帮选', '决定']).description('触发插件的唤醒词列表。'),
11
+ })
12
+
13
+ export function apply(ctx: Context, config: Config) {
14
+ const wakeWords = config.wakeWords.map(word => word.trim()).filter(Boolean)
15
+ if (!wakeWords.length) return
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
+ })
42
+ }