Skip to content

Commit

Permalink
#铃推送fix: 修复系统信息报错的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
yusheng929 committed Aug 28, 2024
1 parent e7c08da commit ac57a79
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
12 changes: 5 additions & 7 deletions apps/state.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import { CPU, RAM, Disk } from '#state'
import { CPU, RAM, Disk, System } from '#state'
import karin from 'node-karin'

export const State = karin.command(/^#系统信息$/, async (e) => {
try {
// 获取系统信息
const systemInfo = state.getSystemInfo()
// 获取CPU信息
const CPUA = await CPU.CPUUsage()
const CPUB = await CPU.CPUInfo()
const RAMA = await RAM.RAM()
const RAMB = await RAM.SwapRAMUsage()
const adapter = e.bot.adapter.name
const implementation = e.bot.version.app_name || e.bot.version.name
const Disk = Disk.Disk()
const disk = await Disk.Disk()
const system = System.System()

// 组装消息
const msg = `系统架构:${systemInfo.system}
const msg = `系统架构:${system}
CPU:${CPUA}
CPU信息: ${CPUB}
内存:${RAMA}
内存交换: ${RAMB}
储存: \n${Disk}
储存: \n${disk}
运行环境:NodeJS ${process.version}
标准协议: ${adapter}
适配器: ${implementation}`
Expand Down
9 changes: 6 additions & 3 deletions state/Disk.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const Disk = () => {
import os from 'os'
import { execSync } from 'child_process'

const Disk = async () => {
try {
let output = ''
const platform = os.platform()

if (platform === 'win32') {
// Windows 系统使用 wmic 命令
output = execSync('wmic logicaldisk get size,freespace,caption').toString()
output = await execSync('wmic logicaldisk get size,freespace,caption').toString()
const lines = output.trim().split('\n')

const diskInfo = []
Expand All @@ -25,7 +28,7 @@ const Disk = () => {
return diskInfo.length > 0 ? diskInfo.join('\n') : '无法获取磁盘信息'
} else if (platform === 'linux' || platform === 'darwin') {
// Linux 和 macOS 系统使用 df 命令
output = execSync('df -h /').toString()
output = await execSync('df -h /').toString()
const lines = output.trim().split('\n')
const data = lines[1].split(/\s+/)
const totalSize = data[1]
Expand Down
14 changes: 14 additions & 0 deletions state/System.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os from 'os'
/**
* 先写个简单示例以后再优化
*/
const System = () => {
const platform = os.platform()
const release = os.release()
const architecture = os.arch()

return `${platform} ${release} ${architecture}`
}
export default {
System
}
3 changes: 2 additions & 1 deletion state/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import CPU from './CPU.js'
import RAM from './RAM.js'
import Disk from './Disk.js'
import System from './System.js'

export { CPU, RAM, Disk }
export { CPU, RAM, Disk, System }

0 comments on commit ac57a79

Please sign in to comment.