@onebots/adapter-discord 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/LICENSE +21 -0
- package/README.md +202 -0
- package/lib/adapter.d.ts +175 -0
- package/lib/adapter.js +897 -0
- package/lib/bot.d.ts +293 -0
- package/lib/bot.js +526 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.js +8 -0
- package/lib/lite/bot.d.ts +305 -0
- package/lib/lite/bot.js +527 -0
- package/lib/lite/gateway.d.ts +119 -0
- package/lib/lite/gateway.js +349 -0
- package/lib/lite/index.d.ts +148 -0
- package/lib/lite/index.js +244 -0
- package/lib/lite/interactions.d.ts +134 -0
- package/lib/lite/interactions.js +238 -0
- package/lib/lite/rest.d.ts +102 -0
- package/lib/lite/rest.js +292 -0
- package/lib/types.d.ts +106 -0
- package/lib/types.js +64 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 凉菜
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# @onebots/adapter-discord
|
|
2
|
+
|
|
3
|
+
onebots Discord 适配器 - 轻量版实现,直接封装 Discord API,无外部依赖。
|
|
4
|
+
|
|
5
|
+
## 特性
|
|
6
|
+
|
|
7
|
+
- ✅ **轻量级**:不依赖 discord.js,包体积小
|
|
8
|
+
- ✅ **多运行时**:支持 Node.js、Cloudflare Workers、Vercel Edge
|
|
9
|
+
- ✅ **原生 fetch**:使用原生 API,兼容性好
|
|
10
|
+
- ✅ **代理支持**:使用 https-proxy-agent(可选)
|
|
11
|
+
|
|
12
|
+
## 安装
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @onebots/adapter-discord
|
|
16
|
+
|
|
17
|
+
# Node.js Gateway 模式需要 ws
|
|
18
|
+
npm install ws
|
|
19
|
+
|
|
20
|
+
# 需要代理时
|
|
21
|
+
npm install https-proxy-agent
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## 配置
|
|
25
|
+
|
|
26
|
+
在 onebots 配置文件中添加 Discord 账号配置:
|
|
27
|
+
|
|
28
|
+
```yaml
|
|
29
|
+
discord.your_bot_id:
|
|
30
|
+
token: 'your_discord_bot_token' # Discord Bot Token,必填
|
|
31
|
+
|
|
32
|
+
# 代理配置(可选)
|
|
33
|
+
proxy:
|
|
34
|
+
url: "http://127.0.0.1:7890"
|
|
35
|
+
# username: "user" # 可选
|
|
36
|
+
# password: "pass" # 可选
|
|
37
|
+
|
|
38
|
+
intents: # 可选,Gateway Intents
|
|
39
|
+
- Guilds
|
|
40
|
+
- GuildMessages
|
|
41
|
+
- GuildMembers
|
|
42
|
+
- GuildMessageReactions
|
|
43
|
+
- DirectMessages
|
|
44
|
+
- DirectMessageReactions
|
|
45
|
+
- MessageContent
|
|
46
|
+
presence: # 可选,机器人状态
|
|
47
|
+
status: online # online, idle, dnd, invisible
|
|
48
|
+
activities:
|
|
49
|
+
- name: '正在运行 onebots'
|
|
50
|
+
type: 0 # 0: Playing, 1: Streaming, 2: Listening, 3: Watching, 5: Competing
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## 独立使用(不依赖 onebots)
|
|
54
|
+
|
|
55
|
+
### Node.js Gateway 模式
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import { DiscordLite, GatewayIntents } from '@onebots/adapter-discord/lite';
|
|
59
|
+
|
|
60
|
+
const client = new DiscordLite({
|
|
61
|
+
token: process.env.DISCORD_TOKEN,
|
|
62
|
+
intents: GatewayIntents.Guilds | GatewayIntents.GuildMessages | GatewayIntents.MessageContent,
|
|
63
|
+
mode: 'gateway',
|
|
64
|
+
proxy: { url: 'http://127.0.0.1:7890' }, // 可选
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
client.on('ready', (user) => {
|
|
68
|
+
console.log(`已登录为 ${user.username}`);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
client.on('messageCreate', async (message) => {
|
|
72
|
+
if (message.content === '!ping') {
|
|
73
|
+
await client.sendMessage(message.channel_id, 'Pong!');
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
await client.start();
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Cloudflare Workers 模式
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
import { InteractionsHandler } from '@onebots/adapter-discord/lite';
|
|
84
|
+
|
|
85
|
+
export default {
|
|
86
|
+
async fetch(request: Request, env: Env): Promise<Response> {
|
|
87
|
+
const handler = new InteractionsHandler({
|
|
88
|
+
publicKey: env.DISCORD_PUBLIC_KEY,
|
|
89
|
+
token: env.DISCORD_TOKEN,
|
|
90
|
+
applicationId: env.DISCORD_APP_ID,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
handler.onCommand('ping', async () => {
|
|
94
|
+
return InteractionsHandler.messageResponse('🏓 Pong!');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
return handler.handleRequest(request);
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### 直接使用 REST API
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
import { DiscordREST } from '@onebots/adapter-discord/lite';
|
|
106
|
+
|
|
107
|
+
const rest = new DiscordREST({ token: process.env.DISCORD_TOKEN });
|
|
108
|
+
|
|
109
|
+
// 发送消息
|
|
110
|
+
await rest.createMessage('channel_id', 'Hello!');
|
|
111
|
+
|
|
112
|
+
// 获取用户
|
|
113
|
+
const user = await rest.getUser('user_id');
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## 获取 Discord Bot Token
|
|
117
|
+
|
|
118
|
+
1. 前往 [Discord Developer Portal](https://discord.com/developers/applications)
|
|
119
|
+
2. 点击 "New Application" 创建新应用
|
|
120
|
+
3. 进入应用后,点击左侧 "Bot" 菜单
|
|
121
|
+
4. 点击 "Reset Token" 获取 Bot Token
|
|
122
|
+
5. 在 "Privileged Gateway Intents" 中启用需要的 Intents
|
|
123
|
+
|
|
124
|
+
## 支持的 API
|
|
125
|
+
|
|
126
|
+
### 消息相关
|
|
127
|
+
- ✅ sendMessage - 发送消息
|
|
128
|
+
- ✅ deleteMessage - 删除消息
|
|
129
|
+
- ✅ getMessage - 获取消息
|
|
130
|
+
- ✅ getMessageHistory - 获取历史消息
|
|
131
|
+
|
|
132
|
+
### 用户相关
|
|
133
|
+
- ✅ getLoginInfo - 获取机器人信息
|
|
134
|
+
- ✅ getUserInfo - 获取用户信息
|
|
135
|
+
|
|
136
|
+
### 群组(服务器)相关
|
|
137
|
+
- ✅ getGroupList - 获取服务器列表
|
|
138
|
+
- ✅ getGroupInfo - 获取服务器信息
|
|
139
|
+
- ✅ leaveGroup - 退出服务器
|
|
140
|
+
- ✅ getGroupMemberList - 获取成员列表
|
|
141
|
+
- ✅ getGroupMemberInfo - 获取成员信息
|
|
142
|
+
- ✅ kickGroupMember - 踢出成员
|
|
143
|
+
- ✅ muteGroupMember - 禁言成员
|
|
144
|
+
- ✅ setGroupCard - 设置昵称
|
|
145
|
+
|
|
146
|
+
### 频道相关
|
|
147
|
+
- ✅ getChannelInfo - 获取频道信息
|
|
148
|
+
- ✅ getChannelList - 获取频道列表
|
|
149
|
+
- ✅ createChannel - 创建频道
|
|
150
|
+
- ✅ deleteChannel - 删除频道
|
|
151
|
+
- ✅ updateChannel - 更新频道
|
|
152
|
+
|
|
153
|
+
## 依赖说明
|
|
154
|
+
|
|
155
|
+
本适配器采用轻量级设计,核心功能无需外部依赖。以下为可选依赖:
|
|
156
|
+
|
|
157
|
+
| 依赖 | 何时需要 | 安装命令 |
|
|
158
|
+
|------|----------|----------|
|
|
159
|
+
| `ws` | Node.js Gateway 模式 | `npm install ws` |
|
|
160
|
+
| `https-proxy-agent` | 使用 HTTP/HTTPS 代理(REST API) | `npm install https-proxy-agent` |
|
|
161
|
+
| `socks-proxy-agent` | 使用 SOCKS5 代理(WebSocket 推荐) | `npm install socks-proxy-agent` |
|
|
162
|
+
|
|
163
|
+
### 常见问题
|
|
164
|
+
|
|
165
|
+
#### 1. 连接超时 / ECONNRESET
|
|
166
|
+
|
|
167
|
+
如果你在中国大陆等需要代理的地区,请配置代理:
|
|
168
|
+
|
|
169
|
+
```yaml
|
|
170
|
+
discord.your_bot:
|
|
171
|
+
token: 'xxx'
|
|
172
|
+
proxy:
|
|
173
|
+
url: "http://127.0.0.1:7890" # 你的代理地址
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
并安装代理依赖:
|
|
177
|
+
|
|
178
|
+
```bash
|
|
179
|
+
# 推荐同时安装(WebSocket 使用 SOCKS5 更稳定)
|
|
180
|
+
npm install https-proxy-agent socks-proxy-agent
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
#### 2. 缺少 ws 模块
|
|
184
|
+
|
|
185
|
+
如果看到 `Cannot find module 'ws'` 错误:
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
npm install ws
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
> **注意**:Cloudflare Workers 模式(Interactions)不需要 `ws`
|
|
192
|
+
|
|
193
|
+
#### 3. WebSocket 连接失败但 REST API 正常
|
|
194
|
+
|
|
195
|
+
某些代理软件(如 Clash)的 HTTP 代理模式对 WebSocket 支持不佳。适配器会自动将 HTTP 代理转换为 SOCKS5,但需要:
|
|
196
|
+
|
|
197
|
+
1. 确保代理软件开启了混合端口(同时支持 HTTP 和 SOCKS5)
|
|
198
|
+
2. 安装 `socks-proxy-agent`:`npm install socks-proxy-agent`
|
|
199
|
+
|
|
200
|
+
## 许可证
|
|
201
|
+
|
|
202
|
+
MIT
|
package/lib/adapter.d.ts
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Discord 适配器
|
|
3
|
+
* 轻量版实现,直接封装 Discord API
|
|
4
|
+
*/
|
|
5
|
+
import { Account } from "onebots";
|
|
6
|
+
import { Adapter } from "onebots";
|
|
7
|
+
import { BaseApp } from "onebots";
|
|
8
|
+
import { DiscordBot } from "./bot.js";
|
|
9
|
+
import type { DiscordConfig } from "./types.js";
|
|
10
|
+
export declare class DiscordAdapter extends Adapter<DiscordBot, "discord"> {
|
|
11
|
+
constructor(app: BaseApp);
|
|
12
|
+
/**
|
|
13
|
+
* 发送消息
|
|
14
|
+
* 支持私聊(DM)、群组(Guild)和频道(Channel)消息
|
|
15
|
+
*/
|
|
16
|
+
sendMessage(uin: string, params: Adapter.SendMessageParams): Promise<Adapter.SendMessageResult>;
|
|
17
|
+
/**
|
|
18
|
+
* 删除/撤回消息
|
|
19
|
+
*/
|
|
20
|
+
deleteMessage(uin: string, params: Adapter.DeleteMessageParams): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* 获取消息
|
|
23
|
+
*/
|
|
24
|
+
getMessage(uin: string, params: Adapter.GetMessageParams): Promise<Adapter.MessageInfo>;
|
|
25
|
+
/**
|
|
26
|
+
* 获取历史消息
|
|
27
|
+
*/
|
|
28
|
+
getMessageHistory(uin: string, params: Adapter.GetMessageHistoryParams): Promise<Adapter.MessageInfo[]>;
|
|
29
|
+
/**
|
|
30
|
+
* 获取机器人信息
|
|
31
|
+
*/
|
|
32
|
+
getLoginInfo(uin: string): Promise<Adapter.UserInfo>;
|
|
33
|
+
/**
|
|
34
|
+
* 获取用户信息
|
|
35
|
+
*/
|
|
36
|
+
getUserInfo(uin: string, params: Adapter.GetUserInfoParams): Promise<Adapter.UserInfo>;
|
|
37
|
+
/**
|
|
38
|
+
* 获取好友列表
|
|
39
|
+
* Discord 没有传统好友系统,返回空列表
|
|
40
|
+
*/
|
|
41
|
+
getFriendList(uin: string, params?: Adapter.GetFriendListParams): Promise<Adapter.FriendInfo[]>;
|
|
42
|
+
/**
|
|
43
|
+
* 获取好友信息
|
|
44
|
+
* Discord 没有传统好友系统,返回用户信息
|
|
45
|
+
*/
|
|
46
|
+
getFriendInfo(uin: string, params: Adapter.GetFriendInfoParams): Promise<Adapter.FriendInfo>;
|
|
47
|
+
/**
|
|
48
|
+
* 获取群列表(服务器列表)
|
|
49
|
+
*/
|
|
50
|
+
getGroupList(uin: string, params?: Adapter.GetGroupListParams): Promise<Adapter.GroupInfo[]>;
|
|
51
|
+
/**
|
|
52
|
+
* 获取群信息(服务器信息)
|
|
53
|
+
*/
|
|
54
|
+
getGroupInfo(uin: string, params: Adapter.GetGroupInfoParams): Promise<Adapter.GroupInfo>;
|
|
55
|
+
/**
|
|
56
|
+
* 退出群组(服务器)
|
|
57
|
+
*/
|
|
58
|
+
leaveGroup(uin: string, params: Adapter.LeaveGroupParams): Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* 获取群成员列表
|
|
61
|
+
*/
|
|
62
|
+
getGroupMemberList(uin: string, params: Adapter.GetGroupMemberListParams): Promise<Adapter.GroupMemberInfo[]>;
|
|
63
|
+
/**
|
|
64
|
+
* 获取群成员信息
|
|
65
|
+
*/
|
|
66
|
+
getGroupMemberInfo(uin: string, params: Adapter.GetGroupMemberInfoParams): Promise<Adapter.GroupMemberInfo>;
|
|
67
|
+
/**
|
|
68
|
+
* 踢出群成员
|
|
69
|
+
*/
|
|
70
|
+
kickGroupMember(uin: string, params: Adapter.KickGroupMemberParams): Promise<void>;
|
|
71
|
+
/**
|
|
72
|
+
* 群成员禁言(超时)
|
|
73
|
+
*/
|
|
74
|
+
muteGroupMember(uin: string, params: Adapter.MuteGroupMemberParams): Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* 设置群名片(昵称)
|
|
77
|
+
*/
|
|
78
|
+
setGroupCard(uin: string, params: Adapter.SetGroupCardParams): Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* 发送群消息表情回应
|
|
81
|
+
*/
|
|
82
|
+
sendGroupMessageReaction(uin: string, params: Adapter.SendGroupMessageReactionParams): Promise<void>;
|
|
83
|
+
/**
|
|
84
|
+
* 获取频道服务器信息
|
|
85
|
+
*/
|
|
86
|
+
getGuildInfo(uin: string, params: Adapter.GetGuildInfoParams): Promise<Adapter.GuildInfo>;
|
|
87
|
+
/**
|
|
88
|
+
* 获取频道服务器列表
|
|
89
|
+
*/
|
|
90
|
+
getGuildList(uin: string): Promise<Adapter.GuildInfo[]>;
|
|
91
|
+
/**
|
|
92
|
+
* 获取频道成员信息
|
|
93
|
+
*/
|
|
94
|
+
getGuildMemberInfo(uin: string, params: Adapter.GetGuildMemberInfoParams): Promise<Adapter.GuildMemberInfo>;
|
|
95
|
+
/**
|
|
96
|
+
* 获取频道信息
|
|
97
|
+
*/
|
|
98
|
+
getChannelInfo(uin: string, params: Adapter.GetChannelInfoParams): Promise<Adapter.ChannelInfo>;
|
|
99
|
+
/**
|
|
100
|
+
* 获取频道列表
|
|
101
|
+
*/
|
|
102
|
+
getChannelList(uin: string, params?: Adapter.GetChannelListParams): Promise<Adapter.ChannelInfo[]>;
|
|
103
|
+
/**
|
|
104
|
+
* 创建频道
|
|
105
|
+
*/
|
|
106
|
+
createChannel(uin: string, params: Adapter.CreateChannelParams): Promise<Adapter.ChannelInfo>;
|
|
107
|
+
/**
|
|
108
|
+
* 删除频道
|
|
109
|
+
*/
|
|
110
|
+
deleteChannel(uin: string, params: Adapter.DeleteChannelParams): Promise<void>;
|
|
111
|
+
/**
|
|
112
|
+
* 更新频道
|
|
113
|
+
*/
|
|
114
|
+
updateChannel(uin: string, params: Adapter.UpdateChannelParams): Promise<void>;
|
|
115
|
+
/**
|
|
116
|
+
* 获取频道成员信息
|
|
117
|
+
*/
|
|
118
|
+
getChannelMemberInfo(uin: string, params: Adapter.GetChannelMemberInfoParams): Promise<Adapter.ChannelMemberInfo>;
|
|
119
|
+
/**
|
|
120
|
+
* 获取频道成员列表
|
|
121
|
+
*/
|
|
122
|
+
getChannelMemberList(uin: string, params: Adapter.GetChannelMemberListParams): Promise<Adapter.ChannelMemberInfo[]>;
|
|
123
|
+
/**
|
|
124
|
+
* 踢出频道成员
|
|
125
|
+
*/
|
|
126
|
+
kickChannelMember(uin: string, params: Adapter.KickChannelMemberParams): Promise<void>;
|
|
127
|
+
/**
|
|
128
|
+
* 设置频道成员禁言
|
|
129
|
+
*/
|
|
130
|
+
setChannelMemberMute(uin: string, params: Adapter.SetChannelMemberMuteParams): Promise<void>;
|
|
131
|
+
/**
|
|
132
|
+
* 检查是否可以发送图片
|
|
133
|
+
*/
|
|
134
|
+
canSendImage(uin: string): Promise<boolean>;
|
|
135
|
+
/**
|
|
136
|
+
* 检查是否可以发送语音
|
|
137
|
+
*/
|
|
138
|
+
canSendRecord(uin: string): Promise<boolean>;
|
|
139
|
+
/**
|
|
140
|
+
* 获取版本信息
|
|
141
|
+
*/
|
|
142
|
+
getVersion(uin: string): Promise<Adapter.VersionInfo>;
|
|
143
|
+
/**
|
|
144
|
+
* 获取运行状态
|
|
145
|
+
*/
|
|
146
|
+
getStatus(uin: string): Promise<Adapter.StatusInfo>;
|
|
147
|
+
createAccount(config: Account.Config<'discord'>): Account<'discord', DiscordBot>;
|
|
148
|
+
/**
|
|
149
|
+
* 构建 Discord 消息内容
|
|
150
|
+
*/
|
|
151
|
+
private buildDiscordMessage;
|
|
152
|
+
/**
|
|
153
|
+
* 转换消息为 MessageInfo
|
|
154
|
+
*/
|
|
155
|
+
private convertMessageToInfo;
|
|
156
|
+
/**
|
|
157
|
+
* 获取成员角色
|
|
158
|
+
*/
|
|
159
|
+
private getMemberRole;
|
|
160
|
+
}
|
|
161
|
+
declare module "onebots" {
|
|
162
|
+
namespace Adapter {
|
|
163
|
+
interface Configs {
|
|
164
|
+
discord: DiscordConfig;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
declare module './adapter.js' {
|
|
169
|
+
namespace Adapter {
|
|
170
|
+
interface Configs {
|
|
171
|
+
discord: DiscordConfig;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
//# sourceMappingURL=adapter.d.ts.map
|