style: 异步实现

This commit is contained in:
手瓜一十雪
2024-12-02 11:44:37 +08:00
parent 59ddb84cb1
commit 0a7f518cc4
8 changed files with 42 additions and 76 deletions

View File

@@ -1,6 +1,6 @@
import { OneBotAction } from '@/onebot/action/OneBotAction';
import { ActionName } from '@/onebot/action/router';
import * as fs from 'node:fs';
import fs from 'node:fs/promises';
import { checkFileExist, uri2local } from '@/common/file';
import { Static, Type } from '@sinclair/typebox';
@@ -21,9 +21,7 @@ export default class SetAvatar extends OneBotAction<Payload, null> {
if (path) {
await checkFileExist(path, 5000);// 避免崩溃
const ret = await this.core.apis.UserApi.setQQAvatar(path);
fs.unlink(path, () => {
});
fs.unlink(path).catch(() => { });
if (!ret) {
throw new Error(`头像${payload.file}设置失败,api无返回`);
}
@@ -34,7 +32,7 @@ export default class SetAvatar extends OneBotAction<Payload, null> {
throw new Error(`头像${payload.file}设置失败,未知的错误,${ret.result}:${ret.errMsg}`);
}
} else {
fs.unlink(path, () => { });
fs.unlink(path).catch(() => { });
throw new Error(`头像${payload.file}设置失败,无法获取头像,文件可能不存在`);
}
return null;

View File

@@ -1,7 +1,7 @@
import { checkFileExist, uri2local } from '@/common/file';
import { OneBotAction } from '@/onebot/action/OneBotAction';
import { ActionName } from '@/onebot/action/router';
import { unlink } from 'node:fs';
import { unlink } from 'node:fs/promises';
import { Static, Type } from '@sinclair/typebox';
const SchemaData = Type.Object({
@@ -41,8 +41,7 @@ export class SendGroupNotice extends OneBotAction<Payload, null> {
throw new Error(`群公告${payload.image}设置失败,图片上传失败`);
}
unlink(path, () => {
});
unlink(path).catch(() => { });
UploadImage = ImageUploadResult.picInfo;
}

View File

@@ -1,9 +1,8 @@
import { OneBotAction } from '@/onebot/action/OneBotAction';
import { ActionName, BaseCheckResult } from '@/onebot/action/router';
import * as fs from 'node:fs';
import { ActionName } from '@/onebot/action/router';
import { checkFileExistV2, uri2local } from '@/common/file';
import { Static, Type } from '@sinclair/typebox';
import fs from 'node:fs/promises';
const SchemaData = Type.Object({
file: Type.String(),
group_id: Type.Union([Type.Number(), Type.String()])
@@ -14,7 +13,7 @@ type Payload = Static<typeof SchemaData>;
export default class SetGroupPortrait extends OneBotAction<Payload, any> {
actionName = ActionName.SetGroupPortrait;
payloadSchema = SchemaData;
async _handle(payload: Payload): Promise<any> {
const { path, success } = (await uri2local(this.core.NapCatTempPath, payload.file));
if (!success) {
@@ -23,7 +22,7 @@ export default class SetGroupPortrait extends OneBotAction<Payload, any> {
if (path) {
await checkFileExistV2(path, 5000); // 文件不存在QQ会崩溃需要提前判断
const ret = await this.core.apis.GroupApi.setGroupAvatar(payload.group_id.toString(), path);
fs.unlink(path, () => { });
fs.unlink(path).catch(() => { });
if (!ret) {
throw new Error(`头像${payload.file}设置失败,api无返回`);
}
@@ -34,7 +33,7 @@ export default class SetGroupPortrait extends OneBotAction<Payload, any> {
}
return ret;
} else {
fs.unlink(path, () => { });
fs.unlink(path).catch(() => { });
throw new Error(`头像${payload.file}设置失败,无法获取头像,文件可能不存在`);
}
}

View File

@@ -15,7 +15,6 @@ import {
RawMessage,
SendMessageElement,
SendTextElement,
BaseEmojiType,
FaceType,
GrayTipElement,
} from '@/core';
@@ -26,12 +25,9 @@ import { EventType } from '@/onebot/event/OneBotEvent';
import { encodeCQCode } from '@/onebot/helper/cqcode';
import { uri2local } from '@/common/file';
import { RequestUtil } from '@/common/request';
import fs from 'node:fs';
import fsPromise from 'node:fs/promises';
import fsPromise, { constants } from 'node:fs/promises';
import { OB11FriendAddNoticeEvent } from '@/onebot/event/notice/OB11FriendAddNoticeEvent';
// import { decodeSysMessage } from '@/core/packet/proto/old/ProfileLike';
import { ForwardMsgBuilder } from "@/common/forward-msg-builder";
import { decodeSysMessage } from "@/core/helper/adaptDecoder";
import { GroupChange, PushMsgBody } from "@/core/packet/transformer/proto";
import { NapProtoMsg } from '@napneko/nap-proto-core';
import { OB11GroupIncreaseEvent } from '../event/notice/OB11GroupIncreaseEvent';
@@ -887,16 +883,16 @@ export class OneBotMsgApi {
try {
for (const fileElement of sendElements) {
if (fileElement.elementType === ElementType.PTT) {
totalSize += fs.statSync(fileElement.pttElement.filePath).size;
totalSize += (await fsPromise.stat(fileElement.pttElement.filePath)).size;
}
if (fileElement.elementType === ElementType.FILE) {
totalSize += fs.statSync(fileElement.fileElement.filePath).size;
totalSize += (await fsPromise.stat(fileElement.fileElement.filePath)).size;
}
if (fileElement.elementType === ElementType.VIDEO) {
totalSize += fs.statSync(fileElement.videoElement.filePath).size;
totalSize += (await fsPromise.stat(fileElement.videoElement.filePath)).size;
}
if (fileElement.elementType === ElementType.PIC) {
totalSize += fs.statSync(fileElement.picElement.sourcePath).size;
totalSize += (await fsPromise.stat(fileElement.picElement.sourcePath)).size;
}
}
//且 PredictTime ((totalSize / 1024 / 512) * 1000)不等于Nan
@@ -916,9 +912,9 @@ export class OneBotMsgApi {
}, returnMsg.msgId);
setTimeout(() => {
deleteAfterSentFiles.forEach(file => {
deleteAfterSentFiles.forEach(async file => {
try {
if (fs.existsSync(file)) {
if (await fsPromise.access(file, constants.W_OK).then(() => true).catch(() => false)) {
fsPromise.unlink(file).then().catch(e => this.core.context.logger.logError('发送消息删除文件失败', e));
}
} catch (error) {