koishi-plugin-dynamic-bot 0.0.0-dev
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/AGENTS.md +6 -0
- package/LICENSE +201 -0
- package/package.json +33 -0
- package/src/dbk/avatar.ts +58 -0
- package/src/dbk/bots.ts +261 -0
- package/src/dbk/codec.ts +55 -0
- package/src/dbk/error.ts +11 -0
- package/src/dbk/gateway.ts +155 -0
- package/src/dbk/get-target.ts +275 -0
- package/src/dbk/handlers.ts +29 -0
- package/src/dbk/list-targets.ts +459 -0
- package/src/dbk/protocol.ts +100 -0
- package/src/dbk/recall.ts +222 -0
- package/src/dbk/send.ts +398 -0
- package/src/dbk/session.ts +239 -0
- package/src/dbk/socket.ts +52 -0
- package/src/dbk/version.ts +65 -0
- package/src/gen/dbk/v1/common_pb.ts +238 -0
- package/src/gen/dbk/v1/frame_pb.ts +114 -0
- package/src/gen/dbk/v1/rpc_pb.ts +868 -0
- package/src/index.ts +104 -0
package/AGENTS.md
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
# koishi/
|
|
2
|
+
|
|
3
|
+
Koishi **Plugin**:执行 DBK,调用已有 `ctx.bots`。不要实现 `Adapter` / `Bot` 去反向连接 dynamic-bot。
|
|
4
|
+
只做生成类型 ↔ `h()` / `session`。不要解析命令、不要 `session.send` 业务回复、不要在这里保存平台 Token 或 OneBot 连接(仍归 Koishi Adapter 配置,含 `adapter-onebot`)。
|
|
5
|
+
`bot.status`:`ONLINE` → `READY`,`CONNECT`/`RECONNECT` → `CONNECTING`,其余 → `UNAVAILABLE`。不要用 `isActive`。
|
|
6
|
+
`targets.list` 在无嵌套 `channel.list` 的 bot 上可以不完整(典型 Telegram),必须设置 incomplete。
|
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "koishi-plugin-dynamic-bot",
|
|
3
|
+
"repository": {
|
|
4
|
+
"type": "git",
|
|
5
|
+
"url": "https://github.com/incubator4/dynamic-bot-koishi"
|
|
6
|
+
},
|
|
7
|
+
"version": "0.0.0-dev",
|
|
8
|
+
"license": "Apache-2.0",
|
|
9
|
+
"contributors": [
|
|
10
|
+
"AresWang <incubator4@outlook.com>"
|
|
11
|
+
],
|
|
12
|
+
"description": "dynamic-bot Koishi gateway (DBK)",
|
|
13
|
+
"main": "src/index.ts",
|
|
14
|
+
"types": "src/index.ts",
|
|
15
|
+
"peerDependencies": {
|
|
16
|
+
"koishi": "^4.18.7"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@bufbuild/protobuf": "^2.12.1"
|
|
20
|
+
},
|
|
21
|
+
"koishi": {
|
|
22
|
+
"description": {
|
|
23
|
+
"zh": "将 Koishi 已连接账号作为 dynamic-bot 的消息出口",
|
|
24
|
+
"en": "Expose connected Koishi bots as a dynamic-bot message gateway"
|
|
25
|
+
},
|
|
26
|
+
"service": {
|
|
27
|
+
"required": [
|
|
28
|
+
"server",
|
|
29
|
+
"http"
|
|
30
|
+
]
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const AVATAR_KEYS = [
|
|
2
|
+
"avatar",
|
|
3
|
+
"avatarUrl",
|
|
4
|
+
"avatar_url",
|
|
5
|
+
"icon",
|
|
6
|
+
"iconUrl",
|
|
7
|
+
"icon_url",
|
|
8
|
+
"photoUrl",
|
|
9
|
+
"photo_url",
|
|
10
|
+
] as const;
|
|
11
|
+
|
|
12
|
+
/** First non-empty avatar URI among Satori/Koishi-shaped objects. */
|
|
13
|
+
export function pickAvatar(...sources: unknown[]): string {
|
|
14
|
+
for (const source of sources) {
|
|
15
|
+
const value = avatarFrom(source);
|
|
16
|
+
if (value) return value;
|
|
17
|
+
}
|
|
18
|
+
return "";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function avatarFrom(source: unknown): string {
|
|
22
|
+
if (typeof source === "string") return sanitize(source);
|
|
23
|
+
if (!source || typeof source !== "object") return "";
|
|
24
|
+
const rec = source as Record<string, unknown>;
|
|
25
|
+
const nested = rec.user && typeof rec.user === "object"
|
|
26
|
+
? rec.user as Record<string, unknown>
|
|
27
|
+
: undefined;
|
|
28
|
+
for (const obj of [nested, rec]) {
|
|
29
|
+
if (!obj) continue;
|
|
30
|
+
for (const key of AVATAR_KEYS) {
|
|
31
|
+
const value = sanitize(obj[key]);
|
|
32
|
+
if (value) return value;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return photoUri(rec.photo);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function photoUri(photo: unknown): string {
|
|
39
|
+
if (typeof photo === "string") return sanitize(photo);
|
|
40
|
+
if (!photo || typeof photo !== "object") return "";
|
|
41
|
+
const rec = photo as Record<string, unknown>;
|
|
42
|
+
for (const key of ["url", "big", "small", "src"] as const) {
|
|
43
|
+
const value = sanitize(rec[key]);
|
|
44
|
+
if (value) return value;
|
|
45
|
+
}
|
|
46
|
+
return "";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function sanitize(value: unknown): string {
|
|
50
|
+
if (typeof value !== "string") return "";
|
|
51
|
+
const trimmed = value.trim();
|
|
52
|
+
if (!trimmed) return "";
|
|
53
|
+
// Telegram file_id is a token without a URI scheme or path; skip it.
|
|
54
|
+
if (!/^(https?:|data:|file:|\/\/)/i.test(trimmed) && !trimmed.includes("/")) {
|
|
55
|
+
return "";
|
|
56
|
+
}
|
|
57
|
+
return trimmed;
|
|
58
|
+
}
|
package/src/dbk/bots.ts
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { create } from "@bufbuild/protobuf";
|
|
2
|
+
import type { Bot, Context } from "koishi";
|
|
3
|
+
import { BotStatus, TargetKind } from "../gen/dbk/v1/common_pb";
|
|
4
|
+
import { BotSchema, type Bot as DbkBot } from "../gen/dbk/v1/rpc_pb";
|
|
5
|
+
|
|
6
|
+
/** Koishi/Satori `Status`: OFFLINE=0 ONLINE=1 CONNECT=2 DISCONNECT=3 RECONNECT=4 */
|
|
7
|
+
const KOISHI_STATUS_ONLINE = 1;
|
|
8
|
+
const KOISHI_STATUS_CONNECT = 2;
|
|
9
|
+
const KOISHI_STATUS_RECONNECT = 4;
|
|
10
|
+
|
|
11
|
+
const SATORI_MESSAGE_DELETE = "message.delete";
|
|
12
|
+
const SATORI_GUILD_LIST = "guild.list";
|
|
13
|
+
const SATORI_CHANNEL_LIST = "channel.list";
|
|
14
|
+
const SATORI_GUILD_GET = "guild.get";
|
|
15
|
+
const SATORI_CHANNEL_GET = "channel.get";
|
|
16
|
+
const SATORI_FRIEND_LIST = "friend.list";
|
|
17
|
+
const SATORI_USER_GET = "user.get";
|
|
18
|
+
const SATORI_USER_CHANNEL_CREATE = "user.channel.create";
|
|
19
|
+
|
|
20
|
+
export const BotFeature = {
|
|
21
|
+
MESSAGE_RECALL: "message.recall",
|
|
22
|
+
MESSAGE_FORWARD: "message.forward",
|
|
23
|
+
MENTION_ALL: "mention.all",
|
|
24
|
+
TARGETS_LIST: "targets.list",
|
|
25
|
+
TARGET_USER: "target.user",
|
|
26
|
+
TARGET_GROUP: "target.group",
|
|
27
|
+
TARGET_CHANNEL: "target.channel",
|
|
28
|
+
TARGET_THREAD: "target.thread",
|
|
29
|
+
} as const;
|
|
30
|
+
|
|
31
|
+
const TARGET_KIND_FEATURES: ReadonlyArray<readonly [TargetKind, string]> = [
|
|
32
|
+
[TargetKind.USER, BotFeature.TARGET_USER],
|
|
33
|
+
[TargetKind.GROUP, BotFeature.TARGET_GROUP],
|
|
34
|
+
[TargetKind.CHANNEL, BotFeature.TARGET_CHANNEL],
|
|
35
|
+
[TargetKind.THREAD, BotFeature.TARGET_THREAD],
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
const THREAD_METHODS = [
|
|
39
|
+
"getThreadList",
|
|
40
|
+
"listActiveThreads",
|
|
41
|
+
"listPublicArchivedThreads",
|
|
42
|
+
"listPrivateArchivedThreads",
|
|
43
|
+
"startThread",
|
|
44
|
+
"startThreadFromMessage",
|
|
45
|
+
"startThreadWithoutMessage",
|
|
46
|
+
"createThread",
|
|
47
|
+
] as const;
|
|
48
|
+
|
|
49
|
+
/** Native merged-forward APIs (OneBot / NapCat style). Discord thread-forwards do not count. */
|
|
50
|
+
const MERGED_FORWARD_METHODS = [
|
|
51
|
+
"send_group_forward_msg",
|
|
52
|
+
"send_private_forward_msg",
|
|
53
|
+
"send_forward_msg",
|
|
54
|
+
"upload_forward_msg",
|
|
55
|
+
"sendGroupForwardMsg",
|
|
56
|
+
"sendPrivateForwardMsg",
|
|
57
|
+
"sendForwardMsg",
|
|
58
|
+
"uploadForwardMsg",
|
|
59
|
+
"sendGroupForwardMessage",
|
|
60
|
+
"sendPrivateForwardMessage",
|
|
61
|
+
"sendForwardMessage",
|
|
62
|
+
"sendMergeForward",
|
|
63
|
+
"send_merge_forward",
|
|
64
|
+
] as const;
|
|
65
|
+
|
|
66
|
+
export function listBots(ctx: Pick<Context, "bots">): DbkBot[] {
|
|
67
|
+
return ctx.bots.filter((bot) => !bot.hidden).map(toDbkBot);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function toDbkBot(bot: Bot): DbkBot {
|
|
71
|
+
const platform = bot.platform ?? "";
|
|
72
|
+
const selfId = bot.selfId ?? "";
|
|
73
|
+
return create(BotSchema, {
|
|
74
|
+
botKey: platform && selfId ? `${platform}:${selfId}` : "",
|
|
75
|
+
platform,
|
|
76
|
+
selfId,
|
|
77
|
+
name: bot.user?.name || bot.user?.nick || "",
|
|
78
|
+
avatar: bot.user?.avatar || "",
|
|
79
|
+
status: toDbkBotStatus(bot.status),
|
|
80
|
+
features: botFeatures(bot),
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function toDbkBotStatus(status: number): BotStatus {
|
|
85
|
+
switch (status) {
|
|
86
|
+
case KOISHI_STATUS_ONLINE:
|
|
87
|
+
return BotStatus.READY;
|
|
88
|
+
case KOISHI_STATUS_CONNECT:
|
|
89
|
+
case KOISHI_STATUS_RECONNECT:
|
|
90
|
+
return BotStatus.CONNECTING;
|
|
91
|
+
default:
|
|
92
|
+
return BotStatus.UNAVAILABLE;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function botFeatures(bot: Bot): string[] {
|
|
97
|
+
const features: string[] = [];
|
|
98
|
+
if (canRecall(bot)) features.push(BotFeature.MESSAGE_RECALL);
|
|
99
|
+
if (canMergedForward(bot)) features.push(BotFeature.MESSAGE_FORWARD);
|
|
100
|
+
if (canMentionAll(bot)) features.push(BotFeature.MENTION_ALL);
|
|
101
|
+
if (canListTargets(bot)) features.push(BotFeature.TARGETS_LIST);
|
|
102
|
+
for (const kind of botTargetKinds(bot)) {
|
|
103
|
+
const feature = targetKindFeature(kind);
|
|
104
|
+
if (feature) features.push(feature);
|
|
105
|
+
}
|
|
106
|
+
return features;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function botTargetKinds(bot: Bot): TargetKind[] {
|
|
110
|
+
const kinds: TargetKind[] = [];
|
|
111
|
+
const nested = hasNestedChannels(bot);
|
|
112
|
+
const guilds = hasGuildSurface(bot);
|
|
113
|
+
if (canDirectMessage(bot)) kinds.push(TargetKind.USER);
|
|
114
|
+
if (nested) {
|
|
115
|
+
kinds.push(TargetKind.CHANNEL);
|
|
116
|
+
kinds.push(TargetKind.THREAD);
|
|
117
|
+
} else if (guilds) {
|
|
118
|
+
kinds.push(TargetKind.GROUP);
|
|
119
|
+
kinds.push(TargetKind.CHANNEL);
|
|
120
|
+
}
|
|
121
|
+
if (hasThreadSurface(bot) && !kinds.includes(TargetKind.THREAD)) {
|
|
122
|
+
kinds.push(TargetKind.THREAD);
|
|
123
|
+
}
|
|
124
|
+
return kinds;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function targetKindFeature(kind: TargetKind): string | undefined {
|
|
128
|
+
return TARGET_KIND_FEATURES.find(([value]) => value === kind)?.[1];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Nested `channel.list` under a guild: Discord/KOOK-style servers, not sendable GROUP. */
|
|
132
|
+
export function hasNestedChannels(bot: Bot): boolean {
|
|
133
|
+
return hasBotMethod(bot, "getChannelList") || satoriFeatures(bot).includes(SATORI_CHANNEL_LIST);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Guild/chat list or get, including flat adapters where the guild is the sendable target. */
|
|
137
|
+
export function hasGuildSurface(bot: Bot): boolean {
|
|
138
|
+
return (
|
|
139
|
+
hasBotMethod(bot, "getGuildList")
|
|
140
|
+
|| hasBotMethod(bot, "getGuild")
|
|
141
|
+
|| satoriFeatures(bot).includes(SATORI_GUILD_LIST)
|
|
142
|
+
|| satoriFeatures(bot).includes(SATORI_GUILD_GET)
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function canListDirectUsers(bot: Bot): boolean {
|
|
147
|
+
return (
|
|
148
|
+
hasBotMethod(bot, "getFriendList")
|
|
149
|
+
|| hasBotMethod(bot, "getUserList")
|
|
150
|
+
|| satoriFeatures(bot).includes(SATORI_FRIEND_LIST)
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function canDirectMessage(bot: Bot): boolean {
|
|
155
|
+
return (
|
|
156
|
+
canListDirectUsers(bot)
|
|
157
|
+
|| hasBotMethod(bot, "getUser")
|
|
158
|
+
|| hasBotMethod(bot, "createDirectChannel")
|
|
159
|
+
|| satoriFeatures(bot).includes(SATORI_USER_GET)
|
|
160
|
+
|| satoriFeatures(bot).includes(SATORI_USER_CHANNEL_CREATE)
|
|
161
|
+
|| looksDirectMessageOnly(bot)
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function hasThreadSurface(bot: Bot): boolean {
|
|
166
|
+
if (THREAD_METHODS.some((name) => hasBotMethod(bot, name) || hasInternalMethod(bot, name))) {
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
return satoriFeatures(bot).some((feature) => feature.includes("thread"));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function canRecall(bot: Bot): boolean {
|
|
173
|
+
return hasBotMethod(bot, "deleteMessage") || satoriFeatures(bot).includes(SATORI_MESSAGE_DELETE);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function canListTargets(bot: Bot): boolean {
|
|
177
|
+
return (
|
|
178
|
+
hasBotMethod(bot, "getGuildList")
|
|
179
|
+
|| hasBotMethod(bot, "getChannelList")
|
|
180
|
+
|| hasBotMethod(bot, "getGuild")
|
|
181
|
+
|| hasBotMethod(bot, "getChannel")
|
|
182
|
+
|| satoriFeatures(bot).some((feature) => (
|
|
183
|
+
feature === SATORI_GUILD_LIST
|
|
184
|
+
|| feature === SATORI_CHANNEL_LIST
|
|
185
|
+
|| feature === SATORI_GUILD_GET
|
|
186
|
+
|| feature === SATORI_CHANNEL_GET
|
|
187
|
+
))
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function canMentionAll(bot: Bot): boolean {
|
|
192
|
+
if (encoderHandlesMentionAll(bot)) return true;
|
|
193
|
+
if (hasGuildOrChannelSurface(bot)) return true;
|
|
194
|
+
// Unsure and not a group-looking adapter: still declare unless it looks DM-only.
|
|
195
|
+
return !looksDirectMessageOnly(bot);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function canMergedForward(bot: Bot): boolean {
|
|
199
|
+
return MERGED_FORWARD_METHODS.some((name) => hasBotMethod(bot, name) || hasInternalMethod(bot, name));
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function hasGuildOrChannelSurface(bot: Bot): boolean {
|
|
203
|
+
return canListTargets(bot)
|
|
204
|
+
|| hasBotMethod(bot, "getGuildMember")
|
|
205
|
+
|| hasBotMethod(bot, "getGuildMemberList");
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function looksDirectMessageOnly(bot: Bot): boolean {
|
|
209
|
+
if (hasGuildOrChannelSurface(bot)) return false;
|
|
210
|
+
const Encoder = messageEncoder(bot);
|
|
211
|
+
if (Encoder && !encoderHandlesMentionAll(bot)) {
|
|
212
|
+
// Encoder exists, cannot at-all, and there is no guild/channel API.
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function encoderHandlesMentionAll(bot: Bot): boolean {
|
|
219
|
+
const visit = messageEncoder(bot)?.prototype?.visit;
|
|
220
|
+
if (typeof visit !== "function") return false;
|
|
221
|
+
try {
|
|
222
|
+
const src = Function.prototype.toString.call(visit);
|
|
223
|
+
return /type\s*===\s*['"]all['"]/.test(src)
|
|
224
|
+
|| /['"]all['"]\s*===\s*(?:attrs\.)?type/.test(src)
|
|
225
|
+
|| /id\s*===\s*['"]all['"]/.test(src)
|
|
226
|
+
|| /['"]all['"]\s*===\s*(?:attrs\.)?id/.test(src)
|
|
227
|
+
|| /@everyone/.test(src)
|
|
228
|
+
|| /atall/i.test(src);
|
|
229
|
+
} catch {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function messageEncoder(bot: Bot): { prototype?: { visit?: unknown } } | undefined {
|
|
235
|
+
return (bot.constructor as { MessageEncoder?: { prototype?: { visit?: unknown } } }).MessageEncoder;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function satoriFeatures(bot: Bot): string[] {
|
|
239
|
+
const features = (bot as Bot & { features?: unknown }).features;
|
|
240
|
+
if (!Array.isArray(features)) return [];
|
|
241
|
+
return features.filter((item): item is string => typeof item === "string");
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function hasInternalMethod(bot: Bot, name: string): boolean {
|
|
245
|
+
return hasBotMethod((bot as Bot & { internal?: object }).internal, name);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function hasBotMethod(target: object | null | undefined, name: string): boolean {
|
|
249
|
+
if (!target) return false;
|
|
250
|
+
const value = (target as Record<string, unknown>)[name];
|
|
251
|
+
return typeof value === "function" && !looksUnimplemented(value);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function looksUnimplemented(fn: Function): boolean {
|
|
255
|
+
try {
|
|
256
|
+
const src = Function.prototype.toString.call(fn);
|
|
257
|
+
return /not implemented|NotImplementedError/i.test(src);
|
|
258
|
+
} catch {
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
261
|
+
}
|
package/src/dbk/codec.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { create, fromBinary, fromJsonString, toBinary, toJsonString, type DescMessage, type MessageShape } from "@bufbuild/protobuf";
|
|
2
|
+
import { ErrorCode } from "../gen/dbk/v1/common_pb";
|
|
3
|
+
import { FrameSchema, type Frame } from "../gen/dbk/v1/frame_pb";
|
|
4
|
+
import { DbkRpcError } from "./error";
|
|
5
|
+
import type { DbkEncoding } from "./protocol";
|
|
6
|
+
|
|
7
|
+
export function encodeFrame(frame: Frame, encoding: DbkEncoding): Uint8Array | string {
|
|
8
|
+
if (encoding === "json") {
|
|
9
|
+
return toJsonString(FrameSchema, frame);
|
|
10
|
+
}
|
|
11
|
+
return toBinary(FrameSchema, frame);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function decodeFrame(data: Uint8Array | string, encoding: DbkEncoding): Frame {
|
|
15
|
+
if (encoding === "json") {
|
|
16
|
+
const text = typeof data === "string" ? data : new TextDecoder().decode(data);
|
|
17
|
+
return fromJsonString(FrameSchema, text);
|
|
18
|
+
}
|
|
19
|
+
if (typeof data === "string") {
|
|
20
|
+
throw new DbkRpcError(ErrorCode.PROTOCOL, "DBK only accepts protobuf binary frames");
|
|
21
|
+
}
|
|
22
|
+
return fromBinary(FrameSchema, data);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function encodePayload<Desc extends DescMessage>(schema: Desc, message: MessageShape<Desc>): Uint8Array {
|
|
26
|
+
return toBinary(schema, message);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function decodePayload<Desc extends DescMessage>(schema: Desc, bytes: Uint8Array): MessageShape<Desc> {
|
|
30
|
+
return fromBinary(schema, bytes);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function createFrame(init: Parameters<typeof create<typeof FrameSchema>>[1]): Frame {
|
|
34
|
+
return create(FrameSchema, init);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function toBytes(data: unknown): Uint8Array {
|
|
38
|
+
if (data instanceof Uint8Array) return data;
|
|
39
|
+
if (data instanceof ArrayBuffer) return new Uint8Array(data);
|
|
40
|
+
if (ArrayBuffer.isView(data)) {
|
|
41
|
+
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
42
|
+
}
|
|
43
|
+
if (Array.isArray(data)) {
|
|
44
|
+
const chunks = data.map((part) => toBytes(part));
|
|
45
|
+
const total = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);
|
|
46
|
+
const out = new Uint8Array(total);
|
|
47
|
+
let offset = 0;
|
|
48
|
+
for (const chunk of chunks) {
|
|
49
|
+
out.set(chunk, offset);
|
|
50
|
+
offset += chunk.byteLength;
|
|
51
|
+
}
|
|
52
|
+
return out;
|
|
53
|
+
}
|
|
54
|
+
throw new DbkRpcError(ErrorCode.PROTOCOL, "DBK frame is not binary");
|
|
55
|
+
}
|
package/src/dbk/error.ts
ADDED