@zhin.js/adapter-kook 1.0.13 → 1.0.14
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/CHANGELOG.md +9 -0
- package/package.json +24 -5
- package/src/index.ts +0 -128
- package/tsconfig.json +0 -24
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zhin.js/adapter-kook",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.14",
|
|
4
4
|
"description": "zhin adapter for kook",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -11,6 +11,18 @@
|
|
|
11
11
|
"import": "./lib/index.js"
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"zhin",
|
|
16
|
+
"adapter",
|
|
17
|
+
"kook"
|
|
18
|
+
],
|
|
19
|
+
"author": "lc-cn",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"repository": {
|
|
22
|
+
"url": "git+https://github.com/zhinjs/zhin.git",
|
|
23
|
+
"type": "git",
|
|
24
|
+
"directory": "plugins/adapters/kook"
|
|
25
|
+
},
|
|
14
26
|
"dependencies": {
|
|
15
27
|
"kook-client": "latest"
|
|
16
28
|
},
|
|
@@ -18,16 +30,23 @@
|
|
|
18
30
|
"typescript": "^5.3.0"
|
|
19
31
|
},
|
|
20
32
|
"peerDependencies": {
|
|
21
|
-
"zhin.js": "1.0.
|
|
22
|
-
"@zhin.js/types": "1.0.
|
|
33
|
+
"zhin.js": "1.0.14",
|
|
34
|
+
"@zhin.js/types": "1.0.5"
|
|
23
35
|
},
|
|
24
36
|
"peerDependenciesMeta": {
|
|
25
37
|
"@zhin.js/types": {
|
|
26
38
|
"optional": true
|
|
27
39
|
}
|
|
28
40
|
},
|
|
41
|
+
"files": [
|
|
42
|
+
"lib",
|
|
43
|
+
"node",
|
|
44
|
+
"README.md",
|
|
45
|
+
"CHANGELOG.md"
|
|
46
|
+
],
|
|
29
47
|
"scripts": {
|
|
30
|
-
"build": "
|
|
31
|
-
"clean": "rm -rf lib"
|
|
48
|
+
"build": "pnpm build:node",
|
|
49
|
+
"clean": "rm -rf lib",
|
|
50
|
+
"build:node": "tsc"
|
|
32
51
|
}
|
|
33
52
|
}
|
package/src/index.ts
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import { Client, PrivateMessageEvent,MessageSegment as MessageElem,ChannelMessageEvent, Sendable} from "kook-client";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import {
|
|
4
|
-
Bot,
|
|
5
|
-
Adapter,
|
|
6
|
-
registerAdapter,
|
|
7
|
-
Message,
|
|
8
|
-
SendOptions,
|
|
9
|
-
MessageSegment,
|
|
10
|
-
SendContent,
|
|
11
|
-
segment,
|
|
12
|
-
usePlugin
|
|
13
|
-
} from "zhin.js";
|
|
14
|
-
declare module '@zhin.js/types'{
|
|
15
|
-
interface RegisteredAdapters{
|
|
16
|
-
kook:Adapter<KookBot>
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
const plugin =usePlugin()
|
|
20
|
-
export interface KookBotConfig extends Bot.Config,Client.Config{
|
|
21
|
-
context:'kook'
|
|
22
|
-
name:`${number}`
|
|
23
|
-
}
|
|
24
|
-
export interface KookBot{
|
|
25
|
-
$config:KookBotConfig
|
|
26
|
-
}
|
|
27
|
-
export class KookBot extends Client implements Bot<PrivateMessageEvent|ChannelMessageEvent,KookBotConfig>{
|
|
28
|
-
$connected?:boolean
|
|
29
|
-
constructor(config:KookBotConfig) {
|
|
30
|
-
if(!config.data_dir) config.data_dir=path.join(process.cwd(),'data')
|
|
31
|
-
super(config);
|
|
32
|
-
this.$config=config;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
$formatMessage(msg: PrivateMessageEvent | ChannelMessageEvent){
|
|
36
|
-
const message=Message.from(msg,{
|
|
37
|
-
$id: msg.message_id.toString(),
|
|
38
|
-
$adapter:'kook',
|
|
39
|
-
$bot:`${this.$config.name}`,
|
|
40
|
-
$sender:{
|
|
41
|
-
id:msg.author_id.toString(),
|
|
42
|
-
name:msg.author.info.nickname.toString(),
|
|
43
|
-
},
|
|
44
|
-
$channel:{
|
|
45
|
-
id:msg.message_type==='channel'?msg.channel_id.toString():msg.author_id.toString(),
|
|
46
|
-
type:msg.message_type
|
|
47
|
-
},
|
|
48
|
-
$content: KookBot.toSegments(msg.message),
|
|
49
|
-
$raw: msg.raw_message,
|
|
50
|
-
$timestamp: msg.timestamp,
|
|
51
|
-
$reply:async (content: SendContent, quote?: boolean|string):Promise<string>=> {
|
|
52
|
-
if(!Array.isArray(content)) content=[content];
|
|
53
|
-
if(quote) content.unshift({type:'reply',data:{id:typeof quote==="boolean"?message.$id:quote}})
|
|
54
|
-
return await this.$sendMessage({
|
|
55
|
-
...message.$channel,
|
|
56
|
-
context:'kook',
|
|
57
|
-
bot:`${this.$config.name}`,
|
|
58
|
-
content
|
|
59
|
-
})
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
return message
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
async $connect(): Promise<void> {
|
|
66
|
-
await super.connect()
|
|
67
|
-
this.on('message',(m)=>this.handleKookMessage(m))
|
|
68
|
-
this.$connected=true
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
async $disconnect(): Promise<void> {
|
|
72
|
-
await super.disconnect()
|
|
73
|
-
this.$connected=false;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
async $sendMessage(options: SendOptions): Promise<string> {
|
|
77
|
-
options=await plugin.app.handleBeforeSend(options)
|
|
78
|
-
switch (options.type){
|
|
79
|
-
case 'private':{
|
|
80
|
-
const result= await this.sendPrivateMsg(options.id,KookBot.toSendable(options.content));
|
|
81
|
-
plugin.logger.info(`${this.$config.name} send ${options.type}(${options.id}):${segment.raw(options.content)}`);
|
|
82
|
-
return `private-${options.id}:${(result as unknown as {msg_id:string}).msg_id.toString()}`
|
|
83
|
-
break;
|
|
84
|
-
}
|
|
85
|
-
case "channel":{
|
|
86
|
-
const result=await this.sendChannelMsg(options.id,KookBot.toSendable(options.content));
|
|
87
|
-
plugin.logger.info(`${this.$config.name} send ${options.type}(${options.id}):${segment.raw(options.content)}`);
|
|
88
|
-
return `channel-${options.id}:${(result as unknown as {msg_id:string}).msg_id.toString()}`
|
|
89
|
-
break;
|
|
90
|
-
}
|
|
91
|
-
default:
|
|
92
|
-
throw new Error(`unsupported channel type ${options.type}`)
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
async $recallMessage(id:string):Promise<void> {
|
|
96
|
-
if(!/^(private|channel)-([^\:]+):(.+)$/.test(id)) throw new Error(`invalid message id ${id}`)
|
|
97
|
-
const [target_type,target_id,message_id]=id.match(/^(private|channel)-([^\:]+):(.+)$/)!
|
|
98
|
-
if(target_type==='private') await this.recallPrivateMsg(target_id,message_id)
|
|
99
|
-
if(target_type==='channel') await this.recallChannelMsg(target_id,message_id)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
private handleKookMessage(msg: PrivateMessageEvent|ChannelMessageEvent): void {
|
|
103
|
-
const message=this.$formatMessage(msg)
|
|
104
|
-
plugin.dispatch('message.receive',message)
|
|
105
|
-
plugin.logger.info(`${this.$config.name} recv ${message.$channel.type}(${message.$channel.id}):${segment.raw(message.$content)}`)
|
|
106
|
-
plugin.dispatch(`message.${message.$channel.type}.receive`,message)
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
export namespace KookBot{
|
|
111
|
-
export function toSegments(message:Sendable):MessageSegment[]{
|
|
112
|
-
if(!Array.isArray(message)) message=[message]
|
|
113
|
-
return message.map((item):MessageSegment=>{
|
|
114
|
-
if(typeof item==="string") return {type:'text',data:{text:item}}
|
|
115
|
-
const {type,...data}=item
|
|
116
|
-
return {type:type==='markdown'?'text':type,data}
|
|
117
|
-
})
|
|
118
|
-
}
|
|
119
|
-
export function toSendable(content:SendContent):Sendable{
|
|
120
|
-
if(!Array.isArray(content)) content=[content]
|
|
121
|
-
return content.map((segment):MessageElem=>{
|
|
122
|
-
if(typeof segment==="string") return {type:'text',text:segment}
|
|
123
|
-
const {type,data}=segment
|
|
124
|
-
return {type,...data} as MessageElem
|
|
125
|
-
})
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
registerAdapter(new Adapter('kook',KookBot))
|
package/tsconfig.json
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2022",
|
|
4
|
-
"module": "ESNext",
|
|
5
|
-
"moduleResolution": "bundler",
|
|
6
|
-
"outDir": "./lib",
|
|
7
|
-
"rootDir": "./src",
|
|
8
|
-
"strict": true,
|
|
9
|
-
"esModuleInterop": true,
|
|
10
|
-
"skipLibCheck": true,
|
|
11
|
-
"forceConsistentCasingInFileNames": true,
|
|
12
|
-
"resolveJsonModule": true,
|
|
13
|
-
"isolatedModules": true,
|
|
14
|
-
"allowSyntheticDefaultImports": true,
|
|
15
|
-
"experimentalDecorators": true,
|
|
16
|
-
"emitDecoratorMetadata": true,
|
|
17
|
-
"declaration": true,
|
|
18
|
-
"declarationMap": true,
|
|
19
|
-
"sourceMap": true,
|
|
20
|
-
"verbatimModuleSyntax": false
|
|
21
|
-
},
|
|
22
|
-
"include": ["src/**/*"],
|
|
23
|
-
"exclude": ["lib", "node_modules"]
|
|
24
|
-
}
|