@alemonjs/qq-bot 0.0.11 → 0.0.13
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/README.md +19 -8
- package/dist/assets/index.css +1 -1
- package/dist/assets/index.js +1 -1
- package/lib/api.d.ts +971 -843
- package/lib/api.js +1172 -1156
- package/lib/client.d.ts +22 -22
- package/lib/client.js +201 -217
- package/lib/config.js +2 -2
- package/lib/desktop.js +4 -2
- package/lib/from.js +27 -34
- package/lib/index.d.ts +3 -3
- package/lib/index.group.js +238 -0
- package/lib/index.guild.js +338 -0
- package/lib/index.js +43 -2
- package/lib/message/AT_MESSAGE_CREATE.d.ts +35 -35
- package/lib/message/C2C_MESSAGE_CREATE.d.ts +9 -9
- package/lib/message/CHANNEL_CREATE.d.ts +15 -15
- package/lib/message/CHANNEL_DELETE.d.ts +15 -15
- package/lib/message/CHANNEL_UPDATE.d.ts +15 -15
- package/lib/message/DIRECT_MESSAGE_CREATE.d.ts +29 -29
- package/lib/message/DIRECT_MESSAGE_DELETE.d.ts +17 -17
- package/lib/message/ERROR.d.ts +2 -2
- package/lib/message/GROUP_AT_MESSAGE_CREATE.d.ts +10 -10
- package/lib/message/GUILD_CREATE.d.ts +15 -15
- package/lib/message/GUILD_DELETE.d.ts +15 -15
- package/lib/message/GUILD_MEMBER_ADD.d.ts +14 -14
- package/lib/message/GUILD_MEMBER_REMOVE.d.ts +14 -14
- package/lib/message/GUILD_MEMBER_UPDATE.d.ts +14 -14
- package/lib/message/GUILD_UPDATE.d.ts +15 -15
- package/lib/message/INTERACTION_CREATE.d.ts +2 -2
- package/lib/message/MESSAGE_CREATE.d.ts +2 -2
- package/lib/message/MESSAGE_DELETE.d.ts +2 -2
- package/lib/message/MESSAGE_REACTION_ADD.d.ts +13 -13
- package/lib/message/MESSAGE_REACTION_REMOVE.d.ts +13 -13
- package/lib/message/PUBLIC_MESSAGE_DELETE.d.ts +15 -15
- package/lib/message/READY.d.ts +6 -6
- package/lib/message.d.ts +46 -46
- package/lib/sdk/api.d.ts +847 -0
- package/lib/sdk/api.js +1183 -0
- package/lib/sdk/client.js +228 -0
- package/lib/sdk/config.js +3 -0
- package/lib/sdk/counter.js +19 -0
- package/lib/sdk/from.js +44 -0
- package/lib/sdk/intents.js +102 -0
- package/lib/sdk/typing.d.ts +56 -0
- package/lib/sdk/webhook.secret.js +53 -0
- package/lib/sdk/websoket.group.js +221 -0
- package/lib/sdk/websoket.guild.js +203 -0
- package/lib/send/index.js +87 -21
- package/lib/typing.d.ts +62 -54
- package/lib/utils.js +14 -0
- package/lib/webhook.js +46 -48
- package/package.json +1 -7
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { defineBot, getConfigValue, useUserHashKey, OnProcessor } from 'alemonjs';
|
|
2
|
+
import { AT_MESSAGE_CREATE, GROUP_AT_MESSAGE_CREATE, DIRECT_MESSAGE_CREATE, C2C_MESSAGE_CREATE, MESSAGE_CREATE } from './send/index.js';
|
|
3
|
+
import { QQBotGroupClient } from './sdk/websoket.group.js';
|
|
4
|
+
import { isGuild } from './utils.js';
|
|
5
|
+
|
|
6
|
+
const platform = 'qq-bot';
|
|
7
|
+
var QQBotGroup = defineBot(() => {
|
|
8
|
+
let value = getConfigValue();
|
|
9
|
+
if (!value)
|
|
10
|
+
value = {};
|
|
11
|
+
const config = value[platform];
|
|
12
|
+
// intents 需要默认值
|
|
13
|
+
const client = new QQBotGroupClient({
|
|
14
|
+
app_id: config?.app_id,
|
|
15
|
+
intents: config?.intents ?? ['GROUP_AND_C2C_EVENT'],
|
|
16
|
+
is_private: config?.is_private,
|
|
17
|
+
sandbox: config?.sandbox,
|
|
18
|
+
secret: config?.secret,
|
|
19
|
+
shard: config?.shard,
|
|
20
|
+
token: config?.token,
|
|
21
|
+
mode: config?.mode
|
|
22
|
+
});
|
|
23
|
+
// 连接
|
|
24
|
+
client.connect();
|
|
25
|
+
/**
|
|
26
|
+
* group
|
|
27
|
+
*
|
|
28
|
+
* GROUP_AT_MESSAGE_CREATE
|
|
29
|
+
* C2C_MESSAGE_CREATE
|
|
30
|
+
*/
|
|
31
|
+
// 监听消息
|
|
32
|
+
client.on('GROUP_AT_MESSAGE_CREATE', async (event) => {
|
|
33
|
+
const master_key = config?.master_key ?? [];
|
|
34
|
+
const isMaster = master_key.includes(event.author.id);
|
|
35
|
+
const url = `https://q.qlogo.cn/qqapp/${config.app_id}/${event.author.id}/640`;
|
|
36
|
+
const UserAvatar = {
|
|
37
|
+
toBuffer: async () => {
|
|
38
|
+
const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
|
|
39
|
+
return Buffer.from(arrayBuffer);
|
|
40
|
+
},
|
|
41
|
+
toBase64: async () => {
|
|
42
|
+
const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
|
|
43
|
+
return Buffer.from(arrayBuffer).toString('base64');
|
|
44
|
+
},
|
|
45
|
+
toURL: async () => {
|
|
46
|
+
return url;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const UserId = event.author.id;
|
|
50
|
+
const UserKey = useUserHashKey({
|
|
51
|
+
Platform: platform,
|
|
52
|
+
UserId: UserId
|
|
53
|
+
});
|
|
54
|
+
// 定义消
|
|
55
|
+
const e = {
|
|
56
|
+
name: 'message.create',
|
|
57
|
+
// 事件类型
|
|
58
|
+
Platform: platform,
|
|
59
|
+
// guild
|
|
60
|
+
GuildId: event.group_id,
|
|
61
|
+
ChannelId: event.group_id,
|
|
62
|
+
// 用户Id
|
|
63
|
+
UserId: event.author.id,
|
|
64
|
+
UserKey,
|
|
65
|
+
UserAvatar: UserAvatar,
|
|
66
|
+
IsMaster: isMaster,
|
|
67
|
+
IsBot: false,
|
|
68
|
+
// 格式化数据
|
|
69
|
+
MessageId: event.id,
|
|
70
|
+
MessageText: event.content?.trim(),
|
|
71
|
+
OpenId: event.author.member_openid,
|
|
72
|
+
CreateAt: Date.now(),
|
|
73
|
+
tag: 'GROUP_AT_MESSAGE_CREATE',
|
|
74
|
+
value: null
|
|
75
|
+
};
|
|
76
|
+
// 当访问的时候获取
|
|
77
|
+
Object.defineProperty(e, 'value', {
|
|
78
|
+
get() {
|
|
79
|
+
return event;
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
// 处理消息
|
|
83
|
+
OnProcessor(e, 'message.create');
|
|
84
|
+
});
|
|
85
|
+
client.on('C2C_MESSAGE_CREATE', async (event) => {
|
|
86
|
+
const master_key = config?.master_key ?? [];
|
|
87
|
+
const isMaster = master_key.includes(event.author.id);
|
|
88
|
+
const url = `https://q.qlogo.cn/qqapp/${config.app_id}/${event.author.id}/640`;
|
|
89
|
+
const UserAvatar = {
|
|
90
|
+
toBuffer: async () => {
|
|
91
|
+
const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
|
|
92
|
+
return Buffer.from(arrayBuffer);
|
|
93
|
+
},
|
|
94
|
+
toBase64: async () => {
|
|
95
|
+
const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
|
|
96
|
+
return Buffer.from(arrayBuffer).toString('base64');
|
|
97
|
+
},
|
|
98
|
+
toURL: async () => {
|
|
99
|
+
return url;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
const UserId = event.author.id;
|
|
103
|
+
const UserKey = useUserHashKey({
|
|
104
|
+
Platform: platform,
|
|
105
|
+
UserId: UserId
|
|
106
|
+
});
|
|
107
|
+
// 定义消
|
|
108
|
+
const e = {
|
|
109
|
+
name: 'private.message.create',
|
|
110
|
+
// 事件类型
|
|
111
|
+
Platform: platform,
|
|
112
|
+
// 用户Id
|
|
113
|
+
UserId: event.author.id,
|
|
114
|
+
UserKey,
|
|
115
|
+
UserAvatar: UserAvatar,
|
|
116
|
+
IsMaster: isMaster,
|
|
117
|
+
IsBot: false,
|
|
118
|
+
// 格式化数据
|
|
119
|
+
MessageId: event.id,
|
|
120
|
+
MessageText: event.content?.trim(),
|
|
121
|
+
CreateAt: Date.now(),
|
|
122
|
+
OpenId: event.author.user_openid,
|
|
123
|
+
//
|
|
124
|
+
tag: 'C2C_MESSAGE_CREATE',
|
|
125
|
+
value: null
|
|
126
|
+
};
|
|
127
|
+
// 当访问的时候获取
|
|
128
|
+
Object.defineProperty(e, 'value', {
|
|
129
|
+
get() {
|
|
130
|
+
return event;
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
// 处理消息
|
|
134
|
+
OnProcessor(e, 'private.message.create');
|
|
135
|
+
});
|
|
136
|
+
client.on('ERROR', console.error);
|
|
137
|
+
// FRIEND_ADD
|
|
138
|
+
global.client = client;
|
|
139
|
+
return {
|
|
140
|
+
platform,
|
|
141
|
+
api: {
|
|
142
|
+
// 主动消息
|
|
143
|
+
active: {
|
|
144
|
+
send: {
|
|
145
|
+
channel: async (channel_id, data) => {
|
|
146
|
+
if (isGuild(channel_id)) {
|
|
147
|
+
return await AT_MESSAGE_CREATE(client, {
|
|
148
|
+
ChannelId: channel_id
|
|
149
|
+
}, data);
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
// 需要message_id 。如果没有,则是主动消息,在group中,只能发送4条。
|
|
153
|
+
return await GROUP_AT_MESSAGE_CREATE(client, {
|
|
154
|
+
GuildId: channel_id
|
|
155
|
+
}, data);
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
user: async (user_id, data) => {
|
|
159
|
+
if (isGuild(user_id)) {
|
|
160
|
+
return await DIRECT_MESSAGE_CREATE(client, {
|
|
161
|
+
OpenId: user_id
|
|
162
|
+
}, data);
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
return await C2C_MESSAGE_CREATE(client, {
|
|
166
|
+
OpenId: user_id
|
|
167
|
+
}, data);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
use: {
|
|
173
|
+
send: async (event, val) => {
|
|
174
|
+
if (val.length < 0)
|
|
175
|
+
;
|
|
176
|
+
// 打 tag
|
|
177
|
+
const tag = event.tag;
|
|
178
|
+
try {
|
|
179
|
+
if (tag == 'GROUP_AT_MESSAGE_CREATE') {
|
|
180
|
+
return await GROUP_AT_MESSAGE_CREATE(client, event, val);
|
|
181
|
+
}
|
|
182
|
+
if (tag == 'C2C_MESSAGE_CREATE') {
|
|
183
|
+
return await C2C_MESSAGE_CREATE(client, event, val);
|
|
184
|
+
}
|
|
185
|
+
if (tag == 'DIRECT_MESSAGE_CREATE') {
|
|
186
|
+
return await DIRECT_MESSAGE_CREATE(client, event, val);
|
|
187
|
+
}
|
|
188
|
+
if (tag == 'AT_MESSAGE_CREATE') {
|
|
189
|
+
return await AT_MESSAGE_CREATE(client, event, val);
|
|
190
|
+
}
|
|
191
|
+
if (tag == 'MESSAGE_CREATE') {
|
|
192
|
+
return await AT_MESSAGE_CREATE(client, event, val);
|
|
193
|
+
}
|
|
194
|
+
if (tag == 'MESSAGE_CREATE') {
|
|
195
|
+
return await MESSAGE_CREATE(client, event, val);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
catch (error) {
|
|
199
|
+
return [error];
|
|
200
|
+
}
|
|
201
|
+
return [];
|
|
202
|
+
},
|
|
203
|
+
mention: async (e) => {
|
|
204
|
+
const event = e.value;
|
|
205
|
+
const tag = e.tag;
|
|
206
|
+
// const event = e.value
|
|
207
|
+
const Metions = [];
|
|
208
|
+
// group
|
|
209
|
+
if (tag == 'GROUP_AT_MESSAGE_CREATE' || 'C2C_MESSAGE_CREATE')
|
|
210
|
+
return Metions;
|
|
211
|
+
// guild
|
|
212
|
+
if (event.mentions) {
|
|
213
|
+
const mentions = e.value['mentions'];
|
|
214
|
+
// 艾特消息处理
|
|
215
|
+
const MessageMention = mentions.map(item => {
|
|
216
|
+
return {
|
|
217
|
+
UserId: item.id,
|
|
218
|
+
IsMaster: false,
|
|
219
|
+
UserName: item.username,
|
|
220
|
+
IsBot: item.bot,
|
|
221
|
+
UserKey: useUserHashKey({
|
|
222
|
+
Platform: 'qq-guild-bot',
|
|
223
|
+
UserId: item.id
|
|
224
|
+
})
|
|
225
|
+
};
|
|
226
|
+
}) ?? [];
|
|
227
|
+
return MessageMention;
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
return Metions;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
export { QQBotGroup as default, platform };
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
import { defineBot, getConfigValue, useUserHashKey, OnProcessor } from 'alemonjs';
|
|
2
|
+
import { AT_MESSAGE_CREATE, GROUP_AT_MESSAGE_CREATE, DIRECT_MESSAGE_CREATE, C2C_MESSAGE_CREATE, MESSAGE_CREATE } from './send/index.js';
|
|
3
|
+
import { QQBotGuildClient } from './sdk/websoket.guild.js';
|
|
4
|
+
import { isGuild } from './utils.js';
|
|
5
|
+
|
|
6
|
+
const platform = 'qq-bot';
|
|
7
|
+
var QQBotGuild = defineBot(() => {
|
|
8
|
+
let value = getConfigValue();
|
|
9
|
+
if (!value)
|
|
10
|
+
value = {};
|
|
11
|
+
const config = value[platform];
|
|
12
|
+
// intents 需要默认值
|
|
13
|
+
const client = new QQBotGuildClient({
|
|
14
|
+
app_id: config?.app_id,
|
|
15
|
+
intents: config?.intents ?? [
|
|
16
|
+
'GUILDS', //频道进出
|
|
17
|
+
'GUILD_MEMBERS', //成员资料
|
|
18
|
+
'DIRECT_MESSAGE', //私信
|
|
19
|
+
'PUBLIC_GUILD_MESSAGES', //公域事件
|
|
20
|
+
'REACTIONS' // 表情表态
|
|
21
|
+
],
|
|
22
|
+
is_private: config?.is_private,
|
|
23
|
+
sandbox: config?.sandbox,
|
|
24
|
+
secret: config?.secret,
|
|
25
|
+
shard: config?.shard,
|
|
26
|
+
token: config?.token,
|
|
27
|
+
mode: config?.mode
|
|
28
|
+
});
|
|
29
|
+
// 连接
|
|
30
|
+
client.connect();
|
|
31
|
+
/**
|
|
32
|
+
* guild
|
|
33
|
+
*/
|
|
34
|
+
client.on('DIRECT_MESSAGE_CREATE', async (event) => {
|
|
35
|
+
// 屏蔽其他机器人的消息
|
|
36
|
+
if (event?.author?.bot)
|
|
37
|
+
return;
|
|
38
|
+
const master_key = config?.master_key ?? [];
|
|
39
|
+
const isMaster = master_key.includes(event.author.id);
|
|
40
|
+
let msg = event?.content ?? '';
|
|
41
|
+
const UserAvatar = {
|
|
42
|
+
toBuffer: async () => {
|
|
43
|
+
const arrayBuffer = await fetch(event.author.avatar).then(res => res.arrayBuffer());
|
|
44
|
+
return Buffer.from(arrayBuffer);
|
|
45
|
+
},
|
|
46
|
+
toBase64: async () => {
|
|
47
|
+
const arrayBuffer = await fetch(event?.author?.avatar).then(res => res.arrayBuffer());
|
|
48
|
+
return Buffer.from(arrayBuffer).toString('base64');
|
|
49
|
+
},
|
|
50
|
+
toURL: async () => {
|
|
51
|
+
return event?.author?.avatar;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
const UserId = event.author.id;
|
|
55
|
+
const UserKey = useUserHashKey({
|
|
56
|
+
Platform: platform,
|
|
57
|
+
UserId: UserId
|
|
58
|
+
});
|
|
59
|
+
// 定义消
|
|
60
|
+
const e = {
|
|
61
|
+
name: 'private.message.create',
|
|
62
|
+
// 事件类型
|
|
63
|
+
Platform: platform,
|
|
64
|
+
//
|
|
65
|
+
// GuildId: event.guild_id,
|
|
66
|
+
// ChannelId: event.channel_id,
|
|
67
|
+
// 用户Id
|
|
68
|
+
UserId: event?.author?.id ?? '',
|
|
69
|
+
UserKey,
|
|
70
|
+
UserName: event?.author?.username ?? '',
|
|
71
|
+
UserAvatar: UserAvatar,
|
|
72
|
+
IsMaster: isMaster,
|
|
73
|
+
IsBot: event.author?.bot,
|
|
74
|
+
// message
|
|
75
|
+
MessageId: event.id,
|
|
76
|
+
MessageText: msg?.trim(),
|
|
77
|
+
OpenId: event.guild_id,
|
|
78
|
+
CreateAt: Date.now(),
|
|
79
|
+
//
|
|
80
|
+
tag: 'DIRECT_MESSAGE_CREATE',
|
|
81
|
+
//
|
|
82
|
+
value: null
|
|
83
|
+
};
|
|
84
|
+
// 当访问的时候获取
|
|
85
|
+
Object.defineProperty(e, 'value', {
|
|
86
|
+
get() {
|
|
87
|
+
return event;
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
// 处理消息
|
|
91
|
+
OnProcessor(e, 'private.message.create');
|
|
92
|
+
});
|
|
93
|
+
// 监听消息
|
|
94
|
+
client.on('AT_MESSAGE_CREATE', async (event) => {
|
|
95
|
+
// 屏蔽其他机器人的消息
|
|
96
|
+
if (event?.author?.bot)
|
|
97
|
+
return;
|
|
98
|
+
const master_key = config?.master_key ?? [];
|
|
99
|
+
const isMaster = master_key.includes(event.author.id);
|
|
100
|
+
let msg = getMessageContent(event);
|
|
101
|
+
const UserAvatar = {
|
|
102
|
+
toBuffer: async () => {
|
|
103
|
+
const arrayBuffer = await fetch(event.author.avatar).then(res => res.arrayBuffer());
|
|
104
|
+
return Buffer.from(arrayBuffer);
|
|
105
|
+
},
|
|
106
|
+
toBase64: async () => {
|
|
107
|
+
const arrayBuffer = await fetch(event?.author?.avatar).then(res => res.arrayBuffer());
|
|
108
|
+
return Buffer.from(arrayBuffer).toString('base64');
|
|
109
|
+
},
|
|
110
|
+
toURL: async () => {
|
|
111
|
+
return event?.author?.avatar;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
const UserId = event.author.id;
|
|
115
|
+
const UserKey = useUserHashKey({
|
|
116
|
+
Platform: platform,
|
|
117
|
+
UserId: UserId
|
|
118
|
+
});
|
|
119
|
+
// 定义消
|
|
120
|
+
const e = {
|
|
121
|
+
name: 'message.create',
|
|
122
|
+
// 事件类型
|
|
123
|
+
Platform: platform,
|
|
124
|
+
GuildId: event.guild_id,
|
|
125
|
+
ChannelId: event.channel_id,
|
|
126
|
+
IsMaster: isMaster,
|
|
127
|
+
// 用户Id
|
|
128
|
+
UserId: event?.author?.id ?? '',
|
|
129
|
+
UserKey,
|
|
130
|
+
UserName: event?.author?.username ?? '',
|
|
131
|
+
UserAvatar: UserAvatar,
|
|
132
|
+
IsBot: event.author?.bot,
|
|
133
|
+
// message
|
|
134
|
+
MessageId: event.id,
|
|
135
|
+
MessageText: msg?.trim(),
|
|
136
|
+
OpenId: event.guild_id,
|
|
137
|
+
CreateAt: Date.now(),
|
|
138
|
+
//
|
|
139
|
+
tag: 'AT_MESSAGE_CREATE',
|
|
140
|
+
//
|
|
141
|
+
value: null
|
|
142
|
+
};
|
|
143
|
+
// 当访问的时候获取
|
|
144
|
+
Object.defineProperty(e, 'value', {
|
|
145
|
+
get() {
|
|
146
|
+
return event;
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
// 处理消息
|
|
150
|
+
OnProcessor(e, 'message.create');
|
|
151
|
+
});
|
|
152
|
+
/**
|
|
153
|
+
*
|
|
154
|
+
* @param event
|
|
155
|
+
* @returns
|
|
156
|
+
*/
|
|
157
|
+
const getMessageContent = event => {
|
|
158
|
+
let msg = event?.content ?? '';
|
|
159
|
+
// 艾特消息处理
|
|
160
|
+
const at_users = [];
|
|
161
|
+
if (event.mentions) {
|
|
162
|
+
// 去掉@ 转为纯消息
|
|
163
|
+
for (const item of event.mentions) {
|
|
164
|
+
at_users.push({
|
|
165
|
+
id: item.id
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
// 循环删除文本中的at信息并去除前后空格
|
|
169
|
+
at_users.forEach(item => {
|
|
170
|
+
msg = msg.replace(`<@!${item.id}>`, '').trim();
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
return msg;
|
|
174
|
+
};
|
|
175
|
+
// 私域 -
|
|
176
|
+
client.on('MESSAGE_CREATE', async (event) => {
|
|
177
|
+
// 屏蔽其他机器人的消息
|
|
178
|
+
if (event.author?.bot)
|
|
179
|
+
return;
|
|
180
|
+
// 撤回消息
|
|
181
|
+
if (new RegExp(/DELETE$/).test(event.eventType))
|
|
182
|
+
return;
|
|
183
|
+
const master_key = config?.master_key ?? [];
|
|
184
|
+
const UserId = event.author.id;
|
|
185
|
+
const isMaster = master_key.includes(UserId);
|
|
186
|
+
const msg = getMessageContent(event);
|
|
187
|
+
const UserAvatar = {
|
|
188
|
+
toBuffer: async () => {
|
|
189
|
+
const arrayBuffer = await fetch(event.author.avatar).then(res => res.arrayBuffer());
|
|
190
|
+
return Buffer.from(arrayBuffer);
|
|
191
|
+
},
|
|
192
|
+
toBase64: async () => {
|
|
193
|
+
const arrayBuffer = await fetch(event?.author?.avatar).then(res => res.arrayBuffer());
|
|
194
|
+
return Buffer.from(arrayBuffer).toString('base64');
|
|
195
|
+
},
|
|
196
|
+
toURL: async () => {
|
|
197
|
+
return event?.author?.avatar;
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
const UserKey = useUserHashKey({
|
|
201
|
+
Platform: platform,
|
|
202
|
+
UserId: UserId
|
|
203
|
+
});
|
|
204
|
+
// 定义消
|
|
205
|
+
const e = {
|
|
206
|
+
name: 'message.create',
|
|
207
|
+
// 事件类型
|
|
208
|
+
Platform: platform,
|
|
209
|
+
//
|
|
210
|
+
GuildId: event.guild_id,
|
|
211
|
+
ChannelId: event.channel_id,
|
|
212
|
+
UserId: event?.author?.id ?? '',
|
|
213
|
+
UserKey,
|
|
214
|
+
UserName: event?.author?.username ?? '',
|
|
215
|
+
UserAvatar: UserAvatar,
|
|
216
|
+
IsMaster: isMaster,
|
|
217
|
+
IsBot: false,
|
|
218
|
+
// message
|
|
219
|
+
MessageId: event.id,
|
|
220
|
+
MessageText: msg?.trim(),
|
|
221
|
+
OpenId: event.guild_id,
|
|
222
|
+
CreateAt: Date.now(),
|
|
223
|
+
//
|
|
224
|
+
tag: 'MESSAGE_CREATE',
|
|
225
|
+
value: null
|
|
226
|
+
};
|
|
227
|
+
// 当访问的时候获取
|
|
228
|
+
Object.defineProperty(e, 'value', {
|
|
229
|
+
get() {
|
|
230
|
+
return event;
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
// 处理消息
|
|
234
|
+
OnProcessor(e, 'message.create');
|
|
235
|
+
});
|
|
236
|
+
client.on('ERROR', console.error);
|
|
237
|
+
// FRIEND_ADD
|
|
238
|
+
global.client = client;
|
|
239
|
+
return {
|
|
240
|
+
platform,
|
|
241
|
+
api: {
|
|
242
|
+
// 主动消息
|
|
243
|
+
active: {
|
|
244
|
+
send: {
|
|
245
|
+
channel: async (channel_id, data) => {
|
|
246
|
+
if (isGuild(channel_id)) {
|
|
247
|
+
return await AT_MESSAGE_CREATE(client, {
|
|
248
|
+
ChannelId: channel_id
|
|
249
|
+
}, data);
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
// 需要message_id 。如果没有,则是主动消息,在group中,只能发送4条。
|
|
253
|
+
return await GROUP_AT_MESSAGE_CREATE(client, {
|
|
254
|
+
GuildId: channel_id
|
|
255
|
+
}, data);
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
user: async (user_id, data) => {
|
|
259
|
+
if (isGuild(user_id)) {
|
|
260
|
+
return await DIRECT_MESSAGE_CREATE(client, {
|
|
261
|
+
OpenId: user_id
|
|
262
|
+
}, data);
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
return await C2C_MESSAGE_CREATE(client, {
|
|
266
|
+
OpenId: user_id
|
|
267
|
+
}, data);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
},
|
|
272
|
+
use: {
|
|
273
|
+
send: async (event, val) => {
|
|
274
|
+
if (val.length < 0)
|
|
275
|
+
;
|
|
276
|
+
// 打 tag
|
|
277
|
+
const tag = event.tag;
|
|
278
|
+
try {
|
|
279
|
+
if (tag == 'GROUP_AT_MESSAGE_CREATE') {
|
|
280
|
+
return await GROUP_AT_MESSAGE_CREATE(client, event, val);
|
|
281
|
+
}
|
|
282
|
+
if (tag == 'C2C_MESSAGE_CREATE') {
|
|
283
|
+
return await C2C_MESSAGE_CREATE(client, event, val);
|
|
284
|
+
}
|
|
285
|
+
if (tag == 'DIRECT_MESSAGE_CREATE') {
|
|
286
|
+
return await DIRECT_MESSAGE_CREATE(client, event, val);
|
|
287
|
+
}
|
|
288
|
+
if (tag == 'AT_MESSAGE_CREATE') {
|
|
289
|
+
return await AT_MESSAGE_CREATE(client, event, val);
|
|
290
|
+
}
|
|
291
|
+
if (tag == 'MESSAGE_CREATE') {
|
|
292
|
+
return await AT_MESSAGE_CREATE(client, event, val);
|
|
293
|
+
}
|
|
294
|
+
if (tag == 'MESSAGE_CREATE') {
|
|
295
|
+
return await MESSAGE_CREATE(client, event, val);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
catch (error) {
|
|
299
|
+
return [error];
|
|
300
|
+
}
|
|
301
|
+
return [];
|
|
302
|
+
},
|
|
303
|
+
mention: async (e) => {
|
|
304
|
+
const event = e.value;
|
|
305
|
+
const tag = e.tag;
|
|
306
|
+
// const event = e.value
|
|
307
|
+
const Metions = [];
|
|
308
|
+
// group
|
|
309
|
+
if (tag == 'GROUP_AT_MESSAGE_CREATE' || 'C2C_MESSAGE_CREATE')
|
|
310
|
+
return Metions;
|
|
311
|
+
// guild
|
|
312
|
+
if (event.mentions) {
|
|
313
|
+
const mentions = e.value['mentions'];
|
|
314
|
+
// 艾特消息处理
|
|
315
|
+
const MessageMention = mentions.map(item => {
|
|
316
|
+
return {
|
|
317
|
+
UserId: item.id,
|
|
318
|
+
IsMaster: false,
|
|
319
|
+
UserName: item.username,
|
|
320
|
+
IsBot: item.bot,
|
|
321
|
+
UserKey: useUserHashKey({
|
|
322
|
+
Platform: 'qq-guild-bot',
|
|
323
|
+
UserId: item.id
|
|
324
|
+
})
|
|
325
|
+
};
|
|
326
|
+
}) ?? [];
|
|
327
|
+
return MessageMention;
|
|
328
|
+
}
|
|
329
|
+
else {
|
|
330
|
+
return Metions;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
export { QQBotGuild as default, platform };
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { defineBot, getConfigValue, useUserHashKey, OnProcessor } from 'alemonjs';
|
|
2
|
-
import { QQBotClient } from './client.js';
|
|
3
|
-
import {
|
|
2
|
+
import { QQBotClient } from './sdk/client.js';
|
|
3
|
+
import { AT_MESSAGE_CREATE, GROUP_AT_MESSAGE_CREATE, DIRECT_MESSAGE_CREATE, C2C_MESSAGE_CREATE, MESSAGE_CREATE } from './send/index.js';
|
|
4
|
+
import QQBotGroup from './index.group.js';
|
|
5
|
+
import QQBotGuild from './index.guild.js';
|
|
6
|
+
import { isGuild } from './utils.js';
|
|
4
7
|
|
|
5
8
|
const client = new Proxy({}, {
|
|
6
9
|
get: (_, prop) => {
|
|
@@ -18,6 +21,12 @@ var index = defineBot(() => {
|
|
|
18
21
|
if (!value)
|
|
19
22
|
value = {};
|
|
20
23
|
const config = value[platform];
|
|
24
|
+
if (config.mode == 'guild') {
|
|
25
|
+
return QQBotGuild.callback();
|
|
26
|
+
}
|
|
27
|
+
else if (config.mode == 'group') {
|
|
28
|
+
return QQBotGroup.callback();
|
|
29
|
+
}
|
|
21
30
|
const client = new QQBotClient({
|
|
22
31
|
secret: config?.secret,
|
|
23
32
|
app_id: config?.app_id,
|
|
@@ -348,7 +357,39 @@ var index = defineBot(() => {
|
|
|
348
357
|
// FRIEND_ADD
|
|
349
358
|
global.client = client;
|
|
350
359
|
return {
|
|
360
|
+
platform,
|
|
351
361
|
api: {
|
|
362
|
+
// 主动消息
|
|
363
|
+
active: {
|
|
364
|
+
send: {
|
|
365
|
+
channel: async (channel_id, data) => {
|
|
366
|
+
if (isGuild(channel_id)) {
|
|
367
|
+
return await AT_MESSAGE_CREATE(client, {
|
|
368
|
+
ChannelId: channel_id
|
|
369
|
+
}, data);
|
|
370
|
+
}
|
|
371
|
+
else {
|
|
372
|
+
// 需要message_id 。如果没有,则是主动消息,在group中,只能发送4条。
|
|
373
|
+
return await GROUP_AT_MESSAGE_CREATE(client, {
|
|
374
|
+
GuildId: channel_id
|
|
375
|
+
}, data);
|
|
376
|
+
}
|
|
377
|
+
},
|
|
378
|
+
user: async (user_id, data) => {
|
|
379
|
+
if (isGuild(user_id)) {
|
|
380
|
+
return await DIRECT_MESSAGE_CREATE(client, {
|
|
381
|
+
OpenId: user_id
|
|
382
|
+
}, data);
|
|
383
|
+
}
|
|
384
|
+
else {
|
|
385
|
+
return await C2C_MESSAGE_CREATE(client, {
|
|
386
|
+
OpenId: user_id
|
|
387
|
+
}, data);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
},
|
|
392
|
+
// 被动消息
|
|
352
393
|
use: {
|
|
353
394
|
send: async (event, val) => {
|
|
354
395
|
if (val.length < 0)
|