Skip to content

Commit

Permalink
feat(qq): support image send without temp server (#250)
Browse files Browse the repository at this point in the history
Co-authored-by: LittleC <[email protected]>
  • Loading branch information
shigma and XxLittleCxX authored Mar 24, 2024
1 parent 321b8e3 commit 64d25a4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
22 changes: 9 additions & 13 deletions adapters/qq/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as QQ from './types'
import { Context, Dict, h, MessageEncoder, Quester } from '@satorijs/satori'
import { QQBot } from './bot'
import { QQGuildBot } from './bot/guild'
import { Entry } from '@satorijs/server-temp'

export const escapeMarkdown = (val: string) =>
val
Expand Down Expand Up @@ -301,26 +300,24 @@ export class QQMessageEncoder<C extends Context = Context> extends MessageEncode
}

async sendFile(type: string, attrs: Dict) {
let url = attrs.src || attrs.url, entry: Entry | undefined
if (await this.bot.ctx.http.isLocal(url)) {
const temp = this.bot.ctx.get('server.temp')
if (!temp) {
return this.bot.logger.warn('missing temporary file service, cannot send assets with private url')
}
entry = await temp.create(url)
url = entry.url
}
await this.flush()
const url = attrs.src || attrs.url
let file_type = 0
if (type === 'img' || type === 'image') file_type = 1
else if (type === 'video') file_type = 2
else if (type === 'audio') file_type = 3
else return
const data: QQ.Message.File.Request = {
file_type,
url,
srv_send_msg: false,
}
const capture = /^data:([\w/-]+);base64,(.*)$/.exec(url)
if (capture?.[2]) {
data.file_data = capture[2]
} else if (await this.bot.ctx.http.isLocal(url)) {
data.file_data = Buffer.from((await this.bot.ctx.http.file(url)).data).toString('base64')
} else {
data.url = url
}
let res: QQ.Message.File.Response
try {
if (this.session.isDirect) {
Expand All @@ -337,7 +334,6 @@ export class QQMessageEncoder<C extends Context = Context> extends MessageEncode
await this.sendFile(type, attrs)
}
}
entry?.dispose?.()
this.retry = false
return res
}
Expand Down
2 changes: 1 addition & 1 deletion adapters/qq/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ export namespace Message {
}
export interface Request {
file_type: Type
url: string
url?: string
srv_send_msg: boolean
file_data?: unknown
}
Expand Down

0 comments on commit 64d25a4

Please sign in to comment.