diff --git a/adapters/kook/package.json b/adapters/kook/package.json index 547a38ff..c14a75eb 100644 --- a/adapters/kook/package.json +++ b/adapters/kook/package.json @@ -1,7 +1,7 @@ { "name": "@satorijs/adapter-kook", "description": "KOOK (开黑啦) Adapter for Satorijs", - "version": "4.0.7", + "version": "4.1.3", "main": "lib/index.js", "typings": "lib/index.d.ts", "files": [ diff --git a/adapters/kook/src/http.ts b/adapters/kook/src/http.ts index b4bad148..509ad2dd 100644 --- a/adapters/kook/src/http.ts +++ b/adapters/kook/src/http.ts @@ -12,7 +12,7 @@ export class HttpServer extends Adapter { + ctx.router.post(path, async (ctx) => { const { body } = ctx.request logger.debug('receive %o', body) @@ -27,7 +27,7 @@ export class HttpServer extends Adapter extends MessageEnco }) } else if (type === 'button-group') { this.flushText() - this.cardBuffer.push({ - type: 'action-group', - elements: element.children.map(encodeButton), - }) + const elements = element.children.map(encodeButton) + while (elements.length) { + this.cardBuffer.push({ + type: 'action-group', + elements: elements.splice(0, 4), + }) + } } else if (type === 'hr') { this.flushText() this.cardBuffer.push({ diff --git a/adapters/kook/src/types.ts b/adapters/kook/src/types.ts index ca18d224..d44cea46 100644 --- a/adapters/kook/src/types.ts +++ b/adapters/kook/src/types.ts @@ -155,7 +155,6 @@ export namespace Card { export interface Paragraph { type: 'paragraph' - content: string cols: number fields: Text[] } @@ -292,6 +291,7 @@ export interface Channel { id: string name: string user_id: string + user_info?: User guild_id: string is_category: boolean parent_id: string @@ -312,6 +312,8 @@ export interface Channel { export interface NoticeBody extends Channel, MessageMeta, GuildRole { value: string msg_id: string + channel_type: 'GROUP' | 'PERSON' + author_id: string target_id: string channel_id: string operator_id: string diff --git a/adapters/kook/src/utils.ts b/adapters/kook/src/utils.ts index 274e01c0..2c420ede 100644 --- a/adapters/kook/src/utils.ts +++ b/adapters/kook/src/utils.ts @@ -1,5 +1,6 @@ -import { Bot, Context, h, isNullable, Session, Universal } from '@satorijs/satori' +import { Context, h, isNullable, Session, Universal } from '@satorijs/satori' import * as Kook from './types' +import { KookBot } from './bot' export * from './types' @@ -135,8 +136,25 @@ function adaptMessageCreate(data: Kook.Data, meta: Kook.MessageExtra, session: S adaptMessageSession(data, meta, session.event.message = {}, session.event) } -function adaptMessageModify(data: Kook.Data, meta: Kook.NoticeBody, session: Session) { - session.channelId = meta.channel_id +async function adaptMessageModify(bot: KookBot, data: Kook.Data, meta: Kook.NoticeBody, session: Session) { + session.guildId = meta.guild_id + // updated_private_message 事件会有两个不同的 author_id + // 这里优先使用 meta.user_info,当不存在时使用 meta.author_id + session.event.user = meta.user_info && adaptUser(meta.user_info) + session.userId = meta.author_id + // message_btn_click 事件会有两个不同的 channel_type + // 其中 data.channel_type 永远是 PERSON (即使是群聊环境) + // 但其他任何事件都未出现 meta.channel_type,因此这里做了 fallback + if ((meta.channel_type || data.channel_type) === 'GROUP') { + session.isDirect = false + // updated_message 事件使用 meta.channel_id + // message_btn_click 事件使用 meta.target_id + session.channelId = meta.channel_id || meta.target_id + } else { + session.isDirect = true + // 私聊环境的 message_btn_click 事件中拿不到 channel_id + session.channelId = meta.chat_code || (await bot.createDirectChannel(session.userId)).id + } adaptMessageSession(data, meta, session.event.message = {}, session.event) } @@ -147,7 +165,7 @@ function adaptReaction(body: Kook.NoticeBody, session: Session) { session['emoji'] = body.emoji.id } -export function adaptSession(bot: Bot, input: any) { +export async function adaptSession(bot: KookBot, input: any) { const session = bot.session() session.setInternal('kook', input) if (input.type === Kook.Type.system) { @@ -160,7 +178,7 @@ export function adaptSession(bot: Bot, input: any) { switch (type) { case 'message_btn_click': session.type = 'interaction/button' - adaptMessageModify(input, body, session) + await adaptMessageModify(bot, input, body, session) session.event.button = { id: body.value, } @@ -168,12 +186,12 @@ export function adaptSession(bot: Bot, input: any) { case 'updated_message': case 'updated_private_message': session.type = 'message-updated' - adaptMessageModify(input, body, session) + await adaptMessageModify(bot, input, body, session) break case 'deleted_message': case 'deleted_private_message': session.type = 'message-deleted' - adaptMessageModify(input, body, session) + await adaptMessageModify(bot, input, body, session) break case 'added_reaction': case 'private_added_reaction': diff --git a/adapters/kook/src/ws.ts b/adapters/kook/src/ws.ts index ca83be2a..a97ebd5f 100644 --- a/adapters/kook/src/ws.ts +++ b/adapters/kook/src/ws.ts @@ -50,7 +50,7 @@ export class WsClient extends Adapter.WsClient this.heartbeat(), Time.minute * 0.5)