@zhin.js/adapter-kook 1.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/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # @zhin.js/adapter-kook
2
+
3
+ ## 1.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [efdd58a]
8
+ - @zhin.js/types@1.0.1
9
+ - zhin.js@1.0.1
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 凉菜
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # @zhin.js/adapter-kook
2
+
3
+ 基于 KOOK(开黑啦)的 Zhin 机器人适配器,用于连接 KOOK 机器人。
4
+
5
+ ## 功能特性
6
+
7
+ - 🗣️ 支持KOOK频道和私聊消息处理
8
+ - 📨 消息发送和接收处理
9
+ - 🔄 消息格式转换和适配
10
+ - 📁 自动数据目录管理
11
+ - ⚡ 基于WebSocket的实时通信
12
+
13
+ ## 安装
14
+
15
+ ```bash
16
+ npm install @zhin.js/adapter-kook kook-client
17
+ ```
18
+
19
+ ## 配置
20
+
21
+ ```javascript
22
+ // zhin.config.ts
23
+ export default {
24
+ bots: [
25
+ {
26
+ context: 'kook',
27
+ name: '123456789', // 机器人ID
28
+ token: 'your-bot-token', // KOOK机器人Token
29
+ data_dir: './data', // 数据目录
30
+ // 其他KOOK配置项...
31
+ }
32
+ ]
33
+ }
34
+ ```
35
+
36
+ ## 使用方式
37
+
38
+ 适配器会自动连接KOOK服务器:
39
+ - 基于WebSocket协议通信
40
+ - 自动处理连接和断线重连
41
+ - 支持消息事件监听
42
+
43
+ ## 消息处理
44
+
45
+ - 自动将KOOK消息格式转换为Zhin标准消息格式
46
+ - 支持频道消息和私聊消息
47
+ - 支持Markdown消息格式
48
+ - 提供消息回复功能
49
+
50
+ ## 依赖项
51
+
52
+ - `kook-client` - KOOK客户端库
53
+ - `zhin.js` - Zhin核心框架
54
+
55
+ ## 开发
56
+
57
+ ```bash
58
+ npm run build # 构建
59
+ npm run clean # 清理构建文件
60
+ ```
package/lib/index.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ import { Client, PrivateMessageEvent, ChannelMessageEvent, Sendable } from "kook-client";
2
+ import { Bot, BotConfig, Adapter, Plugin, Message, SendOptions, MessageSegment, SendContent } from "zhin.js";
3
+ declare module 'zhin.js' {
4
+ interface RegisteredAdapters {
5
+ kook: Adapter<KookBot>;
6
+ }
7
+ }
8
+ export interface KookBotConfig extends BotConfig, Client.Config {
9
+ context: 'kook';
10
+ name: `${number}`;
11
+ }
12
+ export interface KookBot {
13
+ $config: KookBotConfig;
14
+ }
15
+ export declare class KookBot extends Client implements Bot<PrivateMessageEvent | ChannelMessageEvent, KookBotConfig> {
16
+ private plugin;
17
+ $connected?: boolean;
18
+ constructor(plugin: Plugin, config: KookBotConfig);
19
+ $formatMessage(msg: PrivateMessageEvent | ChannelMessageEvent): Message<PrivateMessageEvent | ChannelMessageEvent>;
20
+ $connect(): Promise<void>;
21
+ $disconnect(): Promise<void>;
22
+ $sendMessage(options: SendOptions): Promise<void>;
23
+ private handleKookMessage;
24
+ }
25
+ export declare namespace KookBot {
26
+ function toSegments(message: Sendable): MessageSegment[];
27
+ function toSendable(content: SendContent): Sendable;
28
+ }
29
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,mBAAmB,EAA+B,mBAAmB,EAAE,QAAQ,EAAC,MAAM,aAAa,CAAC;AAErH,OAAO,EACH,GAAG,EACH,SAAS,EACT,OAAO,EACP,MAAM,EAEN,OAAO,EACP,WAAW,EACX,cAAc,EACd,WAAW,EAEd,MAAM,SAAS,CAAC;AACjB,OAAO,QAAQ,SAAS,CAAA;IACpB,UAAU,kBAAkB;QACxB,IAAI,EAAC,OAAO,CAAC,OAAO,CAAC,CAAA;KACxB;CACJ;AACD,MAAM,WAAW,aAAc,SAAQ,SAAS,EAAC,MAAM,CAAC,MAAM;IAC1D,OAAO,EAAC,MAAM,CAAA;IACd,IAAI,EAAC,GAAG,MAAM,EAAE,CAAA;CACnB;AACD,MAAM,WAAW,OAAO;IACpB,OAAO,EAAC,aAAa,CAAA;CACxB;AACD,qBAAa,OAAQ,SAAQ,MAAO,YAAW,GAAG,CAAC,mBAAmB,GAAC,mBAAmB,EAAC,aAAa,CAAC;IAEzF,OAAO,CAAC,MAAM;IAD1B,UAAU,CAAC,EAAC,OAAO,CAAA;gBACC,MAAM,EAAC,MAAM,EAAC,MAAM,EAAC,aAAa;IAMtD,cAAc,CAAC,GAAG,EAAE,mBAAmB,GAAG,mBAAmB;IA8BvD,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAMzB,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IAK5B,YAAY,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAkBvD,OAAO,CAAC,iBAAiB;CAO5B;AACD,yBAAiB,OAAO,CAAA;IACpB,SAAgB,UAAU,CAAC,OAAO,EAAC,QAAQ,GAAE,cAAc,EAAE,CAO5D;IACD,SAAgB,UAAU,CAAC,OAAO,EAAC,WAAW,GAAE,QAAQ,CAOvD;CACJ"}
package/lib/index.js ADDED
@@ -0,0 +1,103 @@
1
+ import { Client } from "kook-client";
2
+ import path from "path";
3
+ import { Adapter, registerAdapter, Message, segment } from "zhin.js";
4
+ export class KookBot extends Client {
5
+ plugin;
6
+ $connected;
7
+ constructor(plugin, config) {
8
+ if (!config.data_dir)
9
+ config.data_dir = path.join(process.cwd(), 'data');
10
+ super(config);
11
+ this.plugin = plugin;
12
+ this.$config = config;
13
+ }
14
+ $formatMessage(msg) {
15
+ const message = Message.from(msg, {
16
+ $id: msg.message_id.toString(),
17
+ $adapter: 'kook',
18
+ $bot: `${this.$config.name}`,
19
+ $sender: {
20
+ id: msg.author_id.toString(),
21
+ name: msg.author.info.nickname.toString(),
22
+ },
23
+ $channel: {
24
+ id: msg.message_type === 'channel' ? msg.channel_id.toString() : msg.author_id.toString(),
25
+ type: msg.message_type
26
+ },
27
+ $content: KookBot.toSegments(msg.message),
28
+ $raw: msg.raw_message,
29
+ $timestamp: msg.timestamp,
30
+ $reply: async (content, quote) => {
31
+ if (!Array.isArray(content))
32
+ content = [content];
33
+ if (quote)
34
+ content.unshift({ type: 'reply', data: { id: typeof quote === "boolean" ? message.$id : quote } });
35
+ this.plugin.dispatch('message.send', {
36
+ ...message.$channel,
37
+ context: 'kook',
38
+ bot: `${this.$config.name}`,
39
+ content
40
+ });
41
+ }
42
+ });
43
+ return message;
44
+ }
45
+ async $connect() {
46
+ await super.connect();
47
+ this.on('message', (m) => this.handleKookMessage(m));
48
+ this.$connected = true;
49
+ }
50
+ async $disconnect() {
51
+ await super.disconnect();
52
+ this.$connected = false;
53
+ }
54
+ async $sendMessage(options) {
55
+ options = await this.plugin.app.handleBeforeSend(options);
56
+ switch (options.type) {
57
+ case 'private': {
58
+ const result = await this.sendPrivateMsg(options.id, KookBot.toSendable(options.content));
59
+ this.plugin.logger.info(`send ${options.type}(${options.id}):${segment.raw(options.content)}`);
60
+ break;
61
+ }
62
+ case "channel": {
63
+ const result = await this.sendChannelMsg(options.id, KookBot.toSendable(options.content));
64
+ this.plugin.logger.info(`send ${options.type}(${options.id}):${segment.raw(options.content)}`);
65
+ break;
66
+ }
67
+ default:
68
+ throw new Error(`unsupported channel type ${options.type}`);
69
+ }
70
+ }
71
+ handleKookMessage(msg) {
72
+ const message = this.$formatMessage(msg);
73
+ this.plugin.dispatch('message.receive', message);
74
+ this.plugin.logger.info(`recv ${message.$channel.type}(${message.$channel.id}):${segment.raw(message.$content)}`);
75
+ this.plugin.dispatch(`message.${message.$channel.type}.receive`, message);
76
+ }
77
+ }
78
+ (function (KookBot) {
79
+ function toSegments(message) {
80
+ if (!Array.isArray(message))
81
+ message = [message];
82
+ return message.map((item) => {
83
+ if (typeof item === "string")
84
+ return { type: 'text', data: { text: item } };
85
+ const { type, ...data } = item;
86
+ return { type: type === 'markdown' ? 'text' : type, data };
87
+ });
88
+ }
89
+ KookBot.toSegments = toSegments;
90
+ function toSendable(content) {
91
+ if (!Array.isArray(content))
92
+ content = [content];
93
+ return content.map((segment) => {
94
+ if (typeof segment === "string")
95
+ return { type: 'text', text: segment };
96
+ const { type, data } = segment;
97
+ return { type, ...data };
98
+ });
99
+ }
100
+ KookBot.toSendable = toSendable;
101
+ })(KookBot || (KookBot = {}));
102
+ registerAdapter(new Adapter('kook', KookBot));
103
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAkF,MAAM,aAAa,CAAC;AACrH,OAAO,IAAI,MAAM,M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C,CAAA"}
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@zhin.js/adapter-kook",
3
+ "version": "1.0.1",
4
+ "description": "zhin adapter for kook",
5
+ "type": "module",
6
+ "main": "./lib/index.js",
7
+ "types": "./lib/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./lib/index.d.ts",
11
+ "import": "./lib/index.js"
12
+ }
13
+ },
14
+ "dependencies": {
15
+ "kook-client": "latest"
16
+ },
17
+ "devDependencies": {
18
+ "typescript": "^5.3.0"
19
+ },
20
+ "peerDependencies": {
21
+ "zhin.js": "1.0.1",
22
+ "@zhin.js/types": "1.0.1"
23
+ },
24
+ "peerDependenciesMeta": {
25
+ "@zhin.js/types": {
26
+ "optional": true
27
+ }
28
+ },
29
+ "scripts": {
30
+ "build": "tsc",
31
+ "clean": "rm -rf lib"
32
+ }
33
+ }
package/src/index.ts ADDED
@@ -0,0 +1,120 @@
1
+ import { Client, PrivateMessageEvent,MessageSegment as MessageElem,ChannelMessageEvent, Sendable} from "kook-client";
2
+ import path from "path";
3
+ import {
4
+ Bot,
5
+ BotConfig,
6
+ Adapter,
7
+ Plugin,
8
+ registerAdapter,
9
+ Message,
10
+ SendOptions,
11
+ MessageSegment,
12
+ SendContent,
13
+ segment
14
+ } from "zhin.js";
15
+ declare module 'zhin.js'{
16
+ interface RegisteredAdapters{
17
+ kook:Adapter<KookBot>
18
+ }
19
+ }
20
+ export interface KookBotConfig extends BotConfig,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(private plugin:Plugin,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<void>=> {
52
+ if(!Array.isArray(content)) content=[content];
53
+ if(quote) content.unshift({type:'reply',data:{id:typeof quote==="boolean"?message.$id:quote}})
54
+ this.plugin.dispatch('message.send',{
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<void> {
77
+ options=await this.plugin.app.handleBeforeSend(options)
78
+ switch (options.type){
79
+ case 'private':{
80
+ const result= await this.sendPrivateMsg(options.id,KookBot.toSendable(options.content))
81
+ this.plugin.logger.info(`send ${options.type}(${options.id}):${segment.raw(options.content)}`)
82
+ break;
83
+ }
84
+ case "channel":{
85
+ const result=await this.sendChannelMsg(options.id,KookBot.toSendable(options.content))
86
+ this.plugin.logger.info(`send ${options.type}(${options.id}):${segment.raw(options.content)}`)
87
+ break;
88
+ }
89
+ default:
90
+ throw new Error(`unsupported channel type ${options.type}`)
91
+ }
92
+ }
93
+
94
+ private handleKookMessage(msg: PrivateMessageEvent|ChannelMessageEvent): void {
95
+ const message=this.$formatMessage(msg)
96
+ this.plugin.dispatch('message.receive',message)
97
+ this.plugin.logger.info(`recv ${message.$channel.type}(${message.$channel.id}):${segment.raw(message.$content)}`)
98
+ this.plugin.dispatch(`message.${message.$channel.type}.receive`,message)
99
+ }
100
+
101
+ }
102
+ export namespace KookBot{
103
+ export function toSegments(message:Sendable):MessageSegment[]{
104
+ if(!Array.isArray(message)) message=[message]
105
+ return message.map((item):MessageSegment=>{
106
+ if(typeof item==="string") return {type:'text',data:{text:item}}
107
+ const {type,...data}=item
108
+ return {type:type==='markdown'?'text':type,data}
109
+ })
110
+ }
111
+ export function toSendable(content:SendContent):Sendable{
112
+ if(!Array.isArray(content)) content=[content]
113
+ return content.map((segment):MessageElem=>{
114
+ if(typeof segment==="string") return {type:'text',text:segment}
115
+ const {type,data}=segment
116
+ return {type,...data} as MessageElem
117
+ })
118
+ }
119
+ }
120
+ registerAdapter(new Adapter('kook',KookBot))
package/tsconfig.json ADDED
@@ -0,0 +1,24 @@
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
+ }