Skip to content

Commit

Permalink
fix(qq): status
Browse files Browse the repository at this point in the history
  • Loading branch information
idranme committed Nov 23, 2024
1 parent bfa7e86 commit 965c091
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
12 changes: 3 additions & 9 deletions adapters/qq/src/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,9 @@ export class QQBot<C extends Context = Context> extends Bot<C, QQBot.Config> {
}

async initialize() {
try {
const user = await this.guildBot.internal.getMe()
Object.assign(this.user, user)
this.user.name = user.username
} catch (e) {
if (this.http.isError(e) && e.response) {
this.logger.warn(`GET /users/@me response: %o`, e.response.data)
}
}
const user = await this.guildBot.internal.getMe()
Object.assign(this.user, user)
this.user.name = user.username
}

async stop() {
Expand Down
24 changes: 19 additions & 5 deletions adapters/qq/src/http.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Adapter, Binary, Context, Schema } from '@satorijs/core'
import { Adapter, Binary, Context, Schema, Universal } from '@satorijs/core'
import { getPublicKeyAsync, signAsync, verifyAsync } from '@noble/ed25519'
import { QQBot } from './bot'
import { Opcode, Payload } from './types'
Expand All @@ -13,7 +13,7 @@ export class HttpServer<C extends Context = Context> extends Adapter<C, QQBot<C>
if (bot.config.authType === 'bearer') {
await bot.getAccessToken()
}
await bot.initialize()
await this.initialize(bot)

bot.ctx.server.post(bot.config.path, async (ctx) => {
const bot = this.bots.find(bot => bot.config.id === ctx.get('X-Bot-Appid'))
Expand All @@ -25,9 +25,6 @@ export class HttpServer<C extends Context = Context> extends Adapter<C, QQBot<C>
const key = this.getPrivateKey(bot.config.secret)
const data = payload.d.event_ts + payload.d.plain_token
const sig = await signAsync(new TextEncoder().encode(data), key)
setTimeout(() => {
bot.online()
}, 0)
ctx.body = {
plain_token: payload.d.plain_token,
signature: Binary.toHex(sig)

Check failure on line 30 in adapters/qq/src/http.ts

View workflow job for this annotation

GitHub Actions / lint

Missing trailing comma
Expand All @@ -40,6 +37,9 @@ export class HttpServer<C extends Context = Context> extends Adapter<C, QQBot<C>
return ctx.status = 403
}

if (bot.status !== Universal.Status.ONLINE) {
await this.initialize(bot)
}
bot.dispatch(bot.session({
type: 'internal',
_type: 'qq/' + payload.t.toLowerCase().replace(/_/g, '-'),
Expand All @@ -56,6 +56,20 @@ export class HttpServer<C extends Context = Context> extends Adapter<C, QQBot<C>
})
}

async initialize(bot: QQBot) {
try {
await bot.initialize()
bot.online()
} catch (e) {
if (bot.http.isError(e) && e.response) {
bot.logger.warn(`GET /users/@me response: %o`, e.response.data)
} else {
bot.logger.warn(e)
}
bot.offline()
}
}

private getPrivateKey(secret: string) {
const seedSize = 32
let seed = secret
Expand Down
6 changes: 5 additions & 1 deletion adapters/qq/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ export class WsClient<C extends Context = Context> extends Adapter.WsClient<C, Q
this._sessionId = parsed.d.session_id
this.bot.user = decodeUser(parsed.d.user)
this.bot.guildBot.user = this.bot.user
await this.bot.initialize()
try {
await this.bot.initialize()
} catch (e) {
this.bot.logger.warn(e)
}
return this.bot.online()
}
if (parsed.t === 'RESUMED') {
Expand Down

0 comments on commit 965c091

Please sign in to comment.