-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
127 lines (108 loc) · 3.97 KB
/
Copy pathindex.js
File metadata and controls
127 lines (108 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
import { readFileSync } from 'node:fs'
import { getDatabaseService } from './core/database/DatabaseService.js'
import {
Config as analysisConfig,
getMessageCollector,
reinitializeServices,
stopAllServices
} from './core/services/index.js'
// 获取插件目录和 package.json
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const packageJsonPath = path.join(__dirname, 'package.json')
const localExtensionPath = path.join(__dirname, 'data', 'temp', 'local-extension.js')
async function loadLocalExtension() {
if (!fs.existsSync(localExtensionPath)) return
await import(pathToFileURL(localExtensionPath).href)
}
async function startWebManagementServer() {
const { startWebServer } = await import('./core/web/WebServer.js')
return await startWebServer()
}
let packageJson
try {
packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'))
} catch (err) {
global.logger.error('[发言统计] 读取 package.json 失败:', err)
packageJson = { version: '5.0.0' }
}
const version = packageJson.version
// 1) 初始化数据库(主插件强依赖)
const dbService = getDatabaseService()
try {
await dbService.initialize()
} catch (err) {
const errorMsg = err.message || '数据库初始化失败'
global.logger.error('[发言统计] ' + errorMsg)
throw errorMsg
}
try {
await loadLocalExtension()
} catch (err) {
global.logger.error('[发言统计] 本地扩展初始化失败:', err)
}
try {
await startWebManagementServer()
} catch (err) {
global.logger.error('[发言统计] Web 管理端启动失败:', err)
}
// 2) 启动水群分析配置与服务生命周期
try {
await analysisConfig.load()
analysisConfig.watch()
analysisConfig.onChange(async (newConfig) => {
await reinitializeServices(newConfig)
global.logger.mark('[发言统计] 水群分析配置变更,服务已重载')
})
const gi = analysisConfig.get() || {}
if (gi.enabled !== false && gi.messageCollection?.enabled !== false) {
await getMessageCollector()
}
} catch (err) {
global.logger.error('[发言统计] 水群分析初始化失败:', err)
}
try {
const { globalConfig } = await import('./core/ConfigManager.js')
const dbConfig = globalConfig.getConfig('database') || {}
const dbType = (dbConfig.type || 'postgresql').toLowerCase()
const dbTypeName = dbType === 'sqlite' ? 'SQLite' : 'PostgreSQL'
global.logger.info('[发言统计] ---------^_^---------')
global.logger.info(`[发言统计] 发言统计插件 v${version} 初始化成功~`)
global.logger.info(`[发言统计] 使用${dbTypeName}数据库存储`)
global.logger.info('[发言统计] 支持功能:总榜、日榜、周榜、月榜、个人统计、僵尸群清理、水群分析、水群词云')
global.logger.info('[发言统计] ---------^_^---------')
} catch (err) {
global.logger.error('[发言统计] 插件初始化失败:', err)
}
// 3) apps 风格加载(参考 endfield 风格)
const appsDir = path.join(__dirname, 'apps')
const files = fs.readdirSync(appsDir).filter((file) => file.endsWith('.js'))
const modules = await Promise.allSettled(
files.map((file) => import(pathToFileURL(path.join(appsDir, file)).href))
)
const apps = {}
for (let i = 0; i < files.length; i++) {
const file = files[i]
const loaded = modules[i]
if (loaded.status !== 'fulfilled') {
global.logger.error(`[发言统计] 载入应用失败: ${file}`)
global.logger.error(loaded.reason)
continue
}
const exportsMap = loaded.value || {}
const classExport = Object.entries(exportsMap).find(([, value]) => typeof value === 'function' && value.prototype)
if (!classExport) {
global.logger.warn(`[发言统计] 应用文件未导出类,已跳过: ${file}`)
continue
}
const appName = file.replace(/\.js$/, '')
apps[appName] = classExport[1]
}
process.on('exit', () => {
stopAllServices()
analysisConfig.stop()
})
export { apps }