@alemonjs/kook 0.0.3 → 0.1.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.
@@ -0,0 +1,272 @@
1
+ import axios from 'axios';
2
+ import FormData from 'form-data';
3
+ import { ApiEnum } from './typings.js';
4
+ import { config } from './config.js';
5
+ import 'fs';
6
+ import 'path';
7
+ import 'qrcode';
8
+ import 'public-ip';
9
+ import { createPicFrom } from '../../../core/utils/from.js';
10
+
11
+ /**
12
+ * api接口
13
+ */
14
+ class KOOKAPI {
15
+ API_URL = 'https://www.kookapp.cn';
16
+ /**
17
+ * KOOK服务
18
+ * @param config
19
+ * @returns
20
+ */
21
+ kookService(opstoin) {
22
+ const token = config.get('token');
23
+ const service = axios.create({
24
+ baseURL: this.API_URL,
25
+ timeout: 30000,
26
+ headers: {
27
+ Authorization: `Bot ${token}`
28
+ }
29
+ });
30
+ return service(opstoin);
31
+ }
32
+ /**
33
+ *
34
+ * @returns
35
+ */
36
+ async gateway() {
37
+ return this.kookService({
38
+ baseURL: 'https://www.kookapp.cn/api/v3/gateway/index',
39
+ method: 'get',
40
+ params: {
41
+ compress: 0
42
+ }
43
+ }).then(res => res.data);
44
+ }
45
+ /**
46
+ * ************
47
+ * 资源床单
48
+ * ***********
49
+ */
50
+ /**
51
+ * 发送buffer资源
52
+ * @param id 私信传频道id,公信传子频道id
53
+ * @param message {消息编号,图片,内容}
54
+ * @returns
55
+ */
56
+ async postImage(file, Name = 'image.jpg') {
57
+ const from = await createPicFrom(file, Name);
58
+ if (!from)
59
+ return false;
60
+ const { picData, name } = from;
61
+ const formdata = new FormData();
62
+ formdata.append('file', picData, name);
63
+ const url = await this.createUrl(formdata);
64
+ if (url)
65
+ return url;
66
+ return false;
67
+ }
68
+ /**
69
+ * 发送buffer资源
70
+ * @param id 私信传频道id,公信传子频道id
71
+ * @param message {消息编号,图片,内容}
72
+ * @returns
73
+ */
74
+ async postFile(file, Name = 'image.jpg') {
75
+ const formdata = new FormData();
76
+ formdata.append('file', [file], Name);
77
+ const url = await this.createUrl(formdata);
78
+ if (url)
79
+ return url;
80
+ return false;
81
+ }
82
+ /**
83
+ * 转存
84
+ * @param formdata
85
+ * @returns
86
+ */
87
+ async createUrl(formdata) {
88
+ return await this.kookService({
89
+ method: 'post',
90
+ url: ApiEnum.AssetCreate,
91
+ data: formdata,
92
+ headers: formdata.getHeaders()
93
+ }).then(res => res?.data);
94
+ }
95
+ /**
96
+ * *********
97
+ * 消息api
98
+ * *********
99
+ */
100
+ /**
101
+ * 创建消息
102
+ * @param data
103
+ * @returns
104
+ */
105
+ async createMessage(data) {
106
+ return await this.kookService({
107
+ method: 'post',
108
+ url: ApiEnum.MessageCreate,
109
+ data
110
+ }).then(res => res?.data);
111
+ }
112
+ /**
113
+ * 创建私聊消息
114
+ */
115
+ /**
116
+ * 创建消息
117
+ * @param target_id
118
+ * @returns
119
+ */
120
+ async userChatCreate(target_id) {
121
+ return this.kookService({
122
+ method: 'post',
123
+ url: ApiEnum.UserChatCreate,
124
+ data: {
125
+ target_id
126
+ }
127
+ }).then(res => res?.data);
128
+ }
129
+ /**
130
+ * 创建消息
131
+ * @param data
132
+ * @returns
133
+ */
134
+ async createDirectMessage(data) {
135
+ return this.kookService({
136
+ method: 'post',
137
+ url: ApiEnum.DirectMessageCreate,
138
+ data
139
+ }).then(res => res?.data);
140
+ }
141
+ /**
142
+ * 删除指定消息
143
+ * @param msg_id
144
+ * @returns
145
+ */
146
+ async messageDelete(msg_id) {
147
+ return this.kookService({
148
+ method: 'post',
149
+ url: ApiEnum.MessageDelete,
150
+ data: {
151
+ msg_id
152
+ }
153
+ }).then(res => res?.data);
154
+ }
155
+ /**
156
+ * 重编辑指定消息
157
+ * @param data
158
+ * @returns
159
+ */
160
+ async messageUpdate(data) {
161
+ return this.kookService({
162
+ method: 'post',
163
+ url: ApiEnum.MessageUpdate,
164
+ data
165
+ }).then(res => res?.data);
166
+ }
167
+ /**
168
+ * 删回应
169
+ * @param data
170
+ * @returns
171
+ */
172
+ async messageDeleteReaction(data) {
173
+ return this.kookService({
174
+ method: 'post',
175
+ url: ApiEnum.MessageDeleteReaction,
176
+ data
177
+ }).then(res => res?.data);
178
+ }
179
+ /**
180
+ * 发回应
181
+ * @param data
182
+ * @returns
183
+ */
184
+ async messageAddReaction(data) {
185
+ return this.kookService({
186
+ method: 'post',
187
+ url: ApiEnum.MessageAddReaction,
188
+ data
189
+ }).then(res => res?.data);
190
+ }
191
+ /**
192
+ * 某个回应的所有用户
193
+ * @param data
194
+ * @returns
195
+ */
196
+ async messageReactionList(params) {
197
+ return this.kookService({
198
+ method: 'get',
199
+ url: ApiEnum.MessageReactionList,
200
+ params
201
+ }).then(res => res?.data);
202
+ }
203
+ /**
204
+ * **********
205
+ * user
206
+ * *********
207
+ */
208
+ /**
209
+ * 得到当前信息
210
+ * @param guild_id
211
+ * @param user_id
212
+ * @returns
213
+ */
214
+ async userMe() {
215
+ return this.kookService({
216
+ method: 'get',
217
+ url: ApiEnum.UserMe
218
+ }).then(res => res?.data);
219
+ }
220
+ /**
221
+ * 得到用户信息
222
+ * @param guild_id
223
+ * @param user_id
224
+ * @returns
225
+ */
226
+ async userView(guild_id, user_id) {
227
+ return this.kookService({
228
+ method: 'get',
229
+ url: ApiEnum.UserView,
230
+ params: {
231
+ guild_id,
232
+ user_id
233
+ }
234
+ }).then(res => res?.data);
235
+ }
236
+ /**
237
+ * 踢出
238
+ * @param guild_id
239
+ * @param user_id
240
+ * @returns
241
+ */
242
+ async guildKickout(guild_id, user_id) {
243
+ return this.kookService({
244
+ method: 'post',
245
+ url: ApiEnum.GuildKickout,
246
+ data: {
247
+ guild_id,
248
+ target_id: user_id
249
+ }
250
+ }).then(res => res?.data);
251
+ }
252
+ /**
253
+ * 创建角色
254
+ * @param channel_id
255
+ * @param type
256
+ * @param value
257
+ * @returns
258
+ */
259
+ async channelRoleCreate(channel_id, type, value) {
260
+ return this.kookService({
261
+ method: 'post',
262
+ url: ApiEnum.ChannelRoleCreate,
263
+ data: {
264
+ channel_id,
265
+ type,
266
+ value
267
+ }
268
+ }).then(res => res?.data);
269
+ }
270
+ }
271
+
272
+ export { KOOKAPI };
@@ -0,0 +1,7 @@
1
+ import { BaseConfig } from '../../../core/config.js';
2
+
3
+ const config = new BaseConfig({
4
+ token: ''
5
+ });
6
+
7
+ export { config };
@@ -0,0 +1,124 @@
1
+ import { KOOKEventKey } from './message.js';
2
+
3
+ /**
4
+ * 事件处理集
5
+ */
6
+ const ConversationMap = {
7
+ /**
8
+ * 文字消息
9
+ */
10
+ [1]: {
11
+ public: e => KOOKEventKey['MESSAGES_PUBLIC'],
12
+ direct: e => KOOKEventKey['MESSAGES_DIRECT']
13
+ },
14
+ /**
15
+ * 图片消息,
16
+ */
17
+ [2]: {
18
+ public: e => KOOKEventKey['MESSAGES_PUBLIC'],
19
+ direct: e => KOOKEventKey['MESSAGES_DIRECT']
20
+ },
21
+ /**
22
+ * 视频消息,
23
+ */
24
+ [3]: {
25
+ public: e => KOOKEventKey['MESSAGES_PUBLIC'],
26
+ direct: e => KOOKEventKey['MESSAGES_DIRECT']
27
+ },
28
+ /**
29
+ * 文件消息,
30
+ */
31
+ [4]: {
32
+ public: e => KOOKEventKey['MESSAGES_PUBLIC'],
33
+ direct: e => KOOKEventKey['MESSAGES_DIRECT']
34
+ },
35
+ /**
36
+ * 音频消息,
37
+ */
38
+ [8]: {
39
+ public: e => KOOKEventKey['MESSAGES_PUBLIC'],
40
+ direct: e => KOOKEventKey['MESSAGES_DIRECT']
41
+ },
42
+ /**
43
+ * mk消息
44
+ */
45
+ [9]: {
46
+ public: e => KOOKEventKey['MESSAGES_PUBLIC'],
47
+ direct: e => KOOKEventKey['MESSAGES_DIRECT']
48
+ },
49
+ /**
50
+ * card消息,
51
+ */
52
+ [10]: {
53
+ public: e => KOOKEventKey['MESSAGES_PUBLIC'],
54
+ direct: e => KOOKEventKey['MESSAGES_DIRECT']
55
+ },
56
+ /**
57
+ * 系统消息
58
+ * @param event
59
+ */
60
+ [255]: {
61
+ public: (event) => {
62
+ if (event.extra.type == 'added_reaction' || event.extra.type == 'deleted_reaction') {
63
+ return KOOKEventKey['REACTIONS'];
64
+ }
65
+ else if (event.extra.type == 'joined_channel') {
66
+ console.info('joined_channel');
67
+ return '';
68
+ }
69
+ else if (event.extra.type == 'exited_channel') {
70
+ console.info('111exited_channel');
71
+ return '';
72
+ }
73
+ else if (event.extra.type == 'updated_channel') {
74
+ // ChannelData
75
+ console.info('updated_channel');
76
+ return '';
77
+ /**
78
+ * ***********
79
+ * 频道进出
80
+ * *******
81
+ */
82
+ }
83
+ else if (event.extra.type == 'joined_guild') {
84
+ console.info('joined_guild');
85
+ return KOOKEventKey['MEMBER_ADD'];
86
+ }
87
+ else if (event.extra.type == 'exited_guild') {
88
+ console.info('exited_guild');
89
+ return KOOKEventKey['MEMBER_REMOVE'];
90
+ /**
91
+ * **********
92
+ * 消息变动
93
+ * ********
94
+ */
95
+ }
96
+ else if (event.extra.type == 'updated_message') {
97
+ // 消息更新
98
+ // EditingData
99
+ console.info('updated_message');
100
+ return '';
101
+ }
102
+ else if (event.extra.type == 'pinned_message') {
103
+ // 顶置消息
104
+ // overheadData
105
+ console.info('pinned_message');
106
+ return '';
107
+ }
108
+ },
109
+ direct: (event) => {
110
+ if (event.extra.type == 'guild_member_online') {
111
+ //OnLineData
112
+ console.info('exited_guild');
113
+ return '';
114
+ }
115
+ else if (event.extra.type == 'message_btn_click') {
116
+ //按钮事件
117
+ console.info('message_btn_click');
118
+ return KOOKEventKey['INTERACTION'];
119
+ }
120
+ }
121
+ }
122
+ };
123
+
124
+ export { ConversationMap };
@@ -0,0 +1,8 @@
1
+ import { SystemData } from '../typings.js';
2
+
3
+ /**
4
+ * @param event 按钮数据
5
+ */
6
+ type INTERACTION_TYPE = SystemData;
7
+
8
+ export type { INTERACTION_TYPE };
@@ -0,0 +1,10 @@
1
+ import { SystemData } from '../typings.js';
2
+
3
+ /**
4
+ * 当成员加入时
5
+ * @param event
6
+ * @returns
7
+ */
8
+ type MEMBER_ADD_TYPE = SystemData;
9
+
10
+ export type { MEMBER_ADD_TYPE };
@@ -0,0 +1,10 @@
1
+ import { SystemData } from '../typings.js';
2
+
3
+ /**
4
+ * 当成员移除时
5
+ * @param event
6
+ * @returns
7
+ */
8
+ type MEMBER_REMOVE_TYPE = SystemData;
9
+
10
+ export type { MEMBER_REMOVE_TYPE };
@@ -0,0 +1,10 @@
1
+ import { EventData } from '../typings.js';
2
+
3
+ /**
4
+ * 私聊消息
5
+ * @param event
6
+ * @returns
7
+ */
8
+ type MESSAGES_DIRECT_TYPE = EventData;
9
+
10
+ export type { MESSAGES_DIRECT_TYPE };
@@ -0,0 +1,10 @@
1
+ import { EventData } from '../typings.js';
2
+
3
+ /**
4
+ * 群聊消息
5
+ * @param event
6
+ * @returns
7
+ */
8
+ type MESSAGES_PUBLIC_TYPE = EventData;
9
+
10
+ export type { MESSAGES_PUBLIC_TYPE };
@@ -0,0 +1,10 @@
1
+ import { SystemData } from '../typings.js';
2
+
3
+ /**
4
+ *
5
+ * @param event
6
+ * @returns
7
+ */
8
+ type REACTIONS_TYPE = SystemData;
9
+
10
+ export type { REACTIONS_TYPE };
@@ -0,0 +1,18 @@
1
+ import { MEMBER_ADD_TYPE } from './message/MEMBER_ADD.js';
2
+ import { MEMBER_REMOVE_TYPE } from './message/MEMBER_REMOVE.js';
3
+ import { INTERACTION_TYPE } from './message/INTERACTION.js';
4
+ import { MESSAGES_DIRECT_TYPE } from './message/MESSAGES_DIRECT.js';
5
+ import { MESSAGES_PUBLIC_TYPE } from './message/MESSAGES_PUBLIC.js';
6
+ import { REACTIONS_TYPE } from './message/REACTIONS.js';
7
+
8
+ type KOOKEventMap = {
9
+ MEMBER_ADD: MEMBER_ADD_TYPE;
10
+ MEMBER_REMOVE: MEMBER_REMOVE_TYPE;
11
+ INTERACTION: INTERACTION_TYPE;
12
+ MESSAGES_DIRECT: MESSAGES_DIRECT_TYPE;
13
+ MESSAGES_PUBLIC: MESSAGES_PUBLIC_TYPE;
14
+ REACTIONS: REACTIONS_TYPE;
15
+ ERROR: any;
16
+ };
17
+
18
+ export type { KOOKEventMap };
@@ -0,0 +1,18 @@
1
+ const KOOKEventKey = {
2
+ // 成员加入
3
+ MEMBER_ADD: 'MEMBER_ADD',
4
+ // 成员退出
5
+ MEMBER_REMOVE: 'MEMBER_REMOVE',
6
+ // 交互
7
+ INTERACTION: 'INTERACTION',
8
+ // 私聊消息
9
+ MESSAGES_DIRECT: 'MESSAGES_DIRECT',
10
+ // 频道消息
11
+ MESSAGES_PUBLIC: 'MESSAGES_PUBLIC',
12
+ // 系统消息
13
+ REACTIONS: 'REACTIONS',
14
+ // 错误消息
15
+ ERROR: 'ERROR'
16
+ };
17
+
18
+ export { KOOKEventKey };
@@ -0,0 +1,192 @@
1
+ type MessageType = 1 | 2 | 3 | 4 | 8 | 9 | 10 | 255;
2
+ /**
3
+ * 消息通道类型, GROUP 为组播消息, PERSON 为单播消息, BROADCAST 为广播消息
4
+ */
5
+ type MessageChannelType = 'GROUP' | 'PERSON' | 'BROADCAST';
6
+ /**
7
+ * 会话数据
8
+ */
9
+ interface EventData {
10
+ channel_type: MessageChannelType;
11
+ type: MessageType;
12
+ target_id: string;
13
+ author_id: string;
14
+ content: string;
15
+ extra: Extra;
16
+ msg_id: string;
17
+ msg_timestamp: number;
18
+ nonce: string;
19
+ from_type: number;
20
+ }
21
+ /**
22
+ * 消息发送参数
23
+ */
24
+ interface SendMessageParams {
25
+ /**
26
+ * 消息类型
27
+ */
28
+ type?: number;
29
+ /**
30
+ * 频道编号
31
+ */
32
+ target_id: string;
33
+ /**
34
+ * 消息内容
35
+ */
36
+ content: string;
37
+ /**
38
+ * 引用--消息id
39
+ */
40
+ quote?: string;
41
+ nonce?: string;
42
+ temp_target_id?: string;
43
+ }
44
+ /**
45
+ * 作者信息
46
+ */
47
+ interface Author {
48
+ id: string;
49
+ username: string;
50
+ identify_num: string;
51
+ online: boolean;
52
+ os: string;
53
+ status: number;
54
+ avatar: string;
55
+ vip_avatar: string;
56
+ banner: string;
57
+ nickname: string;
58
+ roles: string[];
59
+ is_vip: boolean;
60
+ vip_amp: boolean;
61
+ is_ai_reduce_noise: boolean;
62
+ is_personal_card_bg: boolean;
63
+ bot: boolean;
64
+ decorations_id_map: null | unknown;
65
+ is_sys: boolean;
66
+ }
67
+ /**
68
+ * mk接口
69
+ */
70
+ interface KMarkdown {
71
+ raw_content: string;
72
+ mention_part: any[];
73
+ mention_role_part: any[];
74
+ channel_part: any[];
75
+ }
76
+ /**
77
+ * 数据包
78
+ */
79
+ interface Extra {
80
+ type: number;
81
+ code: string;
82
+ guild_id: string;
83
+ guild_type: number;
84
+ channel_name: string;
85
+ author: Author;
86
+ visible_only: null;
87
+ mention: any[];
88
+ mention_all: boolean;
89
+ mention_roles: any[];
90
+ mention_here: boolean;
91
+ nav_channels: any[];
92
+ kmarkdown: KMarkdown;
93
+ emoji: any[];
94
+ last_msg_content: string;
95
+ send_msg_device: number;
96
+ }
97
+ /**
98
+ * 私聊消息
99
+ */
100
+ interface SendDirectMessageParams {
101
+ type?: MessageType;
102
+ target_id?: string;
103
+ chat_code?: string;
104
+ content: string;
105
+ quote?: string;
106
+ nonce?: string;
107
+ }
108
+ /**
109
+ * 系统数据
110
+ */
111
+ interface SystemData {
112
+ channel_type: MessageChannelType;
113
+ type: number;
114
+ target_id: string;
115
+ author_id: string;
116
+ content: string;
117
+ extra: {
118
+ type: (typeof SystemDataEnum)[number];
119
+ body: overheadData | memberData | ChannelData | StatementData | EditingData | OnLineData;
120
+ };
121
+ msg_id: string;
122
+ msg_timestamp: number;
123
+ nonce: string;
124
+ from_type: number;
125
+ }
126
+ interface OnLineData {
127
+ user_id: string;
128
+ event_time: number;
129
+ guilds: any[];
130
+ }
131
+ interface overheadData {
132
+ channel_id: string;
133
+ operator_id: string;
134
+ msg_id: string;
135
+ }
136
+ interface memberData {
137
+ user_id: string;
138
+ exited_at: number;
139
+ }
140
+ interface ChannelData {
141
+ id: string;
142
+ name: string;
143
+ user_id: string;
144
+ guild_id: string;
145
+ guild_type: number;
146
+ limit_amount: number;
147
+ is_category: number;
148
+ parent_id: string;
149
+ level: number;
150
+ slow_mode: number;
151
+ topic: string;
152
+ type: number;
153
+ permission_overwrites: any[];
154
+ permission_users: any[];
155
+ permission_sync: number;
156
+ mode: number;
157
+ has_password: boolean;
158
+ last_msg_content: string;
159
+ last_msg_id: string;
160
+ sync_guild_region: number;
161
+ region: string;
162
+ joined_at: number;
163
+ }
164
+ interface StatementData {
165
+ channel_id: string;
166
+ emoji: {
167
+ [key: string]: any;
168
+ };
169
+ user_id: string;
170
+ msg_id: string;
171
+ }
172
+ interface EditingData {
173
+ version_id: string;
174
+ channel_id: string;
175
+ target_id: string;
176
+ content: string;
177
+ mention: string[];
178
+ mention_all: boolean;
179
+ mention_here: boolean;
180
+ mention_roles: string[];
181
+ updated_at: number;
182
+ kmarkdown: any;
183
+ last_msg_content: string;
184
+ embeds: any[];
185
+ msg_id: string;
186
+ }
187
+ /**
188
+ * 系统数据可枚举
189
+ */
190
+ declare const SystemDataEnum: readonly ["exited_guild", "joined_guild", "joined_channel", "exited_channel", "updated_channel", "pinned_message", "guild_member_online", "added_reaction", "deleted_reaction", "updated_message", "message_btn_click"];
191
+
192
+ export { type Author, type ChannelData, type EditingData, type EventData, type Extra, type KMarkdown, type MessageChannelType, type MessageType, type OnLineData, type SendDirectMessageParams, type SendMessageParams, type StatementData, type SystemData, SystemDataEnum, type memberData, type overheadData };