From 7c20ca9b6493013bff803d2ffd0897811c63faca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Sun, 25 Aug 2024 11:45:50 +0800 Subject: [PATCH] style:lint --- src/common/utils/file.ts | 22 ++++++------ src/common/utils/helper.ts | 22 +++++++++++- src/core/apis/friend.ts | 11 ++++-- src/core/apis/user.ts | 10 +++--- src/core/entities/msg.ts | 37 +------------------- src/core/services/NodeIKernelGroupService.ts | 2 +- src/core/services/NodeIKernelMsgService.ts | 12 +++---- src/core/wrapper/wrapper.ts | 6 ++-- src/onebot/action/file/GetFile.ts | 2 +- src/onebot/action/go-cqhttp/SetQQProfile.ts | 2 +- src/onebot/action/msg/SendMsg/index.ts | 2 +- 11 files changed, 58 insertions(+), 70 deletions(-) diff --git a/src/common/utils/file.ts b/src/common/utils/file.ts index 8fa6e580..31d494ae 100644 --- a/src/common/utils/file.ts +++ b/src/common/utils/file.ts @@ -4,6 +4,7 @@ import crypto, { randomUUID } from 'crypto'; import util from 'util'; import path from 'node:path'; import * as fileType from 'file-type'; +import { solveAsyncProblem, solveProblem } from './helper'; export function isGIF(path: string) { const buffer = Buffer.alloc(4); @@ -185,12 +186,11 @@ export enum FileUriType { } export async function checkUriType(Uri: string) { - //先判断是否是本地文件 - try { - if (fs.existsSync(Uri)) return { Uri: Uri, Type: FileUriType.Local }; - } catch (error) { - } - try { + + const LocalFileRet = await solveProblem((Uri) => { if (fs.existsSync(Uri)) return { Uri: Uri, Type: FileUriType.Local }; }); + if (LocalFileRet) return LocalFileRet; + + const OtherFileRet = await solveProblem((Uri) => { //再判断是否是Http if (Uri.startsWith('http://') || Uri.startsWith('https://')) { return { Uri: Uri, Type: FileUriType.Remote }; @@ -200,10 +200,9 @@ export async function checkUriType(Uri: string) { return { Uri: Uri, Type: FileUriType.Base64 }; } if (Uri.startsWith('file://')) { - let pathname: string; let filePath: string; // await fs.copyFile(url.pathname, filePath); - pathname = decodeURIComponent(new URL(Uri).pathname); + const pathname = decodeURIComponent(new URL(Uri).pathname); if (process.platform === 'win32') { filePath = pathname.slice(1); } else { @@ -211,8 +210,9 @@ export async function checkUriType(Uri: string) { } return { Uri: filePath, Type: FileUriType.Local }; } - } catch (error) { - } + }); + if (OtherFileRet) return OtherFileRet; + return { Uri: Uri, Type: FileUriType.Unknown }; } @@ -232,7 +232,7 @@ export async function uri2local(dir: string, uri: string, filename: string | und //接下来都要有文件名 if (!filename) filename = randomUUID(); //解析Http和Https协议 - + if (UriType == FileUriType.Remote) { const pathInfo = path.parse(decodeURIComponent(new URL(HandledUri).pathname)); if (pathInfo.name) { diff --git a/src/common/utils/helper.ts b/src/common/utils/helper.ts index e1029206..dd3720f9 100644 --- a/src/common/utils/helper.ts +++ b/src/common/utils/helper.ts @@ -3,8 +3,28 @@ import fs from 'fs'; import os from 'node:os'; import { QQLevel } from '@/core'; -//下面这个类是用于将uid+msgid合并的类 +export async function solveProblem any>(func: T): Promise | undefined> { + return new Promise | undefined>(async (resolve) => { + try { + const result = func(); + resolve(result); + } catch (e) { + resolve(undefined); + } + }); +} +export async function solveAsyncProblem Promise>(func: T): Promise> | undefined> { + return new Promise> | undefined>(async (resolve) => { + try { + const result = await func(); + resolve(result); + } catch (e) { + resolve(undefined); + } + }); +} +//下面这个类是用于将uid+msgid合并的类 export class UUIDConverter { static encode(highStr: string, lowStr: string): string { const high = BigInt(highStr); diff --git a/src/core/apis/friend.ts b/src/core/apis/friend.ts index 6949b92a..67be161c 100644 --- a/src/core/apis/friend.ts +++ b/src/core/apis/friend.ts @@ -74,9 +74,14 @@ export class NTQQFriendApi { return this.context.session.getBuddyService().clearBuddyReqUnreadCnt(); } async getBuddyReq() { - const [, ret] = await this.core.eventWrapper.CallNormalEventV2 - - ('NodeIKernelBuddyService/getBuddyReq', 'NodeIKernelBuddyListener/onBuddyReqChange', 1, 5000); + const [, ret] = await this.core.eventWrapper.CallNormalEventV2< + NodeIKernelBuddyService['getBuddyReq'], + NodeIKernelBuddyListener['onBuddyReqChange'] + >( + 'NodeIKernelBuddyService/getBuddyReq', + 'NodeIKernelBuddyListener/onBuddyReqChange', + 1, + 5000); return ret; } diff --git a/src/core/apis/user.ts b/src/core/apis/user.ts index 5132891e..d0cb8955 100644 --- a/src/core/apis/user.ts +++ b/src/core/apis/user.ts @@ -3,6 +3,7 @@ import { NodeIKernelProfileListener } from '@/core/listeners'; import { RequestUtil } from '@/common/utils/request'; import { NodeIKernelProfileService, ProfileBizType, UserDetailSource } from '@/core/services'; import { InstanceContext, NapCatCore } from '..'; +import { solveAsyncProblem } from '@/common/utils/helper'; export class NTQQUserApi { context: InstanceContext; @@ -128,12 +129,9 @@ export class NTQQUserApi { } async getUserDetailInfo(uid: string): Promise { - try { - const retUser = await this.fetchUserDetailInfo(uid, UserDetailSource.KDB); - if (retUser.uin !== '0') { - return retUser; - } - } catch (e) { + const retUser = await solveAsyncProblem(async (uid) => this.fetchUserDetailInfo(uid, UserDetailSource.KDB)); + if (retUser && retUser.uin !== '0') { + return retUser; } this.context.logger.logDebug('[NapCat] [Mark] getUserDetailInfo Mode1 Failed.'); return this.fetchUserDetailInfo(uid, UserDetailSource.KSERVER); diff --git a/src/core/entities/msg.ts b/src/core/entities/msg.ts index 1c1ed936..cdfdd538 100644 --- a/src/core/entities/msg.ts +++ b/src/core/entities/msg.ts @@ -848,41 +848,6 @@ export interface MultiForwardMsgElement { resId: string; fileName: string; } -export enum NTSubMsgType { - KMSGSUBTYPEARKGROUPANNOUNCE = 3, - KMSGSUBTYPEARKGROUPANNOUNCECONFIRMREQUIRED = 4, - KMSGSUBTYPEARKGROUPGIFTATME = 5, - KMSGSUBTYPEARKGROUPTASKATALL = 6, - KMSGSUBTYPEARKMULTIMSG = 7, - KMSGSUBTYPEARKNORMAL = 0, - KMSGSUBTYPEARKTENCENTDOCFROMMINIAPP = 1, - KMSGSUBTYPEARKTENCENTDOCFROMPLUSPANEL = 2, - KMSGSUBTYPEEMOTICON = 15, - KMSGSUBTYPEFILEAPP = 11, - KMSGSUBTYPEFILEAUDIO = 3, - KMSGSUBTYPEFILEDOC = 4, - KMSGSUBTYPEFILEEXCEL = 6, - KMSGSUBTYPEFILEFOLDER = 13, - KMSGSUBTYPEFILEHTML = 10, - KMSGSUBTYPEFILEIPA = 14, - KMSGSUBTYPEFILENORMAL = 0, - KMSGSUBTYPEFILEPDF = 7, - KMSGSUBTYPEFILEPIC = 1, - KMSGSUBTYPEFILEPPT = 5, - KMSGSUBTYPEFILEPSD = 12, - KMSGSUBTYPEFILETXT = 8, - KMSGSUBTYPEFILEVIDEO = 2, - KMSGSUBTYPEFILEZIP = 9, - KMSGSUBTYPELINK = 5, - KMSGSUBTYPEMARKETFACE = 1, - KMSGSUBTYPEMIXEMOTICON = 7, - KMSGSUBTYPEMIXFACE = 3, - KMSGSUBTYPEMIXMARKETFACE = 2, - KMSGSUBTYPEMIXPIC = 1, - KMSGSUBTYPEMIXREPLY = 4, - KMSGSUBTYPEMIXTEXT = 0, - KMSGSUBTYPETENCENTDOC = 6 -} export enum SendStatusType { KSEND_STATUS_FAILED = 0, KSEND_STATUS_SENDING = 1, @@ -914,7 +879,7 @@ export interface RawMessage { msgType: NTMsgType; - subMsgType: NTSubMsgType; + subMsgType: number; senderUid: string; diff --git a/src/core/services/NodeIKernelGroupService.ts b/src/core/services/NodeIKernelGroupService.ts index 118dda10..9a90d9f0 100644 --- a/src/core/services/NodeIKernelGroupService.ts +++ b/src/core/services/NodeIKernelGroupService.ts @@ -100,7 +100,7 @@ export interface NodeIKernelGroupService { uid: string, index: number//0 }>, - infos: {}, + infos: unknown, finish: true, hasRobot: false } diff --git a/src/core/services/NodeIKernelMsgService.ts b/src/core/services/NodeIKernelMsgService.ts index 44756bfb..1f0ceb74 100644 --- a/src/core/services/NodeIKernelMsgService.ts +++ b/src/core/services/NodeIKernelMsgService.ts @@ -37,7 +37,7 @@ export interface NodeIKernelMsgService { recallMsg(peer: Peer, msgIds: string[]): Promise; - addKernelMsgImportToolListener(arg: Object): unknown; + addKernelMsgImportToolListener(arg: unknown): unknown; removeKernelMsgListener(args: unknown): unknown; @@ -51,7 +51,7 @@ export interface NodeIKernelMsgService { getOnLineDev(): void; - kickOffLine(DevInfo: Object): unknown; + kickOffLine(DevInfo: unknown): unknown; setStatus(args: { status: number, extStatus: number, batteryStatus: number }): Promise; @@ -80,11 +80,11 @@ export interface NodeIKernelMsgService { // this.voipToken = bArr2; // this.profileId = str; - setToken(arg: Object): unknown; + setToken(arg: unknown): unknown; switchForeGround(): unknown; - switchBackGround(arg: Object): unknown; + switchBackGround(arg: unknown): unknown; //hex setTokenForMqq(token: string): unknown; @@ -384,7 +384,7 @@ export interface NodeIKernelMsgService { getFileThumbSavePath(...args: unknown[]): unknown; //猜测居多 - translatePtt2Text(MsgId: string, Peer: {}, MsgElement: {}): unknown; + translatePtt2Text(MsgId: string, Peer: Peer, MsgElement: unknown): unknown; setPttPlayedState(...args: unknown[]): unknown; @@ -668,7 +668,7 @@ export interface NodeIKernelMsgService { recordEmoji(...args: unknown[]): unknown; - fetchGetHitEmotionsByWord(args: Object): Promise;//表情推荐? + fetchGetHitEmotionsByWord(args: unknown): Promise;//表情推荐? deleteAllRoamMsgs(...args: unknown[]): unknown;//漫游消息? diff --git a/src/core/wrapper/wrapper.ts b/src/core/wrapper/wrapper.ts index 743d8941..95bd0c1f 100644 --- a/src/core/wrapper/wrapper.ts +++ b/src/core/wrapper/wrapper.ts @@ -69,9 +69,9 @@ export interface NodeQQNTWrapperUtil { genFileShaAndMd5Hex(path: string, unknown: number): unknown; //可能是错的 - setTraceInfo(unknown: Object): unknown; + setTraceInfo(unknown: unknown): unknown; - encodeOffLine(unknown: Object): unknown; + encodeOffLine(unknown: unknown): unknown; decodeOffLine(arg: string): unknown; //可能是错的 传递hex @@ -89,7 +89,7 @@ export interface NodeQQNTWrapperUtil { runProcessArgs(arg0: string, arg1: { [key: string]: string }, arg2: boolean): unknown; - calcThumbSize(arg0: number, arg1: number, arg2: Object): unknown; + calcThumbSize(arg0: number, arg1: number, arg2: unknown): unknown; fullWordToHalfWord(arg0: string): unknown; diff --git a/src/onebot/action/file/GetFile.ts b/src/onebot/action/file/GetFile.ts index 428b648a..52b8fe53 100644 --- a/src/onebot/action/file/GetFile.ts +++ b/src/onebot/action/file/GetFile.ts @@ -90,7 +90,7 @@ export class GetFileBase extends BaseAction { return res; } } catch { - + this.CoreContext.context.logger.logDebug('GetFileBase Mode - 1 Error'); } const NTSearchNameResult = (await NTQQFileApi.searchfile([payload.file])).resultItems; diff --git a/src/onebot/action/go-cqhttp/SetQQProfile.ts b/src/onebot/action/go-cqhttp/SetQQProfile.ts index 73334115..27c0089c 100644 --- a/src/onebot/action/go-cqhttp/SetQQProfile.ts +++ b/src/onebot/action/go-cqhttp/SetQQProfile.ts @@ -24,7 +24,7 @@ export class SetQQProfile extends BaseAction { const OldProfile = await NTQQUserApi.getUserDetailInfo(self.uid); const ret = await NTQQUserApi.modifySelfProfile({ nick: payload.nickname, - longNick: payload?.personal_note ?? OldProfile?.longNick!, + longNick: (payload?.personal_note ?? OldProfile?.longNick) || '', sex: parseInt(payload?.sex ? payload?.sex.toString() : OldProfile?.sex!.toString()), birthday: { birthday_year: OldProfile?.birthday_year!.toString(), birthday_month: OldProfile?.birthday_month!.toString(), birthday_day: OldProfile?.birthday_day!.toString() }, location: undefined, diff --git a/src/onebot/action/msg/SendMsg/index.ts b/src/onebot/action/msg/SendMsg/index.ts index 7486c23f..f0e5a662 100644 --- a/src/onebot/action/msg/SendMsg/index.ts +++ b/src/onebot/action/msg/SendMsg/index.ts @@ -158,7 +158,7 @@ export class SendMsg extends BaseAction { if (payload.user_id && payload.message_type !== 'group') { const uid = await NTQQUserApi.getUidByUinV2(payload.user_id.toString()); const isBuddy = await NTQQFriendApi.isBuddy(uid!); - if (!isBuddy) { } + //if (!isBuddy) { } } return { valid: true }; }