Skip to content

Commit

Permalink
fix(kook): fix support for message-updated and deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Nov 4, 2023
1 parent bc57c50 commit 7224003
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
2 changes: 1 addition & 1 deletion adapters/kook/package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
4 changes: 2 additions & 2 deletions adapters/kook/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class HttpServer<C extends Context = Context> extends Adapter<C, KookBot<
super()
let { path } = bot.config as HttpServer.Config
path = sanitize(path)
ctx.router.post(path, (ctx) => {
ctx.router.post(path, async (ctx) => {
const { body } = ctx.request
logger.debug('receive %o', body)

Expand All @@ -27,7 +27,7 @@ export class HttpServer<C extends Context = Context> extends Adapter<C, KookBot<
if (!bot) return

// dispatch events
const session = adaptSession(bot, body.d)
const session = await adaptSession(bot, body.d)
if (session) bot.dispatch(session)
})
}
Expand Down
11 changes: 7 additions & 4 deletions adapters/kook/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,13 @@ export class KookMessageEncoder<C extends Context = Context> 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({
Expand Down
4 changes: 3 additions & 1 deletion adapters/kook/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ export namespace Card {

export interface Paragraph {
type: 'paragraph'
content: string
cols: number
fields: Text[]
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
32 changes: 25 additions & 7 deletions adapters/kook/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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)
}

Expand All @@ -147,7 +165,7 @@ function adaptReaction(body: Kook.NoticeBody, session: Session) {
session['emoji'] = body.emoji.id
}

export function adaptSession<C extends Context>(bot: Bot<C>, input: any) {
export async function adaptSession<C extends Context>(bot: KookBot<C>, input: any) {
const session = bot.session()
session.setInternal('kook', input)
if (input.type === Kook.Type.system) {
Expand All @@ -160,20 +178,20 @@ export function adaptSession<C extends Context>(bot: Bot<C>, 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,
}
break
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':
Expand Down
2 changes: 1 addition & 1 deletion adapters/kook/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class WsClient<C extends Context = Context> extends Adapter.WsClient<C, K
logger.debug('[receive] %o', parsed)
if (parsed.s === Signal.event) {
this._sn = Math.max(this._sn, parsed.sn)
const session = adaptSession(this.bot, parsed.d)
const session = await adaptSession(this.bot, parsed.d)
if (session) this.bot.dispatch(session)
} else if (parsed.s === Signal.hello) {
this._heartbeat = setInterval(() => this.heartbeat(), Time.minute * 0.5)
Expand Down

0 comments on commit 7224003

Please sign in to comment.