@koishi-ce/plugin-callme 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.
@@ -0,0 +1,13 @@
1
+ commands:
2
+ callme:
3
+ description: 修改自己的称呼
4
+
5
+ messages:
6
+ current: 好的呢,{0}!
7
+ unnamed: 你还没有给自己起一个称呼呢~
8
+ unchanged: 称呼未发生变化。
9
+ empty: 称呼不能为空。
10
+ invalid: 称呼中禁止包含纯文本以外的内容。
11
+ duplicate: 禁止与其他用户重名。
12
+ updated: 好的,{0},请多指教!
13
+ failed: 修改称呼失败。
package/lib/index.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ import { Context, Schema } from "@koishi-ce/koishi";
2
+ //#region src/index.d.ts
3
+ declare module "@koishi-ce/koishi" {
4
+ interface Events {
5
+ "common/callme"(name: string, session: import("@koishi-ce/koishi").Session): string | undefined;
6
+ }
7
+ }
8
+ /** 配置项(当前无可用配置) */
9
+ type Config = Record<never, never>;
10
+ declare const name = "callme";
11
+ declare const inject: string[];
12
+ declare const Config: Schema<Config>;
13
+ declare function apply(ctx: Context): void;
14
+ //#endregion
15
+ export { Config, apply, inject, name };
package/lib/index.mjs ADDED
@@ -0,0 +1,49 @@
1
+ import { RuntimeError, Schema, h } from "@koishi-ce/koishi";
2
+ import zhCN from "./assets/zh-CN-Vuobuf_Z.yml";
3
+ //#region src/index.ts
4
+ /**
5
+ * 称呼设置插件(callme)。
6
+ *
7
+ * 提供 `callme [name]` 指令(别名 `nn`,快捷调用“叫我 …”):
8
+ * 查询或修改当前用户的昵称(写入数据库 user.name)。
9
+ * 其他插件可监听 `common/callme` 事件校验或拒绝昵称修改。
10
+ */
11
+ const name = "callme";
12
+ const inject = ["database"];
13
+ const Config = Schema.object({});
14
+ function apply(ctx) {
15
+ ctx.i18n.define("zh-CN", zhCN);
16
+ ctx.command("callme [name:text]").userFields(["id", "name"]).alias("nn").shortcut("叫我", {
17
+ prefix: true,
18
+ fuzzy: true
19
+ }).action(async ({ session }, name) => {
20
+ if (!session) return;
21
+ const { user } = session;
22
+ if (!user) return;
23
+ if (!name) {
24
+ if (user.name) return session.text(".current", [session.username]);
25
+ else return session.text(".unnamed");
26
+ }
27
+ name = h.transform(name, {
28
+ text: true,
29
+ default: false
30
+ }).trim();
31
+ if (name === user.name) return session.text(".unchanged");
32
+ else if (!name) return session.text(".empty");
33
+ const result = ctx.bail("common/callme", name, session);
34
+ if (result) return result;
35
+ try {
36
+ user.name = name;
37
+ await user.$update();
38
+ return session.text(".updated", [session.username]);
39
+ } catch (error) {
40
+ if (RuntimeError.check(error, "duplicate-entry")) return session.text(".duplicate");
41
+ else {
42
+ ctx.logger("common").warn(error);
43
+ return session.text(".failed");
44
+ }
45
+ }
46
+ });
47
+ }
48
+ //#endregion
49
+ export { Config, apply, inject, name };
@@ -0,0 +1,12 @@
1
+ commands:
2
+ callme:
3
+ description: 修改自己的称呼
4
+ messages:
5
+ current: 好的呢,{0}!
6
+ unnamed: 你还没有给自己起一个称呼呢~
7
+ unchanged: 称呼未发生变化。
8
+ empty: 称呼不能为空。
9
+ invalid: 称呼中禁止包含纯文本以外的内容。
10
+ duplicate: 禁止与其他用户重名。
11
+ updated: 好的,{0},请多指教!
12
+ failed: 修改称呼失败。
@@ -0,0 +1,12 @@
1
+ commands:
2
+ callme:
3
+ description: 修改自己的称呼
4
+ messages:
5
+ current: 好的呢,{0}!
6
+ unnamed: 你还没有给自己起一个称呼呢~
7
+ unchanged: 称呼未发生变化。
8
+ empty: 称呼不能为空。
9
+ invalid: 称呼中禁止包含纯文本以外的内容。
10
+ duplicate: 禁止与其他用户重名。
11
+ updated: 好的,{0},请多指教!
12
+ failed: 修改称呼失败。
@@ -0,0 +1,12 @@
1
+ commands:
2
+ callme:
3
+ description: 修改自己的称呼
4
+ messages:
5
+ current: 好的呢,{0}!
6
+ unnamed: 你还没有给自己起一个称呼呢~
7
+ unchanged: 称呼未发生变化。
8
+ empty: 称呼不能为空。
9
+ invalid: 称呼中禁止包含纯文本以外的内容。
10
+ duplicate: 禁止与其他用户重名。
11
+ updated: 好的,{0},请多指教!
12
+ failed: 修改称呼失败。
@@ -0,0 +1,12 @@
1
+ commands:
2
+ callme:
3
+ description: 修改自己的称呼
4
+ messages:
5
+ current: 好的呢,{0}!
6
+ unnamed: 你还没有给自己起一个称呼呢~
7
+ unchanged: 称呼未发生变化。
8
+ empty: 称呼不能为空。
9
+ invalid: 称呼中禁止包含纯文本以外的内容。
10
+ duplicate: 禁止与其他用户重名。
11
+ updated: 好的,{0},请多指教!
12
+ failed: 修改称呼失败。
@@ -0,0 +1,12 @@
1
+ commands:
2
+ callme:
3
+ description: 修改自己的称呼
4
+ messages:
5
+ current: 好的呢,{0}!
6
+ unnamed: 你还没有给自己起一个称呼呢~
7
+ unchanged: 称呼未发生变化。
8
+ empty: 称呼不能为空。
9
+ invalid: 称呼中禁止包含纯文本以外的内容。
10
+ duplicate: 禁止与其他用户重名。
11
+ updated: 好的,{0},请多指教!
12
+ failed: 修改称呼失败。
@@ -0,0 +1,13 @@
1
+ commands:
2
+ callme:
3
+ description: 修改自己的称呼
4
+
5
+ messages:
6
+ current: 好的呢,{0}!
7
+ unnamed: 你还没有给自己起一个称呼呢~
8
+ unchanged: 称呼未发生变化。
9
+ empty: 称呼不能为空。
10
+ invalid: 称呼中禁止包含纯文本以外的内容。
11
+ duplicate: 禁止与其他用户重名。
12
+ updated: 好的,{0},请多指教!
13
+ failed: 修改称呼失败。
@@ -0,0 +1,12 @@
1
+ commands:
2
+ callme:
3
+ description: 修改自己的稱呼
4
+ messages:
5
+ current: 好的呢,{0}!
6
+ unnamed: 你還沒有給自己起一個稱呼呢~
7
+ unchanged: 稱呼未發生變化。
8
+ empty: 稱呼不能為空。
9
+ invalid: 稱呼中禁止包含純文本以外的內容。
10
+ duplicate: 禁止與其他使用者重名。
11
+ updated: 好的,{0},請多指教!
12
+ failed: 修改稱呼失敗。
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@koishi-ce/plugin-callme",
3
+ "description": "Set User Nickname in Koishi",
4
+ "version": "1.0.0",
5
+ "type": "module",
6
+ "main": "lib/index.mjs",
7
+ "typings": "lib/index.d.ts",
8
+ "files": [
9
+ "lib",
10
+ "src",
11
+ "locales"
12
+ ],
13
+ "contributors": [
14
+ "Shigma <shigma10826@gmail.com>",
15
+ "Oppenheymu <oppenheymu@gmail.com>"
16
+ ],
17
+ "license": "MIT",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/Koishi-CE/koishi.git",
21
+ "directory": "plugins/common/callme"
22
+ },
23
+ "bugs": {
24
+ "url": "https://github.com/Koishi-CE/koishi/issues"
25
+ },
26
+ "homepage": "https://koishi.chat/plugins/common/callme.html",
27
+ "keywords": [
28
+ "bot",
29
+ "chatbot",
30
+ "koishi",
31
+ "plugin",
32
+ "callme"
33
+ ],
34
+ "koishi": {
35
+ "category": "tool",
36
+ "description": {
37
+ "en": "Set user nickname",
38
+ "zh": "设置用户昵称"
39
+ },
40
+ "service": {
41
+ "required": [
42
+ "database"
43
+ ]
44
+ },
45
+ "locales": [
46
+ "zh"
47
+ ]
48
+ },
49
+ "peerDependencies": {
50
+ "koishi": "^4.18.11"
51
+ },
52
+ "devDependencies": {
53
+ "@koishi-ce/plugin-mock": "^1.0.0",
54
+ "@koishi-ce/koishi": "^1.0.0"
55
+ },
56
+ "exports": {
57
+ ".": {
58
+ "source": "./src/index.ts",
59
+ "types": "./lib/index.d.ts",
60
+ "import": "./lib/index.mjs",
61
+ "default": "./lib/index.mjs"
62
+ }
63
+ }
64
+ }
package/src/index.ts ADDED
@@ -0,0 +1,83 @@
1
+ /**
2
+ * 称呼设置插件(callme)。
3
+ *
4
+ * 提供 `callme [name]` 指令(别名 `nn`,快捷调用“叫我 …”):
5
+ * 查询或修改当前用户的昵称(写入数据库 user.name)。
6
+ * 其他插件可监听 `common/callme` 事件校验或拒绝昵称修改。
7
+ */
8
+ import { type Context, h, RuntimeError, Schema } from "@koishi-ce/koishi";
9
+ import zhCN from "../locales/zh-CN.yml";
10
+
11
+ // 模块增强:新增 common/callme 事件,监听者返回字符串即可拦截昵称修改。
12
+ // Session 用内联 import() 类型引用:本文件对 Session 的唯一使用就在此
13
+ // 增强块内,顶层 type import 在多包合并检查(大一统 tsconfig)下会被
14
+ // noUnusedLocals 误判为未使用。
15
+ declare module "@koishi-ce/koishi" {
16
+ interface Events {
17
+ "common/callme"(
18
+ name: string,
19
+ session: import("@koishi-ce/koishi").Session,
20
+ ): string | undefined;
21
+ }
22
+ }
23
+
24
+ /** 配置项(当前无可用配置) */
25
+ export type Config = Record<never, never>;
26
+
27
+ export const name = "callme";
28
+ export const inject = ["database"];
29
+ export const Config: Schema<Config> = Schema.object({});
30
+
31
+ export function apply(ctx: Context) {
32
+ ctx.i18n.define("zh-CN", zhCN);
33
+
34
+ ctx
35
+ .command("callme [name:text]")
36
+ .userFields(["id", "name"])
37
+ .alias("nn")
38
+ .shortcut("叫我", { prefix: true, fuzzy: true })
39
+ .action(async ({ session }, name) => {
40
+ if (!session) return;
41
+ const { user } = session;
42
+ if (!user) return;
43
+ if (!name) {
44
+ if (user.name) {
45
+ return session.text(".current", [session.username]);
46
+ } else {
47
+ return session.text(".unnamed");
48
+ }
49
+ }
50
+
51
+ // 剥离消息元素,只保留纯文本内容(其余元素直接丢弃)
52
+ name = h
53
+ .transform(name, {
54
+ text: true,
55
+ default: false,
56
+ })
57
+ .trim();
58
+
59
+ if (name === user.name) {
60
+ return session.text(".unchanged");
61
+ } else if (!name) {
62
+ return session.text(".empty");
63
+ }
64
+
65
+ // 广播 common/callme 事件:任一监听者返回字符串即视为拦截
66
+ const result = ctx.bail("common/callme", name, session);
67
+ if (result) return result;
68
+
69
+ // 昵称重名(duplicate-entry)给出专门提示,其余异常记日志并告知失败
70
+ try {
71
+ user.name = name;
72
+ await user.$update();
73
+ return session.text(".updated", [session.username]);
74
+ } catch (error) {
75
+ if (RuntimeError.check(error, "duplicate-entry")) {
76
+ return session.text(".duplicate");
77
+ } else {
78
+ ctx.logger("common").warn(error);
79
+ return session.text(".failed");
80
+ }
81
+ }
82
+ });
83
+ }