@alemonjs/qq-bot 0.0.19 → 0.0.20

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.
Files changed (46) hide show
  1. package/README.md +6 -10
  2. package/lib/index.d.ts +2 -2
  3. package/lib/index.group.js +6 -203
  4. package/lib/index.guild.js +22 -297
  5. package/lib/index.js +40 -403
  6. package/lib/index.webhook.js +26 -0
  7. package/lib/register.d.ts +3 -0
  8. package/lib/register.js +405 -0
  9. package/lib/sdk/api.d.ts +5 -10
  10. package/lib/sdk/api.js +5 -10
  11. package/lib/sdk/client.websoket.js +221 -0
  12. package/lib/sdk/intents.js +12 -23
  13. package/package.json +1 -1
  14. package/lib/api.d.ts +0 -975
  15. package/lib/api.js +0 -1188
  16. package/lib/client.d.ts +0 -26
  17. package/lib/client.js +0 -212
  18. package/lib/config.js +0 -3
  19. package/lib/from.js +0 -37
  20. package/lib/message/AT_MESSAGE_CREATE.d.ts +0 -37
  21. package/lib/message/C2C_MESSAGE_CREATE.d.ts +0 -11
  22. package/lib/message/CHANNEL_CREATE.d.ts +0 -17
  23. package/lib/message/CHANNEL_DELETE.d.ts +0 -22
  24. package/lib/message/CHANNEL_UPDATE.d.ts +0 -22
  25. package/lib/message/DIRECT_MESSAGE_CREATE.d.ts +0 -36
  26. package/lib/message/DIRECT_MESSAGE_DELETE.d.ts +0 -24
  27. package/lib/message/ERROR.d.ts +0 -3
  28. package/lib/message/GROUP_AT_MESSAGE_CREATE.d.ts +0 -16
  29. package/lib/message/GUILD_CREATE.d.ts +0 -22
  30. package/lib/message/GUILD_DELETE.d.ts +0 -22
  31. package/lib/message/GUILD_MEMBER_ADD.d.ts +0 -21
  32. package/lib/message/GUILD_MEMBER_REMOVE.d.ts +0 -21
  33. package/lib/message/GUILD_MEMBER_UPDATE.d.ts +0 -21
  34. package/lib/message/GUILD_UPDATE.d.ts +0 -22
  35. package/lib/message/INTERACTION_CREATE.d.ts +0 -8
  36. package/lib/message/MESSAGE_CREATE.d.ts +0 -11
  37. package/lib/message/MESSAGE_DELETE.d.ts +0 -11
  38. package/lib/message/MESSAGE_REACTION_ADD.d.ts +0 -15
  39. package/lib/message/MESSAGE_REACTION_REMOVE.d.ts +0 -15
  40. package/lib/message/PUBLIC_MESSAGE_DELETE.d.ts +0 -21
  41. package/lib/message/READY.d.ts +0 -11
  42. package/lib/message.d.ts +0 -49
  43. package/lib/typing.d.ts +0 -73
  44. package/lib/webhook.js +0 -51
  45. /package/lib/sdk/{websoket.group.js → client.websoket.group.js} +0 -0
  46. /package/lib/sdk/{websoket.guild.js → client.websoket.guild.js} +0 -0
package/lib/client.d.ts DELETED
@@ -1,26 +0,0 @@
1
- import { QQBotAPI } from './api.js'
2
- import { QQBotEventMap } from './message.js'
3
- import { Options } from './typing.js'
4
-
5
- declare class QQBotClient extends QQBotAPI {
6
- #private
7
- /**
8
- * 设置配置
9
- * @param opstion
10
- */
11
- constructor(opstion: Options)
12
- /**
13
- * 注册事件处理程序
14
- * @param key 事件名称
15
- * @param val 事件处理函数
16
- */
17
- on<T extends keyof QQBotEventMap>(key: T, val: (event: QQBotEventMap[T]) => any): this
18
- /**
19
- *
20
- * @param cfg
21
- * @param conversation
22
- */
23
- connect(): void
24
- }
25
-
26
- export { QQBotClient }
package/lib/client.js DELETED
@@ -1,212 +0,0 @@
1
- import { QQBotAPI } from './api.js'
2
- import bodyParser from 'koa-bodyparser'
3
- import Router from 'koa-router'
4
- import { WebhookAPI } from './webhook.js'
5
- import Koa from 'koa'
6
- import { config } from './config.js'
7
- import { v4 } from 'uuid'
8
- import { WebSocketServer, WebSocket } from 'ws'
9
-
10
- class QQBotClient extends QQBotAPI {
11
- #events = {}
12
- #app = null
13
- #count = 0
14
- #client = []
15
- #ws = null
16
- /**
17
- * 设置配置
18
- * @param opstion
19
- */
20
- constructor(opstion) {
21
- super()
22
- if (opstion.secret) config.set('secret', opstion.secret)
23
- if (opstion.app_id) config.set('app_id', opstion.app_id)
24
- if (opstion.token) config.set('token', opstion.token)
25
- if (opstion.port) config.set('port', opstion.port)
26
- if (opstion.ws) config.set('ws', opstion.ws)
27
- }
28
- /**
29
- * 注册事件处理程序
30
- * @param key 事件名称
31
- * @param val 事件处理函数
32
- */
33
- on(key, val) {
34
- if (!this.#events[key]) {
35
- this.#events[key] = []
36
- }
37
- this.#events[key].push(val)
38
- return this
39
- }
40
- /**
41
- * 定时鉴权
42
- * @param cfg
43
- * @returns
44
- */
45
- async #setTimeoutBotConfig() {
46
- const callBack = async () => {
47
- const app_id = config.get('app_id')
48
- if (!app_id) return
49
- const secret = config.get('secret')
50
- if (!secret) return
51
- // 发送请求
52
- const data = await this.getAuthentication(app_id, secret).then(res => res.data)
53
- config.set('access_token', data.access_token)
54
- console.info('refresh', data.expires_in, 's')
55
- setTimeout(callBack, data.expires_in * 1000)
56
- }
57
- await callBack()
58
- }
59
- /**
60
- *
61
- * @param cfg
62
- * @param conversation
63
- */
64
- connect() {
65
- try {
66
- const ws = config.get('ws')
67
- if (!ws) {
68
- this.#setTimeoutBotConfig()
69
- this.#app = new Koa()
70
- this.#app.use(bodyParser())
71
- const router = new Router()
72
- const port = config.get('port')
73
- const secret = config.get('secret')
74
- const route = config.get('route') ?? '/webhook'
75
- const cfg = {
76
- secret: secret ?? '',
77
- port: port ? Number(port) : 17157
78
- }
79
- const ntqqWebhook = new WebhookAPI({
80
- secret: cfg.secret
81
- })
82
- this.#app.use(async (ctx, next) => {
83
- let rawData = ''
84
- ctx.req.on('data', chunk => (rawData += chunk))
85
- ctx.req.on('end', () => (ctx.request.rawBody = rawData))
86
- await next()
87
- })
88
- // 启动服务
89
- router.post(route, async ctx => {
90
- const sign = ctx.req.headers['x-signature-ed25519']
91
- const timestamp = ctx.req.headers['x-signature-timestamp']
92
- const rawBody = ctx.request.rawBody
93
- const isValid = ntqqWebhook.validSign(timestamp, rawBody, String(sign))
94
- if (!isValid) {
95
- ctx.status = 400
96
- ctx.body = { msg: 'invalid signature' }
97
- return
98
- }
99
- const body = ctx.request.body
100
- if (body.op == 13) {
101
- ctx.status = 200
102
- ctx.body = {
103
- // 返回明文 token
104
- plain_token: body.d.plain_token,
105
- // 生成签名
106
- signature: ntqqWebhook.getSign(body.d.event_ts, body.d.plain_token)
107
- }
108
- } else if (body.op == 0) {
109
- ctx.status = 204
110
- // 根据事件类型,处理事件
111
- for (const event of this.#events[body.t] || []) {
112
- event(body.d)
113
- }
114
- const access_token = config.get('access_token')
115
- // 也可以分法到客户端。 发送失败需要处理 或清理调
116
- for (const client of this.#client) {
117
- try {
118
- if (access_token) body['access_token'] = access_token
119
- client.ws.send(JSON.stringify(body))
120
- } catch (e) {
121
- this.#error(e)
122
- }
123
- }
124
- }
125
- })
126
- this.#app.use(router.routes())
127
- this.#app.use(router.allowedMethods())
128
- // 启动服务
129
- const server = this.#app.listen(cfg.port, () => {
130
- console.log('Server running at http://localhost:' + cfg.port + route)
131
- })
132
- // 创建 WebSocketServer 并监听同一个端口
133
- const wss = new WebSocketServer({ server: server })
134
- console.log('Server running at wss://localhost:' + cfg.port + '/')
135
- // 处理客户端连接
136
- wss.on('connection', ws => {
137
- const clientId = v4()
138
- ws['clientId'] = clientId
139
- console.log(clientId, 'connection')
140
- this.#client.push({ id: clientId, ws })
141
- // 处理消息事件
142
- ws.on('message', message => {
143
- // 拿到消息
144
- try {
145
- const body = JSON.parse(message.toString())
146
- for (const event of this.#events[body.t] || []) {
147
- event(body.d)
148
- }
149
- } catch (e) {
150
- this.#error(e)
151
- }
152
- })
153
- // 处理关闭事件
154
- ws.on('close', () => {
155
- console.log(`${clientId} disconnected`)
156
- this.#client = this.#client.filter(client => client.id !== clientId)
157
- })
158
- })
159
- } else {
160
- const reconnect = () => {
161
- // 使用了ws服务器
162
- this.#ws = new WebSocket(ws)
163
- this.#ws.on('open', () => {
164
- this.#count = 0
165
- console.log('ws connected')
166
- })
167
- this.#ws.on('message', data => {
168
- try {
169
- // 拿到消息
170
- const body = JSON.parse(data.toString())
171
- const access_token = body['access_token']
172
- if (access_token) config.set('access_token', access_token)
173
- for (const event of this.#events[body.t] || []) {
174
- event(body.d)
175
- }
176
- } catch (e) {
177
- this.#error(e)
178
- }
179
- })
180
- this.#ws.on('close', () => {
181
- console.log('ws closed')
182
- // 重连5次,超过5次不再重连
183
- if (this.#count > 5) return
184
- // 1.3s 后重连
185
- setTimeout(() => {
186
- reconnect()
187
- }, 1300)
188
- })
189
- this.#ws.on('error', e => {
190
- this.#error(e)
191
- })
192
- }
193
- reconnect()
194
- }
195
- } catch (e) {
196
- this.#error(e)
197
- }
198
- }
199
- /**
200
- *
201
- * @param error
202
- */
203
- #error(error) {
204
- if (this.#events['ERROR']) {
205
- for (const event of this.#events['ERROR'] || []) {
206
- event(error)
207
- }
208
- }
209
- }
210
- }
211
-
212
- export { QQBotClient }
package/lib/config.js DELETED
@@ -1,3 +0,0 @@
1
- const config = new Map()
2
-
3
- export { config }
package/lib/from.js DELETED
@@ -1,37 +0,0 @@
1
- import { existsSync, createReadStream } from 'fs'
2
- import { Readable, isReadable } from 'stream'
3
- import { basename } from 'path'
4
- import { fileTypeFromBuffer, fileTypeFromStream } from 'file-type'
5
-
6
- /**
7
- * 创建form
8
- * @param image
9
- * @param name
10
- * @returns
11
- */
12
- async function createPicFrom(image, name = 'image.jpg') {
13
- let picData
14
- // 是 string
15
- if (typeof image === 'string') {
16
- if (!existsSync(image)) return false
17
- if (!name) name = basename(image)
18
- picData = createReadStream(image)
19
- // 是 buffer
20
- } else if (Buffer.isBuffer(image)) {
21
- const file = await fileTypeFromBuffer(image)
22
- if (!name) name = 'file.' + file?.ext
23
- picData = new Readable()
24
- picData.push(image)
25
- picData.push(null)
26
- // 是 文件流
27
- } else if (isReadable(image)) {
28
- const file = await fileTypeFromStream(image)
29
- if (!name) name = 'file.' + file?.ext
30
- picData = image
31
- } else {
32
- return false
33
- }
34
- return { picData, image, name }
35
- }
36
-
37
- export { createPicFrom }
@@ -1,37 +0,0 @@
1
- type AT_MESSAGE_CREATE_TYPE = {
2
- attachments?: {
3
- id: string
4
- url: string
5
- content_type: string
6
- filename: string
7
- size: number
8
- height: number
9
- width: number
10
- }[]
11
- author: {
12
- avatar: string
13
- bot: boolean
14
- id: string
15
- username: string
16
- }
17
- channel_id: string
18
- content: string
19
- guild_id: string
20
- id: string
21
- member: {
22
- joined_at: string
23
- nick: string
24
- roles: string[]
25
- }
26
- mentions: {
27
- avatar: string
28
- bot: boolean
29
- id: string
30
- username: string
31
- }[]
32
- seq: number
33
- seq_in_channel: string
34
- timestamp: string
35
- }
36
-
37
- export type { AT_MESSAGE_CREATE_TYPE }
@@ -1,11 +0,0 @@
1
- type C2C_MESSAGE_CREATE_TYPE = {
2
- author: {
3
- id: string
4
- user_openid: string
5
- }
6
- content: string
7
- id: string
8
- timestamp: string
9
- }
10
-
11
- export type { C2C_MESSAGE_CREATE_TYPE }
@@ -1,17 +0,0 @@
1
- type CHANNEL_CREATE_TYPE = {
2
- application_id?: string
3
- guild_id: string
4
- id: string
5
- name: string
6
- op_user_id: string
7
- owner_id: string
8
- parent_id?: string
9
- permissions?: string
10
- position?: number
11
- private_type: number
12
- speak_permission: number
13
- sub_type: number
14
- type: number
15
- }
16
-
17
- export type { CHANNEL_CREATE_TYPE }
@@ -1,22 +0,0 @@
1
- /**
2
- * 子频道删除
3
- * @param event
4
- * @returns
5
- */
6
- type CHANNEL_DELETE_TYPE = {
7
- application_id?: string
8
- guild_id: string
9
- id: string
10
- name: string
11
- op_user_id: string
12
- owner_id: string
13
- parent_id?: string
14
- permissions?: string
15
- position?: number
16
- private_type: number
17
- speak_permission: number
18
- sub_type: number
19
- type: number
20
- }
21
-
22
- export type { CHANNEL_DELETE_TYPE }
@@ -1,22 +0,0 @@
1
- /**
2
- * 子频道更新
3
- * @param event
4
- * @returns
5
- */
6
- type CHANNEL_UPDATE_TYPE = {
7
- application_id?: string
8
- guild_id: string
9
- id: string
10
- name: string
11
- op_user_id: string
12
- owner_id: string
13
- parent_id?: string
14
- permissions?: string
15
- position?: number
16
- private_type: number
17
- speak_permission: number
18
- sub_type: number
19
- type: number
20
- }
21
-
22
- export type { CHANNEL_UPDATE_TYPE }
@@ -1,36 +0,0 @@
1
- /**
2
- * 私信
3
- * @param event
4
- * @returns
5
- */
6
- type DIRECT_MESSAGE_CREATE_TYPE = {
7
- attachments?: {
8
- content_type: string
9
- filename: string
10
- height: number
11
- id: string
12
- size: number
13
- url: string
14
- width: number
15
- }[]
16
- author: {
17
- avatar: string
18
- bot: boolean
19
- id: string
20
- username: string
21
- }
22
- channel_id: string
23
- content: string
24
- direct_message: boolean
25
- guild_id: string
26
- id: string
27
- member: {
28
- joined_at: string
29
- }
30
- seq: number
31
- seq_in_channel: string
32
- src_guild_id: string
33
- timestamp: string
34
- }
35
-
36
- export type { DIRECT_MESSAGE_CREATE_TYPE }
@@ -1,24 +0,0 @@
1
- /**
2
- * *
3
- * 私信
4
- * *
5
- */
6
- type DIRECT_MESSAGE_DELETE_TYPE = {
7
- message: {
8
- author: {
9
- bot: boolean
10
- id: string
11
- username: string
12
- }
13
- channel_id: string
14
- direct_message: boolean
15
- guild_id: string
16
- id: string
17
- src_guild_id: string
18
- }
19
- op_user: {
20
- id: string
21
- }
22
- }
23
-
24
- export type { DIRECT_MESSAGE_DELETE_TYPE }
@@ -1,3 +0,0 @@
1
- type ERROR_TYPE = any
2
-
3
- export type { ERROR_TYPE }
@@ -1,16 +0,0 @@
1
- /**
2
- * 群消息事件 AT 事件
3
- */
4
- interface GROUP_AT_MESSAGE_CREATE_TYPE {
5
- author: {
6
- id: string
7
- member_openid: string
8
- }
9
- content: string
10
- group_openid: string
11
- group_id: string
12
- id: string
13
- timestamp: string
14
- }
15
-
16
- export type { GROUP_AT_MESSAGE_CREATE_TYPE }
@@ -1,22 +0,0 @@
1
- /**
2
- * 机器人加入
3
- * @param event
4
- * @returns
5
- */
6
- type GUILD_CREATE_TYPE = {
7
- description: string
8
- icon: string
9
- id: string
10
- joined_at: string
11
- max_members: number
12
- member_count: number
13
- name: string
14
- op_user_id: string
15
- owner: boolean
16
- owner_id: string
17
- union_appid: string
18
- union_org_id: string
19
- union_world_id: string
20
- }
21
-
22
- export type { GUILD_CREATE_TYPE }
@@ -1,22 +0,0 @@
1
- /**
2
- * 机器人退出频道
3
- * @param event
4
- * @returns
5
- */
6
- type GUILD_DELETE_TYPE = {
7
- description: string
8
- icon: string
9
- id: string
10
- joined_at: string
11
- max_members: number
12
- member_count: number
13
- name: string
14
- op_user_id: string
15
- owner: boolean
16
- owner_id: string
17
- union_appid: string
18
- union_org_id: string
19
- union_world_id: string
20
- }
21
-
22
- export type { GUILD_DELETE_TYPE }
@@ -1,21 +0,0 @@
1
- /**
2
- * 当成员加入时
3
- * @param event
4
- * @returns
5
- */
6
- type GUILD_MEMBER_ADD_TYPE = {
7
- guild_id: string
8
- joined_at: string
9
- nick: string
10
- op_user_id: string
11
- roles: string[]
12
- source_type?: string
13
- user: {
14
- avatar: string
15
- bot: number
16
- id: string
17
- username: string
18
- }
19
- }
20
-
21
- export type { GUILD_MEMBER_ADD_TYPE }
@@ -1,21 +0,0 @@
1
- /**
2
- * 当成员被移除时
3
- * @param event
4
- * @returns
5
- */
6
- type GUILD_MEMBER_REMOVE_TYPE = {
7
- guild_id: string
8
- joined_at: string
9
- nick: string
10
- op_user_id: string
11
- roles: string[]
12
- source_type?: string
13
- user: {
14
- avatar: string
15
- bot: number
16
- id: string
17
- username: string
18
- }
19
- }
20
-
21
- export type { GUILD_MEMBER_REMOVE_TYPE }
@@ -1,21 +0,0 @@
1
- /**
2
- * 当成员资料变更时
3
- * @param event
4
- * @returns
5
- */
6
- type GUILD_MEMBER_UPDATE_TYPE = {
7
- guild_id: string
8
- joined_at: string
9
- nick: string
10
- op_user_id: string
11
- roles: string[]
12
- source_type?: string
13
- user: {
14
- avatar: string
15
- bot: number
16
- id: string
17
- username: string
18
- }
19
- }
20
-
21
- export type { GUILD_MEMBER_UPDATE_TYPE }
@@ -1,22 +0,0 @@
1
- /**
2
- * 信息更新
3
- * @param event
4
- * @returns
5
- */
6
- type GUILD_UPDATE_TYPE = {
7
- description: string
8
- icon: string
9
- id: string
10
- joined_at: string
11
- max_members: number
12
- member_count: number
13
- name: string
14
- op_user_id: string
15
- owner: boolean
16
- owner_id: string
17
- union_appid: string
18
- union_org_id: string
19
- union_world_id: string
20
- }
21
-
22
- export type { GUILD_UPDATE_TYPE }
@@ -1,8 +0,0 @@
1
- /**
2
- * 交互消息事件 | 按钮消息
3
- * @param event
4
- * @returns
5
- */
6
- type INTERACTION_CREATE_TYPE = any
7
-
8
- export type { INTERACTION_CREATE_TYPE }
@@ -1,11 +0,0 @@
1
- /**
2
- * *私域*
3
- */
4
- /**
5
- * 频道内的全部消息
6
- * @param event
7
- * @returns
8
- */
9
- type MESSAGE_CREATE_TYPE = any
10
-
11
- export type { MESSAGE_CREATE_TYPE }
@@ -1,11 +0,0 @@
1
- /**
2
- * *私域*
3
- */
4
- /**
5
- * 删除(撤回)消息事件
6
- * @param event
7
- * @returns
8
- */
9
- type MESSAGE_DELETE_TYPE = any
10
-
11
- export type { MESSAGE_DELETE_TYPE }
@@ -1,15 +0,0 @@
1
- type MESSAGE_REACTION_ADD_TYPE = {
2
- channel_id: string
3
- emoji: {
4
- id: string
5
- type: number
6
- }
7
- guild_id: string
8
- target: {
9
- id: string
10
- type: string
11
- }
12
- user_id: string
13
- }
14
-
15
- export type { MESSAGE_REACTION_ADD_TYPE }
@@ -1,15 +0,0 @@
1
- type MESSAGE_REACTION_REMOVE_TYPE = {
2
- channel_id: string
3
- emoji: {
4
- id: string
5
- type: number
6
- }
7
- guild_id: string
8
- target: {
9
- id: string
10
- type: string
11
- }
12
- user_id: string
13
- }
14
-
15
- export type { MESSAGE_REACTION_REMOVE_TYPE }