@alemonjs/kook 0.0.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.
- package/lib/index.d.ts +9 -0
- package/lib/index.js +142 -0
- package/package.json +23 -0
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { Text, At, OnProcessor, useParse } from 'alemonjs';
|
|
2
|
+
import { KOOKClient } from 'chat-space';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* @param val
|
|
7
|
+
*/
|
|
8
|
+
const login = (config) => {
|
|
9
|
+
// 创建客户端
|
|
10
|
+
const client = new KOOKClient({
|
|
11
|
+
token: config.token
|
|
12
|
+
});
|
|
13
|
+
// 连接
|
|
14
|
+
client.connect();
|
|
15
|
+
// 监听消息
|
|
16
|
+
client.on('MESSAGES_PUBLIC', async (event) => {
|
|
17
|
+
// 过滤机器人
|
|
18
|
+
if (event.extra?.author?.bot)
|
|
19
|
+
return false;
|
|
20
|
+
// 创建私聊标记
|
|
21
|
+
const data = await client.userChatCreate(event.extra.author.id).then(res => res?.data);
|
|
22
|
+
// 主人
|
|
23
|
+
const master_id = config?.master_id ?? [];
|
|
24
|
+
const isMaster = master_id.includes(event.author_id);
|
|
25
|
+
// 头像
|
|
26
|
+
const avatar = event.extra.author.avatar;
|
|
27
|
+
// 获取消息
|
|
28
|
+
let msg = event.content;
|
|
29
|
+
// 艾特消息处理
|
|
30
|
+
const at_users = [];
|
|
31
|
+
/**
|
|
32
|
+
* 艾特类型所得到的
|
|
33
|
+
* 包括机器人在内
|
|
34
|
+
*/
|
|
35
|
+
const mention_role_part = event.extra.kmarkdown?.mention_role_part ?? [];
|
|
36
|
+
for (const item of mention_role_part) {
|
|
37
|
+
at_users.push({
|
|
38
|
+
id: item.role_id,
|
|
39
|
+
name: item.name,
|
|
40
|
+
avatar: '',
|
|
41
|
+
bot: true
|
|
42
|
+
});
|
|
43
|
+
msg = msg.replace(`(rol)${item.role_id}(rol)`, '').trim();
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* 艾特用户所得到的
|
|
47
|
+
*/
|
|
48
|
+
const mention_part = event.extra.kmarkdown?.mention_part ?? [];
|
|
49
|
+
for (const item of mention_part) {
|
|
50
|
+
at_users.push({
|
|
51
|
+
id: item.id,
|
|
52
|
+
name: item.username,
|
|
53
|
+
avatar: item.avatar,
|
|
54
|
+
bot: false
|
|
55
|
+
});
|
|
56
|
+
msg = msg.replace(`(met)${item.id}(met)`, '').trim();
|
|
57
|
+
}
|
|
58
|
+
// 定义消
|
|
59
|
+
const e = {
|
|
60
|
+
// 事件类型
|
|
61
|
+
Platform: 'kook',
|
|
62
|
+
// 频道
|
|
63
|
+
GuildId: event.extra.guild_id,
|
|
64
|
+
// 子频道
|
|
65
|
+
ChannelId: event.target_id,
|
|
66
|
+
// 是否是主人
|
|
67
|
+
IsMaster: isMaster,
|
|
68
|
+
// 用户ID
|
|
69
|
+
UserId: event.author_id,
|
|
70
|
+
// 用户名
|
|
71
|
+
UserName: event.extra.author.username,
|
|
72
|
+
// 用户头像
|
|
73
|
+
UserAvatar: avatar.substring(0, avatar.indexOf('?')),
|
|
74
|
+
// 格式化数据
|
|
75
|
+
MsgId: event.msg_id,
|
|
76
|
+
// 用户消息
|
|
77
|
+
Megs: [
|
|
78
|
+
Text(msg),
|
|
79
|
+
...at_users.map(item => At(item.id, 'user', { name: item.name, avatar: item.avatar, bot: item.bot }))
|
|
80
|
+
],
|
|
81
|
+
// 用户openId
|
|
82
|
+
OpenID: data?.code,
|
|
83
|
+
// 创建时间
|
|
84
|
+
CreateAt: Date.now(),
|
|
85
|
+
//
|
|
86
|
+
value: null
|
|
87
|
+
};
|
|
88
|
+
// 当访问的时候获取
|
|
89
|
+
Object.defineProperty(e, 'value', {
|
|
90
|
+
get() {
|
|
91
|
+
return event;
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
// 处理消息
|
|
95
|
+
OnProcessor(e, 'message.create');
|
|
96
|
+
});
|
|
97
|
+
// 发送错误时
|
|
98
|
+
client.on('ERROR', msg => {
|
|
99
|
+
console.error(msg);
|
|
100
|
+
});
|
|
101
|
+
/**
|
|
102
|
+
* 开始实现全局接口
|
|
103
|
+
*/
|
|
104
|
+
if (!global?.alemonjs) {
|
|
105
|
+
global.alemonjs = {
|
|
106
|
+
api: {
|
|
107
|
+
use: {
|
|
108
|
+
send: (event, val) => {
|
|
109
|
+
if (val.length < 0)
|
|
110
|
+
return;
|
|
111
|
+
const content = useParse(val, 'Text');
|
|
112
|
+
if (content) {
|
|
113
|
+
return client.createMessage({
|
|
114
|
+
type: 9,
|
|
115
|
+
target_id: event.ChannelId,
|
|
116
|
+
content: content
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
const images = useParse(val, 'Image');
|
|
120
|
+
if (images) {
|
|
121
|
+
return Promise.all(images.map(async (item) => {
|
|
122
|
+
// 上传图片
|
|
123
|
+
const res = await client.postImage(item);
|
|
124
|
+
if (!res)
|
|
125
|
+
return Promise.resolve();
|
|
126
|
+
// 发送消息
|
|
127
|
+
return await client.createMessage({
|
|
128
|
+
type: 2,
|
|
129
|
+
target_id: event.ChannelId,
|
|
130
|
+
content: res.data.url
|
|
131
|
+
});
|
|
132
|
+
}));
|
|
133
|
+
}
|
|
134
|
+
return Promise.resolve();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
export { login };
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@alemonjs/kook",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "啊柠檬脚本",
|
|
5
|
+
"author": "ningmengchongshui",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "lib/index.js",
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"chat-space": "^0.0.3"
|
|
11
|
+
},
|
|
12
|
+
"types": "lib",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./lib/index.js",
|
|
16
|
+
"types": "./lib/index.d.ts"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public",
|
|
21
|
+
"registry": "https://registry.npmjs.org"
|
|
22
|
+
}
|
|
23
|
+
}
|