From 04b2a323a736687acb22f5a4212737c7735acc72 Mon Sep 17 00:00:00 2001 From: idranme Date: Fri, 6 Sep 2024 01:36:32 +0800 Subject: [PATCH] chore: improve code quality --- src/common/utils/legacyLog.ts | 5 +- src/common/utils/misc.ts | 3 +- .../listeners/NodeIKernelProfileListener.ts | 31 +------ .../services/NodeIKernelProfileLikeService.ts | 8 +- .../action/go-cqhttp/MarkMsgAsRead.ts | 2 +- src/onebot11/action/group/GetGuildList.ts | 2 +- src/onebot11/action/group/SetGroupLeave.ts | 4 +- src/onebot11/action/llonebot/Config.ts | 4 +- src/onebot11/action/llonebot/GetEvent.ts | 5 +- src/onebot11/action/system/CanSendRecord.ts | 2 +- src/onebot11/action/system/GetVersionInfo.ts | 4 +- src/onebot11/helper/eventForHttp.ts | 82 +++++++++---------- 12 files changed, 64 insertions(+), 88 deletions(-) diff --git a/src/common/utils/legacyLog.ts b/src/common/utils/legacyLog.ts index f644911..217c9e8 100644 --- a/src/common/utils/legacyLog.ts +++ b/src/common/utils/legacyLog.ts @@ -2,8 +2,9 @@ import fs from 'fs' import path from 'node:path' import { getConfigUtil } from '../config' import { LOG_DIR } from '../globalVars' +import { Dict } from 'cosmokit' -function truncateString(obj: any, maxLength = 500) { +function truncateString(obj: Dict | null, maxLength = 500) { if (obj !== null && typeof obj === 'object') { Object.keys(obj).forEach((key) => { if (typeof obj[key] === 'string') { @@ -22,7 +23,7 @@ function truncateString(obj: any, maxLength = 500) { export const logFileName = `llonebot-${new Date().toLocaleString('zh-CN')}.log`.replace(/\//g, '-').replace(/:/g, '-') -export function log(...msg: any[]) { +export function log(...msg: unknown[]) { if (!getConfigUtil().getConfig().log) { return } diff --git a/src/common/utils/misc.ts b/src/common/utils/misc.ts index 0b1e496..d3bb40a 100644 --- a/src/common/utils/misc.ts +++ b/src/common/utils/misc.ts @@ -1,4 +1,5 @@ import { QQLevel } from '@/ntqqapi/types' +import { Dict } from 'cosmokit' export function isNumeric(str: string) { return /^\d+$/.test(str) @@ -16,7 +17,7 @@ export function getBuildVersion(): number { } /** 在保证老对象已有的属性不变化的情况下将新对象的属性复制到老对象 */ -export function mergeNewProperties(newObj: any, oldObj: any) { +export function mergeNewProperties(newObj: Dict, oldObj: Dict) { Object.keys(newObj).forEach((key) => { // 如果老对象不存在当前属性,则直接复制 if (!oldObj.hasOwnProperty(key)) { diff --git a/src/ntqqapi/listeners/NodeIKernelProfileListener.ts b/src/ntqqapi/listeners/NodeIKernelProfileListener.ts index a0b744c..0690815 100644 --- a/src/ntqqapi/listeners/NodeIKernelProfileListener.ts +++ b/src/ntqqapi/listeners/NodeIKernelProfileListener.ts @@ -1,6 +1,6 @@ import { User, UserDetailInfoListenerArg } from '@/ntqqapi/types' -interface IProfileListener { +export interface IProfileListener { onProfileSimpleChanged(...args: unknown[]): void onUserDetailInfoChanged(arg: UserDetailInfoListenerArg): void @@ -12,33 +12,4 @@ interface IProfileListener { onSelfStatusChanged(...args: unknown[]): void onStrangerRemarkChanged(...args: unknown[]): void -} - -export interface NodeIKernelProfileListener extends IProfileListener { - new(listener: IProfileListener): NodeIKernelProfileListener -} - -export class ProfileListener implements IProfileListener { - onUserDetailInfoChanged(arg: UserDetailInfoListenerArg): void { - - } - onProfileSimpleChanged(...args: unknown[]) { - - } - - onProfileDetailInfoChanged(profile: User) { - - } - - onStatusUpdate(...args: unknown[]) { - - } - - onSelfStatusChanged(...args: unknown[]) { - - } - - onStrangerRemarkChanged(...args: unknown[]) { - - } } \ No newline at end of file diff --git a/src/ntqqapi/services/NodeIKernelProfileLikeService.ts b/src/ntqqapi/services/NodeIKernelProfileLikeService.ts index 919ec20..52835f9 100644 --- a/src/ntqqapi/services/NodeIKernelProfileLikeService.ts +++ b/src/ntqqapi/services/NodeIKernelProfileLikeService.ts @@ -9,10 +9,10 @@ export interface NodeIKernelProfileLikeService { setBuddyProfileLike(...args: unknown[]): { result: number, errMsg: string, succCounts: number } getBuddyProfileLike(req: BuddyProfileLikeReq): Promise, - 'friendMaxVotes': number, - 'start': number + info: { + userLikeInfos: Array, + friendMaxVotes: number, + start: number } }> diff --git a/src/onebot11/action/go-cqhttp/MarkMsgAsRead.ts b/src/onebot11/action/go-cqhttp/MarkMsgAsRead.ts index ba8f3d6..fef4d2d 100644 --- a/src/onebot11/action/go-cqhttp/MarkMsgAsRead.ts +++ b/src/onebot11/action/go-cqhttp/MarkMsgAsRead.ts @@ -8,7 +8,7 @@ interface Payload { export class MarkMsgAsRead extends BaseAction { actionName = ActionName.GoCQHTTP_MarkMsgAsRead - protected async _handle(payload: Payload): Promise { + protected async _handle() { return null } } diff --git a/src/onebot11/action/group/GetGuildList.ts b/src/onebot11/action/group/GetGuildList.ts index b9d2e9d..8ef385e 100644 --- a/src/onebot11/action/group/GetGuildList.ts +++ b/src/onebot11/action/group/GetGuildList.ts @@ -4,7 +4,7 @@ import { ActionName } from '../types' export default class GetGuildList extends BaseAction { actionName = ActionName.GetGuildList - protected async _handle(payload: null): Promise { + protected async _handle() { return null } } diff --git a/src/onebot11/action/group/SetGroupLeave.ts b/src/onebot11/action/group/SetGroupLeave.ts index 9fbfc2b..cf0b7be 100644 --- a/src/onebot11/action/group/SetGroupLeave.ts +++ b/src/onebot11/action/group/SetGroupLeave.ts @@ -6,10 +6,10 @@ interface Payload { is_dismiss: boolean } -export default class SetGroupLeave extends BaseAction { +export default class SetGroupLeave extends BaseAction { actionName = ActionName.SetGroupLeave - protected async _handle(payload: Payload): Promise { + protected async _handle(payload: Payload) { try { await this.ctx.ntGroupApi.quitGroup(payload.group_id.toString()) } catch (e) { diff --git a/src/onebot11/action/llonebot/Config.ts b/src/onebot11/action/llonebot/Config.ts index 6400103..ce663a6 100644 --- a/src/onebot11/action/llonebot/Config.ts +++ b/src/onebot11/action/llonebot/Config.ts @@ -5,13 +5,15 @@ import { getConfigUtil } from '@/common/config' export class GetConfigAction extends BaseAction { actionName = ActionName.GetConfig - protected async _handle(payload: null): Promise { + + protected async _handle(): Promise { return getConfigUtil().getConfig() } } export class SetConfigAction extends BaseAction { actionName = ActionName.SetConfig + protected async _handle(payload: Config): Promise { getConfigUtil().setConfig(payload) } diff --git a/src/onebot11/action/llonebot/GetEvent.ts b/src/onebot11/action/llonebot/GetEvent.ts index 64f8a60..808d8cd 100644 --- a/src/onebot11/action/llonebot/GetEvent.ts +++ b/src/onebot11/action/llonebot/GetEvent.ts @@ -13,13 +13,14 @@ interface Payload { export default class GetEvent extends BaseAction { actionName = ActionName.GetEvent + protected async _handle(payload: Payload): Promise { let key = '' if (payload.key) { key = payload.key } - let timeout = parseInt(payload.timeout?.toString()) || 0 - let evts = await getHttpEvent(key, timeout) + const timeout = parseInt(payload.timeout?.toString()) || 0 + const evts = await getHttpEvent(key, timeout) return evts } } diff --git a/src/onebot11/action/system/CanSendRecord.ts b/src/onebot11/action/system/CanSendRecord.ts index e23193d..d75f35f 100644 --- a/src/onebot11/action/system/CanSendRecord.ts +++ b/src/onebot11/action/system/CanSendRecord.ts @@ -8,7 +8,7 @@ interface ReturnType { export default class CanSendRecord extends BaseAction { actionName = ActionName.CanSendRecord - protected async _handle(payload: void): Promise { + protected async _handle() { return { yes: true, } diff --git a/src/onebot11/action/system/GetVersionInfo.ts b/src/onebot11/action/system/GetVersionInfo.ts index 88754a8..cc818b0 100644 --- a/src/onebot11/action/system/GetVersionInfo.ts +++ b/src/onebot11/action/system/GetVersionInfo.ts @@ -3,10 +3,10 @@ import { OB11Version } from '../../types' import { ActionName } from '../types' import { version } from '../../../version' -export default class GetVersionInfo extends BaseAction { +export default class GetVersionInfo extends BaseAction { actionName = ActionName.GetVersionInfo - protected async _handle(payload: any): Promise { + protected async _handle(): Promise { return { app_name: 'LLOneBot', protocol_version: 'v11', diff --git a/src/onebot11/helper/eventForHttp.ts b/src/onebot11/helper/eventForHttp.ts index dc00f2e..a5adbb6 100644 --- a/src/onebot11/helper/eventForHttp.ts +++ b/src/onebot11/helper/eventForHttp.ts @@ -4,13 +4,13 @@ import { OB11BaseEvent } from '../event/OB11BaseEvent' type PostEventType = OB11Message | OB11BaseEvent interface HttpEventType { - seq: number - event: PostEventType + seq: number + event: PostEventType } interface HttpUserType { - lastAccessTime: number - userSeq: number + lastAccessTime: number + userSeq: number } let curentSeq: number = 0 @@ -18,51 +18,51 @@ const eventList: HttpEventType[] = [] const httpUser: Record = {} export function postHttpEvent(event: PostEventType) { - curentSeq += 1 - eventList.push({ - seq: curentSeq, - event: event - }) - while (eventList.length > 100) { - eventList.shift() - } + curentSeq += 1 + eventList.push({ + seq: curentSeq, + event: event + }) + while (eventList.length > 100) { + eventList.shift() + } } export async function getHttpEvent(userKey: string, timeout = 0) { - const toRetEvent: PostEventType[] = [] + const toRetEvent: PostEventType[] = [] - // 清除过时的user,5分钟没访问过的user将被删除 - const now = Date.now() - for (let key in httpUser) { - let user = httpUser[key] - if (now - user.lastAccessTime > 1000 * 60 * 5) { - delete httpUser[key] - } + // 清除过时的user,5分钟没访问过的user将被删除 + const now = Date.now() + for (const key in httpUser) { + const user = httpUser[key] + if (now - user.lastAccessTime > 1000 * 60 * 5) { + delete httpUser[key] } + } - // 增加新的user - if (!httpUser[userKey]) { - httpUser[userKey] = { - lastAccessTime: now, - userSeq: curentSeq - } + // 增加新的user + if (!httpUser[userKey]) { + httpUser[userKey] = { + lastAccessTime: now, + userSeq: curentSeq } + } - const user = httpUser[userKey] - // 等待数据到来,暂时先这么写吧...... - while (curentSeq == user.userSeq && Date.now() - now < timeout) { - await new Promise(resolve => setTimeout(resolve, 10)) - } - // 取数据 - for (let i = 0; i < eventList.length; i++) { - let evt = eventList[i] - if (evt.seq > user.userSeq) { - toRetEvent.push(evt.event) - } + const user = httpUser[userKey] + // 等待数据到来,暂时先这么写吧...... + while (curentSeq == user.userSeq && Date.now() - now < timeout) { + await new Promise(resolve => setTimeout(resolve, 10)) + } + // 取数据 + for (let i = 0; i < eventList.length; i++) { + const evt = eventList[i] + if (evt.seq > user.userSeq) { + toRetEvent.push(evt.event) } + } - // 更新user数据 - user.lastAccessTime = Date.now() - user.userSeq = curentSeq - return toRetEvent + // 更新user数据 + user.lastAccessTime = Date.now() + user.userSeq = curentSeq + return toRetEvent }