@zhin.js/adapter-discord 5.0.2 → 6.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/CHANGELOG.md +62 -0
- package/README.md +49 -79
- package/adapters/discord.ts +46 -0
- package/agent/tools/add_role.ts +2 -2
- package/agent/tools/create_thread.ts +2 -2
- package/agent/tools/forum_post.ts +2 -2
- package/agent/tools/list_roles.ts +2 -2
- package/agent/tools/react.ts +2 -2
- package/agent/tools/remove_role.ts +2 -2
- package/agent/tools/send_embed.ts +2 -2
- package/lib/discord-agent-deps.d.ts +35 -0
- package/lib/discord-agent-deps.js +32 -0
- package/lib/endpoint.d.ts +71 -0
- package/lib/endpoint.js +355 -0
- package/lib/gateway.d.ts +122 -0
- package/lib/gateway.js +235 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +6 -0
- package/lib/platform-permit.d.ts +15 -0
- package/lib/{src/platform-permit.js → platform-permit.js} +1 -2
- package/lib/protocol.d.ts +135 -0
- package/lib/protocol.js +234 -0
- package/lib/webhook.d.ts +13 -0
- package/lib/webhook.js +86 -0
- package/package.json +43 -36
- package/plugin.ts +13 -0
- package/schema.json +105 -0
- package/src/discord-agent-deps.ts +68 -11
- package/src/endpoint.ts +385 -1167
- package/src/gateway.ts +337 -0
- package/src/index.ts +56 -157
- package/src/platform-permit.ts +1 -1
- package/src/protocol.ts +392 -0
- package/src/webhook.ts +121 -0
- package/client/Dashboard.tsx +0 -195
- package/client/index.tsx +0 -11
- package/client/tsconfig.json +0 -7
- package/client/utils/api.ts +0 -30
- package/dist/index.js +0 -29
- package/lib/agent/tools/add_role.js +0 -22
- package/lib/agent/tools/add_role.js.map +0 -1
- package/lib/agent/tools/create_thread.js +0 -23
- package/lib/agent/tools/create_thread.js.map +0 -1
- package/lib/agent/tools/forum_post.js +0 -22
- package/lib/agent/tools/forum_post.js.map +0 -1
- package/lib/agent/tools/list_roles.js +0 -20
- package/lib/agent/tools/list_roles.js.map +0 -1
- package/lib/agent/tools/react.js +0 -20
- package/lib/agent/tools/react.js.map +0 -1
- package/lib/agent/tools/remove_role.js +0 -22
- package/lib/agent/tools/remove_role.js.map +0 -1
- package/lib/agent/tools/send_embed.js +0 -40
- package/lib/agent/tools/send_embed.js.map +0 -1
- package/lib/src/adapter.js +0 -96
- package/lib/src/adapter.js.map +0 -1
- package/lib/src/discord-agent-deps.js +0 -10
- package/lib/src/discord-agent-deps.js.map +0 -1
- package/lib/src/endpoint-interactions.js +0 -284
- package/lib/src/endpoint-interactions.js.map +0 -1
- package/lib/src/endpoint.js +0 -1093
- package/lib/src/endpoint.js.map +0 -1
- package/lib/src/index.js +0 -159
- package/lib/src/index.js.map +0 -1
- package/lib/src/platform-permit.js.map +0 -1
- package/lib/src/segment-mapper.js +0 -2
- package/lib/src/segment-mapper.js.map +0 -1
- package/lib/src/types.js +0 -2
- package/lib/src/types.js.map +0 -1
- package/plugin.yml +0 -3
- package/src/adapter.ts +0 -108
- package/src/endpoint-interactions.ts +0 -354
- package/src/segment-mapper.ts +0 -1
- package/src/types.ts +0 -60
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,67 @@
|
|
|
1
1
|
# @zhin.js/adapter-discord
|
|
2
2
|
|
|
3
|
+
## 6.0.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 7db69c1: 命令前缀改为适配器配置项:`MessageDispatcher` 不再硬编码 `/`,默认按消息所属适配器实例 config 的 `commandPrefix` 解析(默认 `''` 无前缀,任意文本按命令匹配),`endpoints[i].commandPrefix` 逐项覆盖;`ImRuntime({ commandPrefix })` 仍可设全局静态前缀。全部 20 个平台适配器 schema 新增 `commandPrefix` 属性。
|
|
8
|
+
|
|
9
|
+
BREAKING(行为变化):未配置时命令不再需要 `/` 前缀——原 `/zt` 写法不再命中,直接发 `zt` 即可;需要斜杠风格的适配器请在配置里显式设 `commandPrefix: '/'`。
|
|
10
|
+
|
|
11
|
+
- 713445c: 适配器配置格式定稿(不兼容旧格式):`plugins.<adapter>` 顶层仅共享字段 + `commandPrefix`,`endpoints[i]` 携带 endpoint 级字段(`name` + 凭据,各 schema 已类型化),`endpoints` 为必填(icqq 另需顶层 `master`);icqq 新增 `trusted` 列表(顶层/逐项均可)。scaffold-wizard 全部字段式与自定义 configure() 产出改为新格式,examples(full-bot / qq-games-bot)与 20 个适配器 README 同步迁移。
|
|
12
|
+
- Updated dependencies [7db69c1]
|
|
13
|
+
- Updated dependencies [e5c84ed]
|
|
14
|
+
- Updated dependencies [3ea84a0]
|
|
15
|
+
- Updated dependencies [1ddcd70]
|
|
16
|
+
- Updated dependencies [ac9da66]
|
|
17
|
+
- @zhin.js/core@1.4.0
|
|
18
|
+
- @zhin.js/adapter@1.1.0
|
|
19
|
+
- @zhin.js/plugin-runtime@1.1.0
|
|
20
|
+
- @zhin.js/agent@1.0.5
|
|
21
|
+
- @zhin.js/host-http@1.0.2
|
|
22
|
+
- zhin.js@5.0.0
|
|
23
|
+
|
|
24
|
+
## 5.0.3
|
|
25
|
+
|
|
26
|
+
### Patch Changes
|
|
27
|
+
|
|
28
|
+
- cc5c94d: 约定式插件运行时迁移(breaking):插件与适配器由 `usePlugin()` / `extends Adapter` 迁移为 `definePlugin` / `defineAdapter` + `plugin.ts` + 约定目录(`adapters/`、`commands/`、`components/`、`tools/` 等)。
|
|
29
|
+
|
|
30
|
+
- 新增约定式运行时包:`@zhin.js/plugin-runtime`、`@zhin.js/adapter`、`@zhin.js/runtime`、`@zhin.js/host-http`(首版 1.0.0 走 init-publish,不在本 changeset 内 bump)。
|
|
31
|
+
- 全部 20 个平台适配器改为约定式 `defineAdapter`,旧 `usePlugin` / `extends Adapter` / `segment-mapper` 生产入口已删除;onebot11 反向 WSS、onebot12 webhook/wss、milky sse/webhook/wss、satori webhook、kook webhook、qq webhook/middleware 等 slice 1 推迟的连接模式已补齐。
|
|
32
|
+
- 游戏 / 工具 / 服务插件同步迁移到约定目录结构。
|
|
33
|
+
- CLI 增加 plugin-runtime host installer(http/database/outbound/schedule/console 等)。
|
|
34
|
+
|
|
35
|
+
后续加固(同批):
|
|
36
|
+
|
|
37
|
+
- CLI:`zhin runtime start --daemon`(pidfile/崩溃拉起/风暴保护),orphan watchdog 防僵尸进程;legacy `zhin dev` / `zhin start` 已移除(含 `zhin restart`),`zhin stop` 兼容新 daemon。
|
|
38
|
+
- 安全:builtin 工具统一走 `security/policy-facade.ts` 的 `runToolPolicies`(声明式策略表,deny 优先);审计日志 close flush + 背压队列;`splitCompoundCommand` 引号感知、`extractCommandName` 去引号堵绕过。
|
|
39
|
+
- 日志:Logger 双堆栈修复、本地时区、`getLogger` 挂树(`setLevel` 递归生效)、第三方库(log4js/discord)桥接、启动人读总结。
|
|
40
|
+
- 结构:`plugins/games/shared` 迁为 `packages/game-kit`(`@zhin.js/game-kit`);死目录 `plugins/adapters/common` 删除。
|
|
41
|
+
- 脚手架:`create-zhin-app` / `zhin new` / scaffold-wizard 生成物改为 Plugin Runtime 形态(minimal-bot 同构,新配置格式)。
|
|
42
|
+
- Console:endpoint.list 真实名称与 phase、schema:get-all 按 instanceKey 映射、db:\* 接 DatabaseHost。
|
|
43
|
+
|
|
44
|
+
注:按仓库发布惯例(见 1bb345dd2),本次 breaking 迁移统一使用 patch,避免 zhin.js 5.0 级联。
|
|
45
|
+
|
|
46
|
+
- 447f3e2: 迁移缺口修复(legacy 功能对齐):
|
|
47
|
+
|
|
48
|
+
- html 段出站规范化:经 `@zhin.js/html-renderer` 渲染为 image 段(sandbox 豁免、无渲染器时降级文本),修复真实平台 `[object Object]`。
|
|
49
|
+
- 群聊 @ 触发 AI:适配器入站标注 `metadata.mentioned`(icqq/qq/slack/onebot11/onebot12/napcat/milky/discord/telegram/kook/dingtalk/satori),`matchAiTrigger` 补齐 ignorePrefixes/respondToAt/respondToPrivate/keywords(默认值与 legacy 对齐)。
|
|
50
|
+
- im_transcripts 全量流水恢复写入(chat_history 工具可用);群聊旁听上下文回迁。
|
|
51
|
+
- `ai.trigger.timeout/thinkingMessage/errorTemplate` 生效;masters/trusted 角色解析对齐 legacy。
|
|
52
|
+
- `Message.sender` 统一为用户 ID(onebot11/12、napcat、milky 原误传显示名);quote_id 经 metadata 接入 AI 引用上下文。
|
|
53
|
+
|
|
54
|
+
- Updated dependencies [16ec4e8]
|
|
55
|
+
- Updated dependencies [cc5c94d]
|
|
56
|
+
- Updated dependencies [447f3e2]
|
|
57
|
+
- @zhin.js/core@1.3.5
|
|
58
|
+
- @zhin.js/agent@1.0.4
|
|
59
|
+
- @zhin.js/host-http@1.0.1
|
|
60
|
+
- zhin.js@4.1.3
|
|
61
|
+
- @zhin.js/logger@1.0.75
|
|
62
|
+
- @zhin.js/plugin-runtime@1.0.1
|
|
63
|
+
- @zhin.js/adapter@1.0.1
|
|
64
|
+
|
|
3
65
|
## 5.0.2
|
|
4
66
|
|
|
5
67
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
# @zhin.js/adapter-discord
|
|
2
2
|
|
|
3
|
-
Zhin.js Discord
|
|
3
|
+
Zhin.js Discord 适配器(Plugin Runtime),默认通过 **Gateway WebSocket**(discord.js)收发消息(无需 host-router / host-http)。
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## 功能
|
|
6
6
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
7
|
+
- Gateway WebSocket 入站(默认;无需公网 HTTPS / host)
|
|
8
|
+
- 解析 text / mention / attachment / embed / sticker / button
|
|
9
|
+
- 支持私聊、群组与服务器频道
|
|
10
|
+
- 出站 `send({ target, payload })` → Discord channel message(text / media / embed / keyboard)
|
|
11
|
+
- 约定式 `defineAdapter` / `definePlugin`(无需 `usePlugin`)
|
|
12
|
+
- Interactions HTTP webhook 延期(需 `httpHostToken`);配置 `connection: interactions` 会明确报错
|
|
13
13
|
|
|
14
14
|
## 安装
|
|
15
15
|
|
|
@@ -17,84 +17,57 @@ Zhin.js Discord 适配器,支持 Discord 机器人的消息收发和服务器
|
|
|
17
17
|
pnpm add @zhin.js/adapter-discord discord.js
|
|
18
18
|
```
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
## Plugin Runtime
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
- `@zhin.js/adapter` — 约定式 `adapters/discord.ts`(`defineAdapter`)
|
|
23
|
+
- `@zhin.js/core` — `messageGatewayToken` 入站/出站
|
|
24
|
+
- `@zhin.js/plugin-runtime` — `plugin.ts`(`definePlugin`)
|
|
25
|
+
- 配置经插件 `schema.json` 落到 `plugins.<instanceKey>`
|
|
26
|
+
- **无需** `@zhin.js/host-http` / `@zhin.js/host-router`(Gateway 路径)
|
|
23
27
|
|
|
24
|
-
|
|
28
|
+
入站:`gateway.receive({ adapter, target: channelId, content: text, sender, metadata })`
|
|
29
|
+
出站:`send({ target, payload })` → discord.js channel.send
|
|
25
30
|
|
|
26
|
-
###
|
|
31
|
+
### 平台权限(platform permit)
|
|
27
32
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
- "@zhin.js/adapter-discord"
|
|
31
|
-
|
|
32
|
-
endpoints:
|
|
33
|
-
- context: discord
|
|
34
|
-
connection: gateway
|
|
35
|
-
name: my-discord-bot
|
|
36
|
-
token: "${DISCORD_BOT_TOKEN}"
|
|
37
|
-
# 可选: intents, enableSlashCommands, defaultActivity, slashCommands
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
### Interactions(connection: interactions)
|
|
41
|
-
|
|
42
|
-
```yaml
|
|
43
|
-
plugins:
|
|
44
|
-
- "@zhin.js/host-router"
|
|
45
|
-
- "@zhin.js/adapter-discord"
|
|
46
|
-
|
|
47
|
-
endpoints:
|
|
48
|
-
- context: discord
|
|
49
|
-
connection: interactions
|
|
50
|
-
name: my-discord-bot
|
|
51
|
-
token: "${DISCORD_BOT_TOKEN}"
|
|
52
|
-
applicationId: "${DISCORD_APP_ID}"
|
|
53
|
-
publicKey: "${DISCORD_PUBLIC_KEY}"
|
|
54
|
-
interactionsPath: "/discord/interactions"
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
未写 `connection` 时默认为 `gateway`。
|
|
58
|
-
|
|
59
|
-
### 如何选择 connection
|
|
33
|
+
- sender role 已恢复:Gateway 入站 `metadata.role` / `metadata.permissions`(来自 member 权限位与 guild owner 判定,见 `src/gateway.ts` `resolveSenderRole`)。
|
|
34
|
+
- `plugin.ts` 在 generation setup 注册 checker,并在 dispose 注销;Plugin Runtime CapabilityIngress 与 ToolSystem 统一经 Core `canAccessTool()` 消费 `permissions`。
|
|
60
35
|
|
|
61
|
-
|
|
62
|
-
|------|----------|------|
|
|
63
|
-
| **gateway** | 常规 Bot:读消息、群管、Gateway 事件 | Endpoint Token + Intents |
|
|
64
|
-
| **interactions** | 仅需斜杠命令 / 交互组件,或 Gateway 与 HTTP 并存 | `@zhin.js/host-router` + Application ID + Public Key |
|
|
36
|
+
## 前置条件
|
|
65
37
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
38
|
+
| 要求 | 说明 |
|
|
39
|
+
|------|------|
|
|
40
|
+
| **Bot Token** | [Discord Developer Portal](https://discord.com/developers/applications) 创建应用并获取 Token |
|
|
41
|
+
| **MESSAGE CONTENT INTENT** | 需开启才能读取消息正文 |
|
|
42
|
+
| **Gateway(默认)** | 本地/生产均可;discord.js 连接 Gateway,无需公网 HTTPS |
|
|
43
|
+
| **host-http** | Gateway **不需要**;Interactions webhook 延期至下一棒 |
|
|
71
44
|
|
|
72
|
-
|
|
73
|
-
import { usePlugin, MessageCommand } from 'zhin.js'
|
|
45
|
+
必填字段(`endpoints[i]`):`name`、`token`。
|
|
74
46
|
|
|
75
|
-
|
|
47
|
+
## 最小配置
|
|
76
48
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
49
|
+
```yaml
|
|
50
|
+
# zhin.config.yml(Plugin Runtime)
|
|
51
|
+
plugins:
|
|
52
|
+
discord:
|
|
53
|
+
# connection: gateway # 默认
|
|
54
|
+
endpoints:
|
|
55
|
+
- name: my-discord-bot
|
|
56
|
+
token: ${DISCORD_BOT_TOKEN}
|
|
82
57
|
```
|
|
83
58
|
|
|
84
|
-
|
|
59
|
+
根插件 `zhin.plugins`(或项目图)需引用 `@zhin.js/adapter-discord`(`instanceKey: discord`)。
|
|
85
60
|
|
|
86
|
-
|
|
87
|
-
import { usePlugin } from 'zhin.js'
|
|
61
|
+
## 环境变量
|
|
88
62
|
|
|
89
|
-
|
|
63
|
+
| 变量 | 说明 |
|
|
64
|
+
|------|------|
|
|
65
|
+
| `DISCORD_BOT_TOKEN` | Bot Token |
|
|
66
|
+
| `DISCORD_BOT_NAME` | 可选,默认 endpoint 名 |
|
|
90
67
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
await next()
|
|
96
|
-
})
|
|
97
|
-
```
|
|
68
|
+
## Interactions(HTTP)
|
|
69
|
+
|
|
70
|
+
`connection: interactions` 经 `httpHostToken` 注册 POST 路由(默认 `/discord/interactions`),Ed25519 验签后处理 PING 与 slash command;出站走 Discord REST `channels/.../messages`。需配置 `applicationId` 与 `publicKey`。
|
|
98
71
|
|
|
99
72
|
## AI 工具(Skill)
|
|
100
73
|
|
|
@@ -103,18 +76,15 @@ root.addMiddleware(async (message, next) => {
|
|
|
103
76
|
| Permit 词汇 | `agent/PERMITS.md` |
|
|
104
77
|
| 平台工具(7 个) | `agent/tools/`(`discord_*`:角色、Embed、反应等) |
|
|
105
78
|
| 技能说明 | `agent/skills/discord.md` |
|
|
106
|
-
| 群管标准工具 | `createSceneManagementTools()` |
|
|
107
|
-
|
|
108
79
|
|
|
109
|
-
|
|
80
|
+
工具使用 Discord Snowflake ID 标识 `guild_id`、`user_id`、`channel_id`。
|
|
110
81
|
|
|
111
82
|
## Discord 开发者配置
|
|
112
83
|
|
|
113
84
|
1. 前往 [Discord Developer Portal](https://discord.com/developers/applications)
|
|
114
|
-
2. 创建应用并获取
|
|
115
|
-
3. 开启 **MESSAGE CONTENT INTENT
|
|
116
|
-
4. 通过 OAuth2 URL 邀请
|
|
117
|
-
5. Interactions 模式还需配置 Interactions Endpoint URL
|
|
85
|
+
2. 创建应用并获取 Bot Token
|
|
86
|
+
3. 开启 **MESSAGE CONTENT INTENT**
|
|
87
|
+
4. 通过 OAuth2 URL 邀请 Bot 加入服务器
|
|
118
88
|
|
|
119
89
|
## 许可证
|
|
120
90
|
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convention entry: discover `adapters/discord.ts` → defineAdapter.
|
|
3
|
+
*/
|
|
4
|
+
import { defineAdapter } from '@zhin.js/adapter';
|
|
5
|
+
import { messageGatewayToken } from '@zhin.js/core/runtime';
|
|
6
|
+
import { httpHostToken } from '@zhin.js/host-http';
|
|
7
|
+
import {
|
|
8
|
+
DiscordGatewayEndpoint,
|
|
9
|
+
DiscordInteractionsEndpoint,
|
|
10
|
+
} from '../src/endpoint.js';
|
|
11
|
+
import {
|
|
12
|
+
resolveDiscordConfig,
|
|
13
|
+
type DiscordAdapterConfig,
|
|
14
|
+
} from '../src/protocol.js';
|
|
15
|
+
|
|
16
|
+
export {
|
|
17
|
+
DiscordGatewayEndpoint,
|
|
18
|
+
DiscordInteractionsEndpoint,
|
|
19
|
+
} from '../src/endpoint.js';
|
|
20
|
+
export type {
|
|
21
|
+
CreateDiscordClient,
|
|
22
|
+
DiscordClientTransport,
|
|
23
|
+
DiscordEndpointOptions,
|
|
24
|
+
DiscordInteractionsEndpointOptions,
|
|
25
|
+
} from '../src/endpoint.js';
|
|
26
|
+
|
|
27
|
+
export default defineAdapter<DiscordAdapterConfig>({
|
|
28
|
+
capabilities: ['inbound', 'outbound'],
|
|
29
|
+
create(context) {
|
|
30
|
+
const config = resolveDiscordConfig(context.config);
|
|
31
|
+
const gateway = context.use(messageGatewayToken);
|
|
32
|
+
if (config.connection === 'interactions') {
|
|
33
|
+
return new DiscordInteractionsEndpoint({
|
|
34
|
+
id: context.id,
|
|
35
|
+
gateway,
|
|
36
|
+
http: context.use(httpHostToken),
|
|
37
|
+
config,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
return new DiscordGatewayEndpoint({
|
|
41
|
+
id: context.id,
|
|
42
|
+
gateway,
|
|
43
|
+
config,
|
|
44
|
+
});
|
|
45
|
+
},
|
|
46
|
+
});
|
package/agent/tools/add_role.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
import { platformPermit } from '../../src/platform-permit.js';
|
|
4
4
|
import { getDiscordAgentDeps } from '../../src/discord-agent-deps.js';
|
|
5
5
|
|
|
6
|
-
export default
|
|
6
|
+
export default defineAgentTool<{ endpoint_id: string; guild_id: string; user_id: string; role_id: string }>({
|
|
7
7
|
description: '给成员添加 Discord 角色',
|
|
8
8
|
inputSchema: z.object({
|
|
9
9
|
endpoint_id: z.string().describe('Endpoint 名称'),
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
import { platformPermit } from '../../src/platform-permit.js';
|
|
4
4
|
import { getDiscordAgentDeps } from '../../src/discord-agent-deps.js';
|
|
5
5
|
|
|
6
|
-
export default
|
|
6
|
+
export default defineAgentTool<{ endpoint_id: string; channel_id: string; name: string; message_id?: string; auto_archive_duration?: number }>({
|
|
7
7
|
description: '在 Discord 频道中创建帖子/子线程',
|
|
8
8
|
inputSchema: z.object({
|
|
9
9
|
endpoint_id: z.string().describe('Endpoint 名称'),
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
import { getDiscordAgentDeps } from '../../src/discord-agent-deps.js';
|
|
4
4
|
|
|
5
|
-
export default
|
|
5
|
+
export default defineAgentTool<{ endpoint_id: string; channel_id: string; name: string; content: string; tags?: string }>({
|
|
6
6
|
description: '在 Discord 论坛频道中创建帖子',
|
|
7
7
|
inputSchema: z.object({
|
|
8
8
|
endpoint_id: z.string().describe('Endpoint 名称'),
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
import { platformPermit } from '../../src/platform-permit.js';
|
|
4
4
|
import { getDiscordAgentDeps } from '../../src/discord-agent-deps.js';
|
|
5
5
|
|
|
6
|
-
export default
|
|
6
|
+
export default defineAgentTool<{ endpoint_id: string; guild_id: string }>({
|
|
7
7
|
description: '获取 Discord 服务器角色列表',
|
|
8
8
|
inputSchema: z.object({
|
|
9
9
|
endpoint_id: z.string().describe('Endpoint 名称'),
|
package/agent/tools/react.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
import { getDiscordAgentDeps } from '../../src/discord-agent-deps.js';
|
|
4
4
|
|
|
5
|
-
export default
|
|
5
|
+
export default defineAgentTool<{ endpoint_id: string; channel_id: string; message_id: string; emoji: string }>({
|
|
6
6
|
description: '对 Discord 消息添加表情反应',
|
|
7
7
|
inputSchema: z.object({
|
|
8
8
|
endpoint_id: z.string().describe('Endpoint 名称'),
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
import { platformPermit } from '../../src/platform-permit.js';
|
|
4
4
|
import { getDiscordAgentDeps } from '../../src/discord-agent-deps.js';
|
|
5
5
|
|
|
6
|
-
export default
|
|
6
|
+
export default defineAgentTool<{ endpoint_id: string; guild_id: string; user_id: string; role_id: string }>({
|
|
7
7
|
description: '移除成员的 Discord 角色',
|
|
8
8
|
inputSchema: z.object({
|
|
9
9
|
endpoint_id: z.string().describe('Endpoint 名称'),
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { defineAgentTool } from '@zhin.js/agent/tools';
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
import { getDiscordAgentDeps } from '../../src/discord-agent-deps.js';
|
|
4
4
|
|
|
5
|
-
export default
|
|
5
|
+
export default defineAgentTool<{ endpoint_id: string; channel_id: string; title?: string; description?: string; color?: number; url?: string; fields?: string }>({
|
|
6
6
|
description: '发送 Discord 富文本嵌入消息(Embed)',
|
|
7
7
|
inputSchema: z.object({
|
|
8
8
|
endpoint_id: z.string().describe('Endpoint 名称'),
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent tool deps for discord.
|
|
3
|
+
* Endpoints register themselves on start; tools look up by config name / endpoint id.
|
|
4
|
+
*/
|
|
5
|
+
export interface DiscordAgentEndpoint {
|
|
6
|
+
addRole(guildId: string, userId: string, roleId: string): Promise<boolean>;
|
|
7
|
+
removeRole(guildId: string, userId: string, roleId: string): Promise<boolean>;
|
|
8
|
+
getRoles(guildId: string): Promise<unknown[]>;
|
|
9
|
+
createThread(channelId: string, name: string, messageId?: string, autoArchiveDuration?: number): Promise<{
|
|
10
|
+
id: string;
|
|
11
|
+
}>;
|
|
12
|
+
addReaction(channelId: string, messageId: string, emoji: string): Promise<void>;
|
|
13
|
+
sendEmbed(channelId: string, embedData: Record<string, unknown>): Promise<{
|
|
14
|
+
id: string;
|
|
15
|
+
}>;
|
|
16
|
+
createForumPost(channelId: string, name: string, content: string, tags?: string[]): Promise<{
|
|
17
|
+
id: string;
|
|
18
|
+
}>;
|
|
19
|
+
kickMember(guildId: string, userId: string, reason?: string): Promise<boolean>;
|
|
20
|
+
banMember(guildId: string, userId: string, reason?: string): Promise<boolean>;
|
|
21
|
+
unbanMember(guildId: string, userId: string, reason?: string): Promise<boolean>;
|
|
22
|
+
timeoutMember(guildId: string, userId: string, duration?: number, reason?: string): Promise<boolean>;
|
|
23
|
+
setNickname(guildId: string, userId: string, nickname: string): Promise<boolean>;
|
|
24
|
+
getMembers(guildId: string, limit?: number): Promise<unknown[]>;
|
|
25
|
+
getGuildInfo(guildId: string): Promise<unknown>;
|
|
26
|
+
}
|
|
27
|
+
export interface DiscordAgentDeps {
|
|
28
|
+
getEndpoint: (endpointId: string) => DiscordAgentEndpoint;
|
|
29
|
+
/** Alias kept for existing agent/tools that call getGatewayEndpoint. */
|
|
30
|
+
getGatewayEndpoint: (endpointId: string) => DiscordAgentEndpoint;
|
|
31
|
+
}
|
|
32
|
+
export declare function registerDiscordAgentEndpoint(endpointId: string, endpoint: DiscordAgentEndpoint): () => void;
|
|
33
|
+
/** Optional override used by tests / transitional callers. Pass `null` to clear. */
|
|
34
|
+
export declare function setDiscordAgentDeps(deps: DiscordAgentDeps | null): void;
|
|
35
|
+
export declare function getDiscordAgentDeps(): DiscordAgentDeps;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent tool deps for discord.
|
|
3
|
+
* Endpoints register themselves on start; tools look up by config name / endpoint id.
|
|
4
|
+
*/
|
|
5
|
+
const endpoints = new Map();
|
|
6
|
+
let override = null;
|
|
7
|
+
export function registerDiscordAgentEndpoint(endpointId, endpoint) {
|
|
8
|
+
endpoints.set(endpointId, endpoint);
|
|
9
|
+
return () => {
|
|
10
|
+
if (endpoints.get(endpointId) === endpoint) {
|
|
11
|
+
endpoints.delete(endpointId);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
/** Optional override used by tests / transitional callers. Pass `null` to clear. */
|
|
16
|
+
export function setDiscordAgentDeps(deps) {
|
|
17
|
+
override = deps;
|
|
18
|
+
}
|
|
19
|
+
function lookup(endpointId) {
|
|
20
|
+
const registered = endpoints.get(endpointId);
|
|
21
|
+
if (!registered)
|
|
22
|
+
throw new Error(`Endpoint ${endpointId} 不存在`);
|
|
23
|
+
return registered;
|
|
24
|
+
}
|
|
25
|
+
export function getDiscordAgentDeps() {
|
|
26
|
+
if (override)
|
|
27
|
+
return override;
|
|
28
|
+
return {
|
|
29
|
+
getEndpoint: lookup,
|
|
30
|
+
getGatewayEndpoint: lookup,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { EndpointInstance } from '@zhin.js/adapter';
|
|
2
|
+
import type { MessageGateway } from '@zhin.js/core/runtime';
|
|
3
|
+
import type { HttpHost } from '@zhin.js/host-http';
|
|
4
|
+
import type { CapabilityId } from '@zhin.js/plugin-runtime';
|
|
5
|
+
import { type CreateDiscordClient } from './gateway.js';
|
|
6
|
+
import { type DiscordButtonInbound, type DiscordInboundMessage, type ResolvedDiscordGatewayConfig, type ResolvedDiscordInteractionsConfig } from './protocol.js';
|
|
7
|
+
export type { CreateDiscordClient, DiscordClientTransport, } from './gateway.js';
|
|
8
|
+
export interface DiscordEndpointOptions {
|
|
9
|
+
readonly id: CapabilityId;
|
|
10
|
+
readonly gateway: MessageGateway;
|
|
11
|
+
readonly config: ResolvedDiscordGatewayConfig;
|
|
12
|
+
readonly createClient?: CreateDiscordClient;
|
|
13
|
+
}
|
|
14
|
+
export declare class DiscordGatewayEndpoint implements EndpointInstance {
|
|
15
|
+
#private;
|
|
16
|
+
constructor(options: DiscordEndpointOptions);
|
|
17
|
+
start(): Promise<void>;
|
|
18
|
+
open(): void;
|
|
19
|
+
close(): void;
|
|
20
|
+
stop(): Promise<void>;
|
|
21
|
+
send({ target, payload }: {
|
|
22
|
+
readonly target: string;
|
|
23
|
+
readonly payload: unknown;
|
|
24
|
+
}): Promise<string>;
|
|
25
|
+
/** Test / internal: admit a message when open. */
|
|
26
|
+
admit(msg: DiscordInboundMessage): void;
|
|
27
|
+
/** Test / internal: admit a button interaction when open. */
|
|
28
|
+
admitButton(interaction: DiscordButtonInbound): void;
|
|
29
|
+
addRole(guildId: string, userId: string, roleId: string): Promise<boolean>;
|
|
30
|
+
removeRole(guildId: string, userId: string, roleId: string): Promise<boolean>;
|
|
31
|
+
getRoles(guildId: string): Promise<unknown[]>;
|
|
32
|
+
createThread(channelId: string, name: string, messageId?: string, autoArchiveDuration?: number): Promise<{
|
|
33
|
+
id: string;
|
|
34
|
+
}>;
|
|
35
|
+
addReaction(channelId: string, messageId: string, emoji: string): Promise<void>;
|
|
36
|
+
sendEmbed(channelId: string, embedData: Record<string, unknown>): Promise<{
|
|
37
|
+
id: string;
|
|
38
|
+
}>;
|
|
39
|
+
createForumPost(channelId: string, name: string, content: string, tags?: string[]): Promise<{
|
|
40
|
+
id: string;
|
|
41
|
+
}>;
|
|
42
|
+
kickMember(guildId: string, userId: string, reason?: string): Promise<boolean>;
|
|
43
|
+
banMember(guildId: string, userId: string, reason?: string): Promise<boolean>;
|
|
44
|
+
unbanMember(guildId: string, userId: string, reason?: string): Promise<boolean>;
|
|
45
|
+
timeoutMember(guildId: string, userId: string, duration?: number, reason?: string): Promise<boolean>;
|
|
46
|
+
setNickname(guildId: string, userId: string, nickname: string): Promise<boolean>;
|
|
47
|
+
getMembers(guildId: string, limit?: number): Promise<unknown[]>;
|
|
48
|
+
getGuildInfo(guildId: string): Promise<unknown>;
|
|
49
|
+
}
|
|
50
|
+
export interface DiscordInteractionsEndpointOptions {
|
|
51
|
+
readonly id: CapabilityId;
|
|
52
|
+
readonly gateway: MessageGateway;
|
|
53
|
+
readonly http: HttpHost;
|
|
54
|
+
readonly config: ResolvedDiscordInteractionsConfig;
|
|
55
|
+
readonly fetch?: typeof globalThis.fetch;
|
|
56
|
+
}
|
|
57
|
+
export declare class DiscordInteractionsEndpoint implements EndpointInstance {
|
|
58
|
+
#private;
|
|
59
|
+
constructor(options: DiscordInteractionsEndpointOptions);
|
|
60
|
+
get isOpen(): boolean;
|
|
61
|
+
get config(): ResolvedDiscordInteractionsConfig;
|
|
62
|
+
start(): Promise<void>;
|
|
63
|
+
open(): void;
|
|
64
|
+
close(): void;
|
|
65
|
+
stop(): Promise<void>;
|
|
66
|
+
send({ target, payload }: {
|
|
67
|
+
readonly target: string;
|
|
68
|
+
readonly payload: unknown;
|
|
69
|
+
}): Promise<string>;
|
|
70
|
+
admit(msg: DiscordInboundMessage): void;
|
|
71
|
+
}
|