feat: 调整

This commit is contained in:
ningmengchongshui 2024-06-11 19:45:04 +08:00
parent e1f53b1cc6
commit 61b099ce81
22 changed files with 383 additions and 136 deletions

View File

@ -10,11 +10,20 @@ import { CONFIG_DEFAULT_PATH, CONFIG_INIT_PATH } from './system.js'
* ********
*/
class Cfg {
/**
*
*/
config = {}
/** 监听文件 */
/**
*
*/
watcher = { config: {}, defSet: {} }
/**
*
*/
constructor() {
this.initCfg()
}

View File

@ -52,8 +52,10 @@ function createLog() {
const commandLogger = log4js.getLogger('command')
const errorLogger = log4js.getLogger('error')
/** 调整error日志等级 */
/**
* error日志等级
*/
const logger = {
trace() {
defaultLogger.trace.call(defaultLogger, ...arguments)
@ -100,7 +102,9 @@ export default function setLog() {
})
}
/** 全局变量 logger */
/**
* logger
*/
global.logger = createLog() as any
logger.chalk = chalk

View File

@ -4,9 +4,24 @@ import PluginsLoader from './plugins.loader.js'
*
*/
export default class EventListener {
/**
*
*/
prefix = ''
/**
*
*/
event = null
/**
*
*/
once = false
/**
*
*/
plugins = PluginsLoader
/**
*

View File

@ -13,8 +13,17 @@ import Handler from './plugins/handler.js'
*
*/
class PluginsLoader {
/**
*
*/
priority = []
/**
*
*/
handler = {}
/**
*
*/
task = []
//
@ -25,7 +34,9 @@ class PluginsLoader {
*/
groupGlobalCD = {}
//
/**
*
*/
singleCD = {}
/**
@ -33,9 +44,21 @@ class PluginsLoader {
*/
watcher = {}
/**
*
*/
eventMap = {
/**
*
*/
message: ['post_type', 'message_type', 'sub_type'],
/**
*
*/
notice: ['post_type', 'notice_type', 'sub_type'],
/**
*
*/
request: ['post_type', 'request_type', 'sub_type']
}
@ -89,6 +112,9 @@ class PluginsLoader {
return ret
}
/**
*
*/
pluginCount = null
/**
@ -436,8 +462,14 @@ class PluginsLoader {
}
}
/**
*
*/
e.logText = ''
/**
*
*/
if (e.message_type === 'private' || e.notice_type === 'friend') {
e.isPrivate = true
@ -453,6 +485,9 @@ class PluginsLoader {
e.logText = `[私聊][${e.sender.nickname}(${e.user_id})]`
}
/**
*
*/
if (e.message_type === 'group' || e.notice_type === 'group') {
e.isGroup = true
if (e.sender) {
@ -480,6 +515,9 @@ class PluginsLoader {
e.isGuild = true
}
/**
*
*/
if (
e.user_id &&
cfg.masterQQ.includes(String(e.user_id) || String(e.user_id))
@ -487,7 +525,9 @@ class PluginsLoader {
e.isMaster = true
}
/** 只关注主动at msg处理 */
/**
* at msg处理
*/
if (e.msg && e.isGroup) {
let groupCfg = cfg.getGroup(e.group_id)
let alias = groupCfg.botAlias
@ -509,7 +549,13 @@ class PluginsLoader {
* @param e
*/
reply(e) {
/**
*
*/
if (e.reply) {
/**
*
*/
e.replyNew = e.reply
/**
@ -581,7 +627,13 @@ class PluginsLoader {
this.count(e, msg)
return msgRes
}
/**
*
*/
} else {
/**
*
*/
e.reply = async (msg = '', _ = false, __ = {}) => {
if (!msg) return false
this.count(e, msg)
@ -609,16 +661,37 @@ class PluginsLoader {
* @param msg
*/
count(e, msg) {
/**
*
*/
let screenshot = false
/**
*
*/
if (msg && msg?.file && Buffer.isBuffer(msg?.file)) {
screenshot = true
}
/**
*
*/
this.saveCount('sendMsg')
/**
*
*/
if (screenshot) this.saveCount('screenshot')
/**
*
*/
if (e.group_id) {
/**
*
*/
this.saveCount('sendMsg', e.group_id)
/**
*
*/
if (screenshot) this.saveCount('screenshot', e.group_id)
}
}
@ -629,20 +702,53 @@ class PluginsLoader {
* @param groupId
*/
saveCount(type, groupId = '') {
/**
*
*/
let key = 'Yz:count:'
/**
*
*/
if (groupId) {
/**
*
*/
key += `group:${groupId}:`
}
/**
*
*/
let dayKey = `${key}${type}:day:${moment().format('MMDD')}`
/**
*
*/
let monthKey = `${key}${type}:month:${Number(moment().month()) + 1}`
/**
*
*/
let totalKey = `${key}${type}:total`
/**
*
*/
redis.incr(dayKey)
/**
*
*/
redis.incr(monthKey)
/**
*
*/
if (!groupId) redis.incr(totalKey)
/**
*
*/
redis.expire(dayKey, 3600 * 24 * 30)
/**
*
*/
redis.expire(monthKey, 3600 * 24 * 30)
}
@ -650,8 +756,17 @@ class PluginsLoader {
*
*/
delCount() {
/**
*
*/
let key = 'Yz:count:'
/**
*
*/
redis.set(`${key}sendMsg:total`, '0')
/**
*
*/
redis.set(`${key}screenshot:total`, '0')
}
@ -660,18 +775,31 @@ class PluginsLoader {
* @param task
*/
collectTask(task) {
for (const i of Array.isArray(task) ? task : [task])
/**
*
*/
for (const i of Array.isArray(task) ? task : [task]) {
if (i?.cron && i?.name) {
this.task.push(i)
}
}
}
/**
*
*/
createTask() {
for (const i of this.task)
/**
*
*/
for (const i of this.task) {
/**
*
*/
i.job = schedule.scheduleJob(i?.cron, async () => {
/**
*
*/
try {
if (i.log == true) logger.mark(`开始定时任务:${i.name}`)
await i.fnc()
@ -681,6 +809,7 @@ class PluginsLoader {
logger.error(error)
}
})
}
}
/**
@ -691,24 +820,48 @@ class PluginsLoader {
checkLimit(e) {
/** 禁言中 */
if (e.isGroup && e?.group?.mute_left > 0) return false
/**
*
*/
if (!e.message || e.isPrivate) return true
/**
*
*/
let config = cfg.getGroup(e.group_id)
/**
*
*/
if (config.groupGlobalCD && this.groupGlobalCD[e.group_id]) {
return false
}
/**
*
*/
if (config.singleCD && this.singleCD[`${e.group_id}.${e.user_id}`]) {
return false
}
/**
*
*/
let { msgThrottle } = this
/**
*
*/
let msgId = e.user_id + ':' + e.raw_message
if (msgThrottle[msgId]) {
return false
}
/**
*
*/
msgThrottle[msgId] = true
/**
*
*/
setTimeout(() => {
delete msgThrottle[msgId]
}, 200)
@ -722,9 +875,18 @@ class PluginsLoader {
* @returns
*/
setLimit(e) {
/**
*
*/
if (!e.message || e.isPrivate) return
/**
*
*/
let config = cfg.getGroup(e.group_id)
/**
*
*/
if (config.groupGlobalCD) {
this.groupGlobalCD[e.group_id] = true
setTimeout(() => {
@ -780,6 +942,9 @@ class PluginsLoader {
* @returns
*/
checkBlack(e) {
/**
*
*/
const other = cfg.getOther()
/** 黑名单qq */
@ -866,13 +1031,17 @@ class PluginsLoader {
const watcher = chokidar.watch(file)
const key = `${dirName}/${appName}`
/** 监听修改 */
/**
*
*/
watcher.on('change', () => {
logger.mark(`[修改插件][${dirName}][${appName}]`)
this.changePlugin(key)
})
/** 监听删除 */
/**
*
*/
watcher.on('unlink', () => {
logger.mark(`[卸载插件][${dirName}][${appName}]`)
/** 停止更新监听 */
@ -895,20 +1064,35 @@ class PluginsLoader {
watchDir(dirName) {
if (this.watcher[dirName]) return
const watcher = chokidar.watch(`./${this.dir}/${dirName}/`)
/** 热更新 */
/**
*
*/
setTimeout(() => {
/** 新增文件 */
/**
*
*/
watcher.on('add', async PluPath => {
const appName = path.basename(PluPath)
if (!appName.endsWith('.js')) return
/**
*/
if (!/^(.js|.ts)$/.test(appName)) return
logger.mark(`[新增插件][${dirName}][${appName}]`)
const key = `${dirName}/${appName}`
/**
*
*/
await this.importPlugin({
name: key,
path: `../../${this.dir}/${key}?${moment().format('X')}`
})
/** 优先级排序 */
/**
*
*/
this.priority = lodash.orderBy(this.priority, ['priority'], ['asc'])
/**
*
*/
this.watch(dirName, appName)
})
}, 10000)

View File

@ -20,16 +20,24 @@ export const PluginSuperDefine: {
*
*/
export class Messages {
count = 0
rule: {
#count = 0
#rule: {
reg: RegExp
fnc: string
}[] = []
#init = PluginSuperDefine
/**
*
* @param init
*/
constructor(init?: typeof PluginSuperDefine) {
this.#init = init
for (const key in init) {
if (Object.prototype.hasOwnProperty.call(this.#init, key)) {
this.#init[key] = init[key]
}
}
}
/**
@ -38,10 +46,10 @@ export class Messages {
* @param fnc
*/
response(reg: RegExp, fnc: MessageCallBackType) {
this.count++
const propName = `prop_${this.count}`
this.#count++
const propName = `prop_${this.#count}`
this[propName] = fnc
this.rule.push({
this.#rule.push({
reg,
fnc: propName
})
@ -55,9 +63,10 @@ export class Messages {
constructor() {
super({
...App.#init,
rule: App.rule
rule: App.#rule
})
for (const key of App.rule) {
for (const key of App.#rule) {
// 确认存在该函数
if (App[key.fnc] instanceof Function) {
this[key.fnc] = App[key.fnc].bind(App)
}
@ -75,12 +84,12 @@ export class Events {
/**
*
*/
count = 0
#count = 0
/**
*
*/
data: {
#data: {
[key: string]: typeof plugin
} = {}
@ -89,14 +98,14 @@ export class Events {
* @param val
*/
use(val: typeof plugin) {
this.count++
this.data[this.count] = val
this.#count++
this.#data[this.#count] = val
}
/**
*
*/
get ok() {
return this.data
return this.#data
}
}

View File

@ -1,13 +1,23 @@
import { Common } from '../../miao.js'
import { EventType } from './types.js'
const stateArr = {}
const State = {}
const SymbolTimeout = Symbol('Timeout')
const SymbolResolve = Symbol('Resolve')
export class plugin {
/**
* @deprecated
*/
name = 'your-plugin'
/**
* @deprecated
*/
dsc = '无'
/**
* @deprecated
*/
task = null
rule: {
reg?: RegExp | string
fnc: string
@ -17,7 +27,6 @@ export class plugin {
}[] = []
event = 'message'
priority = 9999
task = null
namespace = null
handler = null
e: EventType
@ -53,12 +62,21 @@ export class plugin {
task,
rule
}: {
/**
* @deprecated
*/
name?: typeof this.name
/**
* @deprecated
*/
dsc?: typeof this.dsc
namespace?: typeof this.namespace
priority?: typeof this.priority
handler?: typeof this.handler
event?: typeof this.event
/**
* @deprecated
*/
task?: typeof this.task
rule?: typeof this.rule
}) {
@ -92,6 +110,7 @@ export class plugin {
* @param quote
* @param data.recallMsg 0-1200
* @param data.at at用户
* @deprecated
*/
reply(msg = '', quote = false, data = {}) {
if (!this.e?.reply || !msg) return false
@ -99,14 +118,20 @@ export class plugin {
}
/**
* ******
* tudo
*
* *****
* @deprecated
*/
group_id: number
/**
* @deprecated
*/
groupId: number
/**
* @deprecated
*/
user_id: number
/**
* @deprecated
*/
userId: number
/**
@ -135,17 +160,17 @@ export class plugin {
timeout = '操作超时已取消'
) {
const key = this.conKey(isGroup)
if (!stateArr[key]) stateArr[key] = {}
stateArr[key][type] = this.e
if (!State[key]) State[key] = {}
State[key][type] = this.e
if (time)
stateArr[key][type][SymbolTimeout] = setTimeout(() => {
if (stateArr[key][type]) {
const resolve = stateArr[key][type][SymbolResolve]
delete stateArr[key][type]
State[key][type][SymbolTimeout] = setTimeout(() => {
if (State[key][type]) {
const resolve = State[key][type][SymbolResolve]
delete State[key][type]
resolve ? resolve(false) : this.reply(timeout, true)
}
}, time * 1000)
return stateArr[key][type]
return State[key][type]
}
/**
@ -155,8 +180,8 @@ export class plugin {
* @returns
*/
getContext(type: string, isGroup?: boolean) {
if (type) return stateArr[this.conKey(isGroup)]?.[type]
return stateArr[this.conKey(isGroup)]
if (type) return State[this.conKey(isGroup)]?.[type]
return State[this.conKey(isGroup)]
}
/**
@ -166,9 +191,9 @@ export class plugin {
*/
finish(type: string, isGroup?: boolean) {
const key = this.conKey(isGroup)
if (stateArr[key]?.[type]) {
clearTimeout(stateArr[key][type][SymbolTimeout])
delete stateArr[key][type]
if (State[key]?.[type]) {
clearTimeout(State[key][type][SymbolTimeout])
delete State[key][type]
}
}
@ -194,7 +219,7 @@ export class plugin {
}
/**
*
* @deprecated
* @param plugin
* @param tpl
* @param data

View File

@ -10,7 +10,7 @@ Data.createDir('/data/db', 'root')
/**
* DB自定义
*/
const sequelize = new Sequelize({
export const sequelize = new Sequelize({
dialect: 'sqlite',
storage: join(process.cwd(), '/data/db/data.db'),
logging: false
@ -34,8 +34,3 @@ export default class BaseModel extends Model {
model.COLUMNS = columns
}
}
/**
*
*/
export { sequelize }

View File

@ -1,6 +1,5 @@
import BaseModel from './BaseModel.js'
const { Types } = BaseModel
import { DataTypes } from 'sequelize'
/**
*
@ -8,22 +7,22 @@ const { Types } = BaseModel
const COLUMNS = {
// 用户IDqq为数字
ltuid: {
type: Types.INTEGER,
type: DataTypes.INTEGER,
primaryKey: true
},
// MysUser类型mys / hoyolab
type: {
type: Types.STRING,
type: DataTypes.STRING,
defaultValue: 'mys',
notNull: true
},
// CK
ck: Types.STRING,
device: Types.STRING,
ck: DataTypes.STRING,
device: DataTypes.STRING,
uids: {
type: Types.STRING,
type: DataTypes.STRING,
get() {
let data = this.getDataValue('uids')
let ret = {}

View File

@ -1,11 +1,7 @@
import BaseModel from './BaseModel.js'
import lodash from 'lodash'
import MysUtil from '../mys/MysUtil.js'
/**
*
*/
const { Types } = BaseModel
import { DataTypes } from 'sequelize'
/**
*
@ -13,26 +9,26 @@ const { Types } = BaseModel
const COLUMNS = {
// 用户IDqq为数字
id: {
type: Types.STRING,
type: DataTypes.STRING,
autoIncrement: false,
primaryKey: true
},
type: {
type: Types.STRING,
type: DataTypes.STRING,
defaultValue: 'qq',
notNull: true
},
// 昵称
name: Types.STRING,
name: DataTypes.STRING,
// 头像
face: Types.STRING,
face: DataTypes.STRING,
ltuids: Types.STRING,
ltuids: DataTypes.STRING,
games: {
type: Types.STRING,
type: DataTypes.STRING,
get() {
let data = this.getDataValue('games')
let ret = {}
@ -54,7 +50,7 @@ const COLUMNS = {
this.setDataValue('games', JSON.stringify(data))
}
},
data: Types.STRING
data: DataTypes.STRING
}
/**

View File

@ -1,23 +1,18 @@
import { DataTypes } from 'sequelize'
import BaseModel from './BaseModel.js'
import lodash from 'lodash'
/**
*
*/
const { Types } = BaseModel
/**
*
*/
const COLUMNS = {
// 用户IDqq为数字
userId: {
type: Types.STRING
type: DataTypes.STRING
},
game: Types.STRING,
uid: Types.STRING,
game: DataTypes.STRING,
uid: DataTypes.STRING,
data: {
type: Types.STRING,
type: DataTypes.STRING,
get() {
let data = this.getDataValue('data')
let ret = {}

13
src/global.d.ts vendored
View File

@ -54,21 +54,26 @@ type ChalkInstanceType = {
declare global {
/**
* @deprecated 使
* @deprecated 使
*/
var redis: RedisClientType
/**
* @deprecated 使
*
* @deprecated 使
*/
var Bot: typeof Yunzai.prototype
/**
* @deprecated 使
* @deprecated 使
*/
var segment: typeof se
/**
* @deprecated 使
* @deprecated 使
*/
var plugin: typeof p
/**
*
* 使 logger.chalk
*/
var logger: LoggerType &
ChalkInstanceType & {
chalk: ChalkInstance

View File

@ -3,9 +3,19 @@
*/
import MysUtil from './MysUtil.js'
/**
*
*/
const cacheMap = {}
/**
*
*/
const reFn = {}
/**
*
*/
export default class BaseModel {
_uuid = null

View File

@ -3,11 +3,19 @@ import BaseModel from './BaseModel.js'
import MysUtil from './MysUtil.js'
import { REDIS_ROOT_KEY } from '../config/system.js'
//
/**
*
*/
const servs = ['mys', 'hoyolab']
// 超时时间不必精确直接定24小时即可
/**
* 24
*/
const EX = 3600 * 24
//
/**
*
*/
const redisKeyRoot = REDIS_ROOT_KEY
/**

View File

@ -5,16 +5,14 @@
* MysUser对应一个有效CK
* MysUser可能有多个MysUid关联记录
*/
import lodash from 'lodash'
import fetch from 'node-fetch'
import DailyCache from './DailyCache.js'
import BaseModel from './BaseModel.js'
import NoteUser from './NoteUser.js'
import MysApi from './mysApi.js'
import MysUtil from './MysUtil.js'
import lodash from 'lodash'
import fetch from 'node-fetch'
import { MysUserDB } from '../db/index.js'
import { Data } from '../miao.js'
const tables = {

View File

@ -11,7 +11,7 @@ const games = [
/**
*
*/
const MysUtil = {
export default {
/**
* ltuid
* @param data
@ -87,8 +87,3 @@ const MysUtil = {
await Data.forEach(['mys', 'hoyolab'], fn)
}
}
/**
*
*/
export default MysUtil

View File

@ -14,6 +14,13 @@ import { Data } from '../miao.js'
* *******************
*/
export default class NoteUser extends BaseModel {
db = null
qq = null
mysUsers = null
_map = null
/**
*
* @param qq
@ -129,10 +136,6 @@ export default class NoteUser extends BaseModel {
})
}
db = null
qq = null
/**
*
* @param db
@ -152,8 +155,6 @@ export default class NoteUser extends BaseModel {
await this.save()
}
mysUsers = null
/**
* MysUser对象
*/
@ -425,8 +426,6 @@ export default class NoteUser extends BaseModel {
return await this.delMysUser(ltuid)
}
_map = null
/**
*
* @param mysUser

View File

@ -1,6 +0,0 @@
mys 存在循环引用
(!) Circular dependencies
src/mys/apiTool.ts -> src/mys/mysApi.ts -> src/mys/apiTool.ts
src/mys/gsCfg.ts -> src/mys/mysInfo.ts -> src/mys/gsCfg.ts
src/mys/NoteUser.ts -> src/mys/MysUser.ts -> src/mys/NoteUser.ts

View File

@ -7,14 +7,12 @@ import mysInfo from './mysInfo.js'
import MysUser from './MysUser.js'
import MysUtil from './MysUtil.js'
import NoteUser from './NoteUser.js'
export {
apiTool,
BaseModel,
DailyCache,
gsCfg,
mysApi,
mysInfo,
MysUser,
MysUtil,
NoteUser
}
export const ApiTool = apiTool
export const GSCfg = gsCfg
export const MysApi = mysApi
export const MysInfo = mysInfo
export { BaseModel, DailyCache, MysUser, MysUtil, NoteUser }
/**
* @deprecated
*/
export { apiTool, gsCfg, mysApi, mysInfo }

View File

@ -3,6 +3,9 @@ import fetch from 'node-fetch'
import cfg from '../config/config.js'
import apiTool from './apiTool.js'
/**
*
*/
let HttpsProxyAgent = null
/**

View File

@ -1,9 +1,6 @@
import MysApi from './mysApi.js'
// tudo
import GsCfg from './gsCfg.js'
import lodash from 'lodash'
import MysApi from './mysApi.js'
import GsCfg from './gsCfg.js'
import NoteUser from './NoteUser.js'
import MysUser from './MysUser.js'
import DailyCache from './DailyCache.js'
@ -15,14 +12,23 @@ import DailyCache from './DailyCache.js'
*/
export default class MysInfo {
static tips = '请先#绑定Cookie\n发送【Cookie帮助】查看配置教程'
/**
* uid
*/
uid = null
e = null
userId = null
/**
* ck信息
*/
ckInfo = null
auth = null
gtest = null
mysButton = null
/**
* ck对应MysUser对象
*/
ckUser = null
/**
*
* @param e
@ -32,9 +38,7 @@ export default class MysInfo {
this.e = e
this.userId = String(e.user_id)
}
/** 当前查询原神uid */
this.uid = ''
/** 当前ck信息 */
this.ckInfo = {
ck: '',
uid: '',
@ -42,7 +46,6 @@ export default class MysInfo {
ltuid: '',
type: ''
}
// ck对应MysUser对象
this.ckUser = null
this.auth = [
'dailyNote',
@ -60,9 +63,7 @@ export default class MysInfo {
'action_cardList',
'avatarInfo'
]
this.gtest = false
/**
* ********
* tudo
@ -619,8 +620,6 @@ export default class MysInfo {
return res
}
ckUser = null
/**
* ck
* @returns

View File

@ -47,6 +47,7 @@ export function mkdirs(dirname: string) {
return true
}
}
return false
}
/**

View File

@ -3,6 +3,9 @@ import { renderToString } from 'react-dom/server'
import { mkdirSync, writeFileSync } from 'fs'
import { join } from 'path'
/**
*
*/
export type ComponentCreateOpsionType = {
html_head?: string
html_name?: string
@ -18,6 +21,9 @@ export type ComponentCreateOpsionType = {
*/
export class Component {
#dir = ''
/**
*
*/
constructor() {
this.#dir = join(process.cwd(), 'html')
mkdirSync(this.#dir, {