Skip to content

Commit

Permalink
fix(qq): message timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
XxLittleCxX committed Oct 21, 2023
1 parent f446dad commit fef2844
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions adapters/qq/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,19 @@ export class QQGuildMessageEncoder extends MessageEncoder<QQGuildBot> {
let endpoint = `/channels/${this.channelId}/messages`
if (isDirect) endpoint = `/dms/${this.channelId.split('_')[0]}/messages`
const useFormData = Boolean(this.file)
let msg_id = this.options?.session?.messageId ?? this.options?.session?.id
if (this.options?.session && (Date.now() - this.options?.session?.timestamp) > MSG_TIMEOUT) {
msg_id = null
}

let r: QQ.Message
this.bot.ctx.logger('qq').debug('use form data %s', useFormData)
try {
if (useFormData) {
const form = new FormData()
form.append('content', this.content)
if (this.options?.session) {
form.append('msg_id', this.options?.session?.messageId)
if (this.options?.session && msg_id) {
form.append('msg_id', msg_id)
}
if (this.file) {
form.append('file_image', this.file, this.filename)
Expand All @@ -49,7 +54,7 @@ export class QQGuildMessageEncoder extends MessageEncoder<QQGuildBot> {
r = await this.bot.http.post<QQ.Message>(endpoint, {
...{
content: this.content,
msg_id: this.options?.session?.messageId ?? this.options?.session?.id,
msg_id,
image: this.fileUrl,
},
...(this.reference ? {
Expand Down Expand Up @@ -139,18 +144,24 @@ export class QQGuildMessageEncoder extends MessageEncoder<QQGuildBot> {
}
}

const MSG_TIMEOUT = 5 * 60 * 1000 - 2000// 5 mins

export class QQMessageEncoder extends MessageEncoder<QQBot> {
private content: string = ''
private useMarkdown = false
private rows: QQ.Button[][] = []
async flush() {
if (!this.content.trim() && !this.rows.flat().length) return
this.trimButtons()
let msg_id = this.options?.session?.messageId
if (this.options?.session && (Date.now() - this.options?.session?.timestamp) > MSG_TIMEOUT) {
msg_id = null
}
const data: QQ.SendMessageParams = {
content: this.content,
msg_type: 0,
timestamp: Math.floor(Date.now() / 1000),
msg_id: this.options?.session?.messageId,
msg_id,
}

if (this.useMarkdown) {
Expand Down

0 comments on commit fef2844

Please sign in to comment.