kokkoro-plugin-hitokoto 3.0.0 → 3.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/README.md +23 -3
  2. package/package.json +1 -1
  3. package/src/index.ts +43 -11
package/README.md CHANGED
@@ -10,15 +10,35 @@ bun add kokkoro-plugin-hitokoto
10
10
 
11
11
  ## 指令
12
12
 
13
- 发送「/一言」,插件默认会从一言接口随机返回一条动画或漫画语句。
13
+ ```text
14
+ /一言 [types]...
15
+ ```
16
+
17
+ `types` 是可选的一言类型。不填写时,插件默认会从一言接口随机返回一条动画或漫画语句。
18
+
19
+ ```text
20
+ /一言
21
+ ```
22
+
23
+ 填写类型可以限定本次返回的语句,多个类型使用空格分隔。
24
+
25
+ ```text
26
+ /一言 诗词 文学
27
+ ```
28
+
29
+ 支持动画、漫画、游戏、文学、原创、来自网络、其他、影视、诗词、网易云、哲学和抖机灵。
14
30
 
15
31
  ## 快捷方式
16
32
 
17
- 也可以发送「来点骚话」触发,群聊需要开启「获取群内全部消息」权限。
33
+ 发送「来点骚话」也会随机返回一条语句,群聊需要开启「获取群内全部消息」权限。
34
+
35
+ ```text
36
+ 来点骚话
37
+ ```
18
38
 
19
39
  ## 环境变量
20
40
 
21
- 如需修改语句类型,请在项目根目录创建 `.env` 文件,并通过 `HITOKOTO_TYPES` 设置一言接口的 `c` 参数。多个类型使用逗号分隔,默认值为 `a,b`。
41
+ 如需修改默认语句类型,请在项目根目录创建 `.env` 文件,并通过 `HITOKOTO_TYPES` 设置一言接口的 `c` 参数。多个类型使用逗号分隔,默认值为 `a,b`。
22
42
 
23
43
  ```ini
24
44
  HITOKOTO_TYPES=c,d
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kokkoro-plugin-hitokoto",
3
- "version": "3.0.0",
3
+ "version": "3.0.1",
4
4
  "description": "ヒトコト",
5
5
  "keywords": [
6
6
  "bot",
package/src/index.ts CHANGED
@@ -43,20 +43,52 @@ interface Sentence {
43
43
 
44
44
  const { HITOKOTO_TYPES = 'a,b' } = import.meta.env;
45
45
  const HITOKOTO_API = new URL('https://v1.hitokoto.cn');
46
+ const TYPE_CODES = {
47
+ 动画: 'a',
48
+ 漫画: 'b',
49
+ 游戏: 'c',
50
+ 文学: 'd',
51
+ 原创: 'e',
52
+ 来自网络: 'f',
53
+ 其他: 'g',
54
+ 影视: 'h',
55
+ 诗词: 'i',
56
+ 网易云: 'j',
57
+ 哲学: 'k',
58
+ 抖机灵: 'l',
59
+ } as const satisfies Record<string, SentenceType>;
60
+ const TYPE_NAMES = Object.keys(TYPE_CODES).join('、');
46
61
 
47
- for (const type of HITOKOTO_TYPES.split(',')) {
48
- HITOKOTO_API.searchParams.append('c', type);
62
+ function resolveTypes(names: string[]) {
63
+ if (names.length === 0) {
64
+ return HITOKOTO_TYPES.split(',');
65
+ }
66
+ return names.map(name => {
67
+ const type = TYPE_CODES[<keyof typeof TYPE_CODES>name];
68
+
69
+ if (!type) {
70
+ throw new Error(`不支持的一言类型「${name}」,可用类型为${TYPE_NAMES}`);
71
+ }
72
+ return type;
73
+ });
49
74
  }
50
75
 
51
- export default () => {
52
- useCommand('/一言', async () => {
53
- const response = await fetch(HITOKOTO_API);
76
+ async function getHitokoto(names: string[]) {
77
+ const url = new URL(HITOKOTO_API);
54
78
 
55
- if (!response.ok) {
56
- throw new Error(`接口请求失败,状态码 ${response.status}`);
57
- }
58
- const { hitokoto, from } = <Sentence>await response.json();
79
+ for (const type of resolveTypes(names)) {
80
+ url.searchParams.append('c', type);
81
+ }
82
+ const response = await fetch(url);
59
83
 
60
- return `『${hitokoto}』——「${from}」`;
61
- }).shortcut('来点骚话');
84
+ if (!response.ok) {
85
+ throw new Error(`接口请求失败,状态码 ${response.status}`);
86
+ }
87
+ const { hitokoto, from } = <Sentence>await response.json();
88
+
89
+ return `『${hitokoto}』——「${from}」`;
90
+ }
91
+
92
+ export default () => {
93
+ useCommand('/一言 [types]...', context => getHitokoto(context.args.types)).shortcut('来点骚话');
62
94
  };