Skip to content

Commit

Permalink
feat(kook): support <button> element
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Nov 4, 2023
1 parent 7d2e604 commit bc57c50
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 11 deletions.
49 changes: 49 additions & 0 deletions adapters/kook/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,37 @@ export class KookMessageEncoder<C extends Context = Context> extends MessageEnco
title: attrs.title,
})
}
} else if (type === 'button') {
this.flushText()
this.cardBuffer.push({
type: 'action-group',
elements: [encodeButton(element)],
})
} else if (type === 'button-group') {
this.flushText()
this.cardBuffer.push({
type: 'action-group',
elements: element.children.map(encodeButton),
})
} else if (type === 'hr') {
this.flushText()
this.cardBuffer.push({
type: 'divider',
})
} else if (type === 'kook:countdown') {
this.flushText()
this.cardBuffer.push({
type: 'countdown',
startTime: attrs.startTime,
endTime: attrs.endTime,
mode: attrs.mode,
})
} else if (type === 'kook:invite') {
this.flushText()
this.cardBuffer.push({
type: 'invite',
code: attrs.code,
})
} else if (type === 'quote') {
await this.flush()
this.additional.quote = attrs.id
Expand Down Expand Up @@ -258,3 +289,21 @@ export namespace KookMessageEncoder {
]).role('radio').description('发送图文等混合内容时采用的方式。').default('separate'),
}).description('发送设置')
}

function encodeButton({ attrs, children }: h): Kook.Card.Button {
let theme: Kook.Card.Button.Theme = 'primary'
if (attrs.class === 'secondary') theme = 'info'
if (attrs.class === 'warning') theme = 'warning'
if (attrs.class === 'danger') theme = 'danger'
if (attrs.class === 'success') theme = 'success'
return {
type: 'button',
theme,
value: attrs.id,
click: attrs.type === 'link' ? 'link' : 'return-val',
text: {
type: 'plain-text',
content: children.join(''),
},
}
}
26 changes: 15 additions & 11 deletions adapters/kook/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ export interface Card {
}

export namespace Card {
export type Theme = 'primary' | 'secondary' | 'warning' | 'danger' | 'info'
export type Module = Section | Container | ImageGroup | Header | Divider | File | Countdown | Context
export type Theme = 'primary' | 'secondary' | 'warning' | 'danger' | 'info' | 'none' | 'success'
export type Module = Section | Container | ImageGroup | ActionGroup | Header | Divider | Invite | File | Countdown | Context

export interface Text {
type: 'plain-text' | 'kmarkdown'
Expand Down Expand Up @@ -182,22 +182,21 @@ export namespace Card {

export interface Button {
type: 'button'
theme?: Theme
value: string
theme?: Button.Theme
value?: string
text: Text
click?: string
}

export namespace Button {
export type Theme = 'primary' | 'secondary' | 'warning' | 'danger' | 'info' | 'success'
}

export interface ImageGroup {
type: 'image-group'
elements: Image[]
}

export interface ActionGroup {
type: 'action-group'
elements: Button[]
}

export interface Header {
type: 'header'
text: Text
Expand All @@ -207,6 +206,11 @@ export namespace Card {
type: 'divider'
}

export interface Invite {
type: 'invite'
code: string
}

export interface ActionGroup {
type: 'action-group'
elements: Button[]
Expand All @@ -226,8 +230,8 @@ export namespace Card {

export interface Countdown {
type: 'countdown'
end_time: string
start_time: string
endTime: string
startTime: string
mode: 'day' | 'hour' | 'second'
}
}
Expand Down
7 changes: 7 additions & 0 deletions adapters/kook/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ export function adaptSession<C extends Context>(bot: Bot<C>, input: any) {
_data: body,
}))
switch (type) {
case 'message_btn_click':
session.type = 'interaction/button'
adaptMessageModify(input, body, session)
session.event.button = {
id: body.value,
}
break
case 'updated_message':
case 'updated_private_message':
session.type = 'message-updated'
Expand Down

0 comments on commit bc57c50

Please sign in to comment.