Skip to content

Commit

Permalink
fix(satori): fix active and state, close #187
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Nov 25, 2023
1 parent f1661e8 commit 71cc428
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions adapters/satori/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class SatoriAdapter<C extends Context = Context> extends Adapter.WsClient
public http: Quester
public logger: Logger

private isActive = true
private _status = Universal.Status.OFFLINE
private sequence?: number
private timeout?: NodeJS.Timeout

Expand All @@ -26,13 +26,12 @@ export class SatoriAdapter<C extends Context = Context> extends Adapter.WsClient
}

getActive() {
return this.isActive
return this._status !== Universal.Status.OFFLINE && this._status !== Universal.Status.DISCONNECT
}

setStatus(status: Universal.Status, error?: Error): void {
if (status === Universal.Status.OFFLINE) {
this.isActive = false
}
this._status = status
if (status === Universal.Status.ONLINE) return
for (const bot of this.bots) {
bot.status = status
bot.error = error
Expand All @@ -44,9 +43,13 @@ export class SatoriAdapter<C extends Context = Context> extends Adapter.WsClient
}

getBot(platform: string, selfId: string, login: Universal.Login) {
let bot = this.bots.find(bot => bot.selfId === selfId && bot.platform === platform)
// Do not dispatch event from outside adapters.
if (bot) return this.bots.includes(bot) ? bot : undefined
let bot = this.bots.find(bot => bot.selfId === selfId && bot.platform === platform)
if (bot) {
bot.update(login)
return this.bots.includes(bot) ? bot : undefined
}

if (!login) {
this.logger.error('cannot find bot for', platform, selfId)
return
Expand Down Expand Up @@ -112,6 +115,16 @@ export class SatoriAdapter<C extends Context = Context> extends Adapter.WsClient
clearInterval(this.timeout)
})
}

async start() {
this.setStatus(Universal.Status.CONNECT)
await super.start()
}

async stop() {
this.setStatus(Universal.Status.DISCONNECT)
await super.stop()
}
}

export namespace SatoriAdapter {
Expand Down

0 comments on commit 71cc428

Please sign in to comment.