Skip to content

Commit 08cc7f7

Browse files
committed
feat: 导出基本函数以将插件作为基类使用
1 parent e6279cd commit 08cc7f7

File tree

6 files changed

+156
-97
lines changed

6 files changed

+156
-97
lines changed

src/apps/status.ts

Lines changed: 7 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,11 @@
1-
import moment from 'node-karin/moment'
1+
import { info } from '@/root'
2+
import { karin, logger } from 'node-karin'
23
import { config } from '@/utils/config'
3-
import { EVENT_COUNT, hooks, karin, RECV_MSG, redis, SEND_MSG } from 'node-karin'
4-
5-
import type { Contact } from 'node-karin'
4+
import { getMonthStat, getTodayStat, initStat, MB, uptime } from '@/core'
65

76
export const status = karin.command(/^#$/, async (e) => {
8-
const today = moment().format('YYYY-MM-DD')
9-
const month = moment().format('YYYY-MM')
10-
11-
const [
12-
send,
13-
recv,
14-
event,
15-
sendMonth,
16-
recvMonth,
17-
eventMonth
18-
] = await Promise.all([
19-
getStat(`${SEND_MSG}:${today}*`),
20-
getStat(`${RECV_MSG}:${today}*`),
21-
getStat(`${EVENT_COUNT}:${today}*`),
22-
getStat(`${SEND_MSG}:${month}*`),
23-
getStat(`${RECV_MSG}:${month}*`),
24-
getStat(`${EVENT_COUNT}:${month}*`)
25-
])
7+
const { send, recv, event } = await getTodayStat()
8+
const { send: sendMonth, recv: recvMonth, event: eventMonth } = await getMonthStat()
269

2710
await e.reply([
2811
'------机器人状态------',
@@ -42,66 +25,6 @@ export const status = karin.command(/^#状态$/, async (e) => {
4225
return true
4326
}, { name: '状态统计' })
4427

45-
/**
46-
* 生成存储键
47-
* @param contact 联系人
48-
* @example
49-
* ```ts
50-
* friend:<peer>
51-
* group:<peer>
52-
* guild:<peer>:<subPeer>
53-
* direct:<peer>:<subPeer>
54-
* ```
55-
*/
56-
const createKey = (contact: Contact) => {
57-
const { scene, peer, subPeer } = contact
58-
return `${moment().format('YYYY-MM-DD')}:${scene}:${peer}${subPeer ? `:${subPeer}` : ''}`
59-
}
60-
61-
/**
62-
* 获取 Redis 键值统计
63-
*/
64-
const getStat = async (pattern: string) => {
65-
const keys = await redis.keys(pattern)
66-
const values = await Promise.all(keys.map((key) => redis.get(key).then(Number)))
67-
return values.reduce((total, value) => total + (value || 0), 0)
68-
}
69-
70-
const MB = () => (process.memoryUsage().rss / 1024 / 1024).toFixed(2)
71-
72-
const uptime = () => {
73-
const uptime = process.uptime()
74-
const day = Math.floor(uptime / 86400)
75-
const hour = Math.floor((uptime % 86400) / 3600)
76-
const minute = Math.floor((uptime % 3600) / 60)
77-
78-
return `${day > 0 ? `${day}天` : ''}${hour}小时${minute}分钟`
79-
}
80-
81-
(() => {
82-
if (!config().status) return
83-
84-
hooks.message((event, next) => {
85-
try {
86-
redis.incr(`${RECV_MSG}:${createKey(event.contact)}`)
87-
} finally {
88-
next()
89-
}
90-
})
91-
92-
hooks.sendMsg.message((contact, _, __, next) => {
93-
try {
94-
redis.incr(`${SEND_MSG}:${createKey(contact)}`)
95-
} finally {
96-
next()
97-
}
98-
})
28+
if (config().status) initStat()
9929

100-
hooks.eventCall((e, _, next) => {
101-
try {
102-
redis.incr(`${EVENT_COUNT}:${createKey(e.contact)}`)
103-
} finally {
104-
next()
105-
}
106-
})
107-
})()
30+
logger.info(`${logger.violet(`[插件:${info.version}]`)} ${logger.green(info.pkg.name)} 初始化完成~`)

src/core/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './redis'

src/core/redis.ts

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import moment from 'node-karin/moment'
2+
import { EVENT_COUNT, hooks, RECV_MSG, redis, SEND_MSG } from 'node-karin'
3+
4+
import type { Contact } from 'node-karin'
5+
6+
let isOn = false
7+
8+
/**
9+
* 生成存储键
10+
* @param contact 联系人
11+
* @example
12+
* ```ts
13+
* friend:<peer>
14+
* group:<peer>
15+
* guild:<peer>:<subPeer>
16+
* direct:<peer>:<subPeer>
17+
* ```
18+
*/
19+
export const createKey = (contact: Contact) => {
20+
const { scene, peer, subPeer } = contact
21+
return `${moment().format('YYYY-MM-DD')}:${scene}:${peer}${subPeer ? `:${subPeer}` : ''}`
22+
}
23+
24+
/**
25+
* 获取 Redis 键值统计
26+
* @param pattern - 键值统计的 pattern
27+
*/
28+
export const getStat = async (pattern: string) => {
29+
const keys = await redis.keys(pattern)
30+
const values = await Promise.all(keys.map((key) => redis.get(key).then(Number)))
31+
return values.reduce((total, value) => total + (value || 0), 0)
32+
}
33+
34+
/**
35+
* 获取今日统计
36+
*/
37+
export const getTodayStat = async (): Promise<{
38+
/** 今日发送消息 */
39+
send: number
40+
/** 今日收到消息 */
41+
recv: number
42+
/** 今日插件触发次数 */
43+
event: number
44+
}> => {
45+
const today = moment().format('YYYY-MM-DD')
46+
47+
const [
48+
send,
49+
recv,
50+
event,
51+
] = await Promise.all([
52+
getStat(`${SEND_MSG}:${today}*`),
53+
getStat(`${RECV_MSG}:${today}*`),
54+
getStat(`${EVENT_COUNT}:${today}*`),
55+
])
56+
57+
return {
58+
send,
59+
recv,
60+
event,
61+
}
62+
}
63+
64+
/**
65+
* 获取本月统计
66+
*/
67+
export const getMonthStat = async (): Promise<{
68+
/** 本月发送消息 */
69+
send: number
70+
/** 本月收到消息 */
71+
recv: number
72+
/** 本月插件触发次数 */
73+
event: number
74+
}> => {
75+
const month = moment().format('YYYY-MM')
76+
77+
const [
78+
send,
79+
recv,
80+
event,
81+
] = await Promise.all([
82+
getStat(`${SEND_MSG}:${month}*`),
83+
getStat(`${RECV_MSG}:${month}*`),
84+
getStat(`${EVENT_COUNT}:${month}*`),
85+
])
86+
87+
return {
88+
send,
89+
recv,
90+
event,
91+
}
92+
}
93+
94+
/**
95+
* 获取内存使用情况
96+
*/
97+
export const MB = () => (process.memoryUsage().rss / 1024 / 1024).toFixed(2)
98+
99+
/**
100+
* 获取运行时间
101+
*/
102+
export const uptime = () => {
103+
const uptime = process.uptime()
104+
const day = Math.floor(uptime / 86400)
105+
const hour = Math.floor((uptime % 86400) / 3600)
106+
const minute = Math.floor((uptime % 3600) / 60)
107+
108+
return `${day > 0 ? `${day}天` : ''}${hour}小时${minute}分钟`
109+
}
110+
111+
/**
112+
* 初始化 Redis 键值统计
113+
*/
114+
export const initStat = () => {
115+
if (isOn) return
116+
isOn = true
117+
118+
hooks.message((event, next) => {
119+
try {
120+
redis.incr(`${RECV_MSG}:${createKey(event.contact)}`)
121+
} finally {
122+
next()
123+
}
124+
})
125+
126+
hooks.sendMsg.message((contact, _, __, next) => {
127+
try {
128+
redis.incr(`${SEND_MSG}:${createKey(contact)}`)
129+
} finally {
130+
next()
131+
}
132+
})
133+
134+
hooks.eventCall((e, _, next) => {
135+
try {
136+
redis.incr(`${EVENT_COUNT}:${createKey(e.contact)}`)
137+
} finally {
138+
next()
139+
}
140+
})
141+
}

src/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
import { logger } from 'node-karin'
2-
import { info } from '@/root'
3-
4-
/** 请不要在这编写插件 不会有任何效果~ */
5-
logger.info(`${logger.violet(`[插件:${info.version}]`)} ${logger.green(info.pkg.name)} 初始化完成~`)
1+
export * from './core'

src/web.config.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { info } from './root'
2-
import { components } from 'node-karin'
2+
import { components, defineConfig } from 'node-karin'
33
import { config, writeConfig } from '@/utils/config'
44

55
import type { Config } from '@/types/config'
66

7-
export default {
7+
export default defineConfig({
88
info: {
9+
id: info.name,
910
name: '基础插件',
1011
version: info.version,
1112
description: info.pkg.description,
@@ -64,4 +65,4 @@ export default {
6465
message: '保存成功'
6566
}
6667
}
67-
}
68+
})

tsconfig.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@
2020
"rootDir": "./src",
2121
"declaration": true,
2222
"noImplicitAny": true,
23-
"strict": true
24-
},
25-
"tsc-alias": {
26-
"resolveFullPaths": true,
27-
"resolveFullExtension": ".js"
23+
"strict": true,
24+
"noEmit": true
2825
},
2926
"include": [
3027
"src/**/*",

0 commit comments

Comments
 (0)