Skip to content

Commit

Permalink
feat(status): do not show idle bots
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Sep 2, 2020
1 parent 4d78937 commit 2ba97a0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
10 changes: 5 additions & 5 deletions packages/adapter-cqhttp/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { camelCase, Logger, snakeCase, capitalize } from 'koishi-utils'
import { Bot, AccountInfo, SenderInfo, StatusInfo, StrangerInfo, BotStatus } from 'koishi-core'
import { Bot, AccountInfo, SenderInfo, StatusInfo, StrangerInfo, BotStatusCode } from 'koishi-core'

const logger = new Logger('bot')

Expand Down Expand Up @@ -248,13 +248,13 @@ Bot.prototype.getSelfId = async function getSelfId(this: Bot) {
return userId
}

Bot.prototype.getStatus = async function getStatus(this: Bot) {
if (!this.ready) return BotStatus.BOT_IDLE
Bot.prototype.getStatusCode = async function getStatusCode(this: Bot) {
if (!this.ready) return BotStatusCode.BOT_IDLE
try {
const data = await this.get<StatusInfo>('get_status')
return data.good ? BotStatus.GOOD : data.online ? BotStatus.SERVER_ERROR : BotStatus.BOT_OFFLINE
return data.good ? BotStatusCode.GOOD : data.online ? BotStatusCode.SERVER_ERROR : BotStatusCode.BOT_OFFLINE
} catch {
return BotStatus.NET_ERROR
return BotStatusCode.NET_ERROR
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/koishi-core/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export abstract class Server {
}
}

export enum BotStatus {
export enum BotStatusCode {
/** 正常运行 */
GOOD,
/** Bot 处于闲置状态 */
Expand All @@ -112,7 +112,7 @@ export interface Bot extends BotOptions {
ready?: boolean
version?: string
getSelfId(): Promise<number>
getStatus(): Promise<BotStatus>
getStatusCode(): Promise<BotStatusCode>
getMemberMap(groupId: number): Promise<Record<number, string>>
sendGroupMsg(groupId: number, message: string, autoEscape?: boolean): Promise<number>
sendPrivateMsg(userId: number, message: string, autoEscape?: boolean): Promise<number>
Expand Down
14 changes: 8 additions & 6 deletions packages/plugin-status/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, App } from 'koishi-core'
import { Context, App, BotStatusCode } from 'koishi-core'
import { cpus, totalmem, freemem } from 'os'
import { ActiveData } from './database'

Expand Down Expand Up @@ -71,7 +71,7 @@ export interface Status extends ActiveData {
export interface BotStatus {
label?: string
selfId: number
code: number
code: BotStatusCode
rate?: number
}

Expand Down Expand Up @@ -140,9 +140,11 @@ export function apply(ctx: Context, config: Config) {
.action(async () => {
const { bots: apps, cpu, memory, startTime, activeUsers, activeGroups } = await getStatus(config)

const output = apps.map(({ label, selfId, code, rate }) => {
return `${label || selfId}${code ? '无法连接' : `工作中(${rate}/min)`}`
})
const output = apps
.filter(bot => bot.code !== BotStatusCode.BOT_IDLE)
.map(({ label, selfId, code, rate }) => {
return `${label || selfId}${code ? '无法连接' : `工作中(${rate}/min)`}`
})

output.push('==========')

Expand All @@ -163,7 +165,7 @@ export function apply(ctx: Context, config: Config) {
Promise.all(app.bots.map(async (bot): Promise<BotStatus> => ({
selfId: bot.selfId,
label: bot.label,
code: await bot.getStatus(),
code: await bot.getStatusCode(),
rate: bot.counter.slice(1).reduce((prev, curr) => prev + curr, 0),
}))),
])
Expand Down

0 comments on commit 2ba97a0

Please sign in to comment.