feat: add status command

This commit is contained in:
2024-09-20 16:53:38 +08:00
parent 24c439fe85
commit a1bf8d8bed
2 files changed
+55 -5

No files matched your search

@@ -7,7 +7,10 @@
package cn.rtast.fancybot
import cn.rtast.fancybot.commands.misc.CompilerCommand
import cn.rtast.fancybot.commands.AboutCommand
import cn.rtast.fancybot.commands.EchoCommand
import cn.rtast.fancybot.commands.HelpCommand
import cn.rtast.fancybot.commands.StatusCommand
import cn.rtast.fancybot.commands.lookup.CigaretteCommand
import cn.rtast.fancybot.commands.lookup.DomainPriceCommand
import cn.rtast.fancybot.commands.lookup.GPTCommand
@@ -20,11 +23,9 @@ import cn.rtast.fancybot.commands.lookup.PixivCommand
import cn.rtast.fancybot.commands.lookup.QRCodeCommand
import cn.rtast.fancybot.commands.lookup.WeatherCommand
import cn.rtast.fancybot.commands.lookup.WikipediaCommand
import cn.rtast.fancybot.commands.AboutCommand
import cn.rtast.fancybot.commands.misc.AntiRevokeCommand
import cn.rtast.fancybot.commands.EchoCommand
import cn.rtast.fancybot.commands.misc.CompilerCommand
import cn.rtast.fancybot.commands.misc.FKXQSCommand
import cn.rtast.fancybot.commands.HelpCommand
import cn.rtast.fancybot.commands.misc.HitokotoCommand
import cn.rtast.fancybot.commands.misc.LikeMeCommand
import cn.rtast.fancybot.commands.misc.RUACommand
@@ -65,6 +66,7 @@ import cn.rtast.rob.enums.ArrayMessageType
import cn.rtast.rob.util.ob.MessageChain
import cn.rtast.rob.util.ob.OneBotListener
import java.io.File
import java.time.Instant
class FancyBot : OneBotListener {
@@ -183,9 +185,11 @@ val commands = listOf(
MusicPlayUrlCommand(), DomainPriceCommand(),
SendMailCommand(), ZiBiCommand(),
UnsetZiBiCommand(), WikipediaCommand(),
AsciiArtCommand()
AsciiArtCommand(), StatusCommand()
)
val START_UP_TIME = Instant.now().epochSecond
fun initFilesDir() {
File("./files").also { it.mkdir() }
}
@@ -0,0 +1,46 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/9/20
*/
package cn.rtast.fancybot.commands
import cn.rtast.fancybot.START_UP_TIME
import cn.rtast.rob.entity.GroupMessage
import cn.rtast.rob.util.BaseCommand
import cn.rtast.rob.util.ob.MessageChain
import cn.rtast.rob.util.ob.OneBotListener
import java.time.Instant
class StatusCommand : BaseCommand() {
override val commandNames = listOf("/status")
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
val runtime = Runtime.getRuntime()
val maxMemory = runtime.maxMemory() / (1024 * 1024)
val allocatedMemory = runtime.totalMemory() / (1024 * 1024)
val freeMemory = runtime.freeMemory() / (1024 * 1024)
val usedMemory = allocatedMemory - freeMemory
val seconds = Instant.now().epochSecond - START_UP_TIME
val days = seconds / (24 * 3600)
val hours = (seconds % (24 * 3600)) / 3600
val minutes = (seconds % 3600) / 60
val remainingSeconds = seconds % 60
val osName = System.getProperty("os.name") ?: "未知操作系统"
val osArch = System.getProperty("os.arch") ?: "未知架构"
val javaVersion = System.getProperty("java.version") ?: "未知JDK版本"
val kotlinVersion = KotlinVersion.CURRENT.toString()
val msg = MessageChain.Builder()
.addText("内存占用: $usedMemory/${maxMemory}MB")
.addNewLine()
.addText("运行时间: ${days}${hours}小时 ${minutes}分钟 ${remainingSeconds}")
.addNewLine()
.addText("操作系统: $osName / 系统架构: $osArch")
.addNewLine()
.addText("Java版本: $javaVersion / Kotlin版本: $kotlinVersion")
.build()
message.reply(msg)
}
}