napconnect 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/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # NapConnect
2
+
3
+ A TypeScript library for NapCat.
4
+
5
+ ## Example
6
+ ```typescript
7
+ import { defineHandler, findMessageSegment, isSameNumericId, NumericSet, open, sendRequest, useGuardAsync } from 'napconnect'
8
+
9
+ const groupAllowlist = NumericSet.fromSplit(import.meta.env.GROUP_ALLOWLIST || '')
10
+
11
+ const connection = open({
12
+ transport: token => new WebSocket(`${import.meta.env.NAPCAT_ENDPOINT}/?access_token=${token}`),
13
+ token: import.meta.env.NAPCAT_TOKEN,
14
+ reconnect: {
15
+ interval: 3000,
16
+ attempts: 15,
17
+ },
18
+ })
19
+
20
+ connection.on('connection.connected', () => {
21
+ console.log('Connected')
22
+ })
23
+
24
+ connection.on('connection.disconnected', () => {
25
+ console.log('Disconnected')
26
+ })
27
+
28
+ connection.on('connection.event', (event) => {
29
+ console.log('Event', event)
30
+ })
31
+
32
+ const handleGroupMessage = defineHandler(
33
+ 'message.group',
34
+ useGuardAsync(
35
+ [
36
+ message => groupAllowlist.has(message.group_id),
37
+ (message) => {
38
+ const segment = findMessageSegment('at', message.message)
39
+ return segment && isSameNumericId(segment.data.qq, message.self_id)
40
+ },
41
+ ],
42
+ async (message) => {
43
+ await sendRequest(connection, '_mark_all_as_read')
44
+
45
+ await sendRequest(connection, 'send_group_msg', {
46
+ auto_escape: true,
47
+ group_id: message.group_id,
48
+ message: 'Hello from napconnect',
49
+ })
50
+
51
+ const [stream, res] = await sendRequest.stream(connection, 'test_download_stream', {})
52
+ console.log('Stream start', stream, res)
53
+
54
+ for await (const reply of stream) {
55
+ console.log('Stream reply', reply)
56
+ await sendRequest(connection, 'send_group_msg', {
57
+ auto_escape: true,
58
+ group_id: message.group_id,
59
+ message: `Stream data: ${reply.data.data}`,
60
+ })
61
+ }
62
+
63
+ console.log('Stream response', await res)
64
+ },
65
+ ),
66
+ )
67
+
68
+ connection.on('message.group', handleGroupMessage)
69
+
70
+ await connection.connect()
71
+ ```