Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(qq): more error logs #236

Merged
merged 3 commits into from Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 34 additions & 20 deletions adapters/qq/src/bot/index.ts
Expand Up @@ -61,27 +61,41 @@ export class QQBot<C extends Context = Context> extends Bot<C, QQBot.Config> {
}

async _ensureAccessToken() {
const result = await this.ctx.http.post<GetAppAccessTokenResult>('https://bots.qq.com/app/getAppAccessToken', {
appId: this.config.id,
clientSecret: this.config.secret,
})
let endpoint = this.config.endpoint
if (this.config.sandbox) {
endpoint = endpoint.replace(/^(https?:\/\/)/, '$1sandbox.')
try {
const result = await this.ctx.http.axios<GetAppAccessTokenResult>({
url: 'https://bots.qq.com/app/getAppAccessToken',
method: 'post',
data: {
appId: this.config.id,
clientSecret: this.config.secret,
},
})
if (!result.data.access_token) {
this.logger.warn(`POST https://bots.qq.com/app/getAppAccessToken response: %o, trace id: %s`, result.data, result.headers['x-tps-trace-id'])
throw new Error('failed to refresh access token')
}
let endpoint = this.config.endpoint
if (this.config.sandbox) {
endpoint = endpoint.replace(/^(https?:\/\/)/, '$1sandbox.')
}
this._token = result.data.access_token
this.groupHttp = this.ctx.http.extend({
endpoint,
headers: {
'Authorization': `QQBot ${this._token}`,
'X-Union-Appid': this.config.id,
},
})
// 在上一个 access_token 接近过期的 60 秒内
// 重新请求可以获取到一个新的 access_token
this._timer = setTimeout(() => {
this._ensureAccessToken()
}, (result.data.expires_in - 40) * 1000)
} catch (e) {
if (!Quester.isAxiosError(e) || !e.response) throw e
this.logger.warn(`POST https://bots.qq.com/app/getAppAccessToken response: %o, trace id: %s`, e.response.data, e.response.headers['x-tps-trace-id'])
throw e
}
this._token = result.access_token
this.groupHttp = this.ctx.http.extend({
endpoint,
headers: {
'Authorization': `QQBot ${this._token}`,
'X-Union-Appid': this.config.id,
},
})
// 在上一个 access_token 接近过期的 60 秒内
// 重新请求可以获取到一个新的 access_token
this._timer = setTimeout(() => {
this._ensureAccessToken()
}, (result.expires_in - 40) * 1000)
}

async getAccessToken() {
Expand Down
8 changes: 8 additions & 0 deletions adapters/qq/src/internal/group.ts
Expand Up @@ -17,6 +17,8 @@ declare module './internal' {
acknowledgeInteraction(interaction_id: string, data: {
code: number
}): Promise<any>
getGateway(): Promise<QQ.GetGatewayResponse>
getGatewayBot(): Promise<QQ.GetGatewayBotResponse>
}
}

Expand All @@ -36,4 +38,10 @@ GroupInternal.define(false, {
'/interactions/{interaction.id}': {
PUT: 'acknowledgeInteraction',
},
'/gateway': {
GET: 'getGateway',
},
'/gateway/bot': {
GET: 'getGatewayBot',
},
})
3 changes: 2 additions & 1 deletion adapters/qq/src/internal/internal.ts
Expand Up @@ -33,8 +33,9 @@ export class Internal {
this.bot.logger.debug(`${method} ${url} response: %o, trace id: %s`, response.data, response.headers['x-tps-trace-id'])
return response.data
} catch (error) {
this.bot.logger.warn(`${method} ${url} request: %o`, config)
if (!Quester.isAxiosError(error) || !error.response) throw error
this.bot.logger.debug(`${method} ${url} response: %o, trace id: %s`, error.response.data, error.response.headers['x-tps-trace-id'])
this.bot.logger.warn(`${method} ${url} response: %o, trace id: %s`, error.response.data, error.response.headers['x-tps-trace-id'])
throw error
}
}
Expand Down
3 changes: 3 additions & 0 deletions adapters/qq/src/message.ts
Expand Up @@ -82,6 +82,9 @@ export class QQGuildMessageEncoder<C extends Context = Context> extends MessageE
await this.resolveFile(null, true)
await this.flush()
}
if (useFormData) {
this.bot.logger.warn(`POST ${endpoint} response: %o, trace id: %s`, e.response.data, e.response.headers['x-tps-trace-id'])
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions adapters/qq/src/types.ts
Expand Up @@ -177,6 +177,21 @@ export interface GatewayEvents {
C2C_MSG_RECEIVE: UserEvent
}

export interface GetGatewayResponse {
url: string
}

export interface GetGatewayBotResponse {
url: string
shards: number
session_start_limit: {
total: number
remaining: number
reset_after: number
max_concurrency: number
}
}

export interface PayloadStructure<O extends Opcode, T extends keyof GatewayEvents, D> {
/** opcode for the payload */
op: O
Expand Down
2 changes: 1 addition & 1 deletion adapters/qq/src/ws.ts
Expand Up @@ -10,7 +10,7 @@ export class WsClient<C extends Context = Context> extends Adapter.WsClient<C, Q

async prepare() {
await this.bot.getAccessToken()
let { url } = await this.bot.groupHttp.get(`/gateway`)
let { url } = await this.bot.internal.getGateway()
url = url.replace('api.sgroup.qq.com', new URL(this.bot.config.endpoint).host)
this.bot.logger.debug('url: %s', url)
return this.bot.groupHttp.ws(url)
Expand Down