@alemonjs/discord 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 +119 -0
- package/package.json +23 -0
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { Text, At, OnProcessor, useParse } from 'alemonjs'
|
|
2
|
+
import { DCClient } from 'chat-space'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* @param val
|
|
7
|
+
*/
|
|
8
|
+
const login = config => {
|
|
9
|
+
// 创建客户端
|
|
10
|
+
const client = new DCClient({
|
|
11
|
+
token: config.token
|
|
12
|
+
})
|
|
13
|
+
// 连接
|
|
14
|
+
client.connect()
|
|
15
|
+
// 监听消息
|
|
16
|
+
client.on('MESSAGE_CREATE', async event => {
|
|
17
|
+
// 消除bot消息
|
|
18
|
+
if (event.author?.bot) return
|
|
19
|
+
const master_id = config?.master_id ?? []
|
|
20
|
+
const isMaster = master_id.includes(event.author.id)
|
|
21
|
+
// 艾特消息处理
|
|
22
|
+
const at_users = []
|
|
23
|
+
// 获取艾特用户
|
|
24
|
+
for (const item of event.mentions) {
|
|
25
|
+
at_users.push({
|
|
26
|
+
id: item.id,
|
|
27
|
+
name: item.username,
|
|
28
|
+
avatar: client.userAvatar(item.id, item.avatar),
|
|
29
|
+
bot: item.bot
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
// 清除 @ 相关的消息
|
|
33
|
+
let msg = event.content
|
|
34
|
+
for await (const item of at_users) {
|
|
35
|
+
msg = msg.replace(`<@${item.id}>`, '').trim()
|
|
36
|
+
}
|
|
37
|
+
// 定义消
|
|
38
|
+
const e = {
|
|
39
|
+
// 事件类型
|
|
40
|
+
Platform: 'kook',
|
|
41
|
+
// 频道
|
|
42
|
+
GuildId: event.guild_id,
|
|
43
|
+
// 子频道
|
|
44
|
+
ChannelId: event.channel_id,
|
|
45
|
+
// 是否是主人
|
|
46
|
+
IsMaster: isMaster,
|
|
47
|
+
// 用户ID
|
|
48
|
+
UserId: event.author.id,
|
|
49
|
+
// 用户名
|
|
50
|
+
UserName: event.author.username,
|
|
51
|
+
// 用户头像
|
|
52
|
+
UserAvatar: client.userAvatar(event.author.id, event.author.avatar),
|
|
53
|
+
// 格式化数据
|
|
54
|
+
MsgId: event.id,
|
|
55
|
+
// 用户消息
|
|
56
|
+
Megs: [
|
|
57
|
+
Text(msg),
|
|
58
|
+
...at_users.map(item =>
|
|
59
|
+
At(item.id, 'user', {
|
|
60
|
+
name: item.name,
|
|
61
|
+
avatar: item.avatar,
|
|
62
|
+
bot: item.bot
|
|
63
|
+
})
|
|
64
|
+
)
|
|
65
|
+
],
|
|
66
|
+
// 用户openId
|
|
67
|
+
OpenID: '',
|
|
68
|
+
// 创建时间
|
|
69
|
+
CreateAt: Date.now(),
|
|
70
|
+
//
|
|
71
|
+
value: null
|
|
72
|
+
}
|
|
73
|
+
// 当访问的时候获取
|
|
74
|
+
Object.defineProperty(e, 'value', {
|
|
75
|
+
get() {
|
|
76
|
+
return event
|
|
77
|
+
}
|
|
78
|
+
})
|
|
79
|
+
// 处理消息
|
|
80
|
+
OnProcessor(e, 'message.create')
|
|
81
|
+
})
|
|
82
|
+
// 发送错误时
|
|
83
|
+
client.on('ERROR', msg => {
|
|
84
|
+
console.error(msg)
|
|
85
|
+
})
|
|
86
|
+
/**
|
|
87
|
+
* 开始实现全局接口
|
|
88
|
+
*/
|
|
89
|
+
if (!global?.alemonjs) {
|
|
90
|
+
global.alemonjs = {
|
|
91
|
+
api: {
|
|
92
|
+
use: {
|
|
93
|
+
send: (event, val) => {
|
|
94
|
+
if (val.length < 0) return
|
|
95
|
+
const content = useParse(val, 'Text')
|
|
96
|
+
if (content) {
|
|
97
|
+
return Promise.all(
|
|
98
|
+
[content].map(item =>
|
|
99
|
+
client.channelsMessages(event.ChannelId, {
|
|
100
|
+
content: item
|
|
101
|
+
})
|
|
102
|
+
)
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
const images = useParse(val, 'Image')
|
|
106
|
+
if (images) {
|
|
107
|
+
return Promise.all(
|
|
108
|
+
images.map(item => client.channelsMessagesImage(event.ChannelId, item))
|
|
109
|
+
)
|
|
110
|
+
}
|
|
111
|
+
return Promise.resolve()
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export { login }
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@alemonjs/discord",
|
|
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
|
+
}
|