Skip to content

Commit

Permalink
Merge branch 'main' into enchant
Browse files Browse the repository at this point in the history
  • Loading branch information
h5mcbox committed Jun 19, 2024
2 parents 941c4b6 + 876b29d commit 32b4278
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
35 changes: 34 additions & 1 deletion src/common/utils/file.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import fsPromise from 'fs/promises';
import fsPromise, { stat } from 'fs/promises';
import crypto from 'crypto';
import util from 'util';
import path from 'node:path';
Expand Down Expand Up @@ -50,7 +50,40 @@ export function checkFileReceived(path: string, timeout: number = 3000): Promise
check();
});
}
// 定义一个异步函数来检查文件是否存在
export async function checkFileReceived2(path: string, timeout: number = 3000): Promise<void> {
// 使用 Promise.race 来同时进行文件状态检查和超时计时
// Promise.race 会返回第一个解决(resolve)或拒绝(reject)的 Promise
await Promise.race([
checkFile(path),
timeoutPromise(timeout, `文件不存在: ${path}`),
]);
}

// 转换超时时间至 Promise
function timeoutPromise(timeout: number, errorMsg: string): Promise<void> {
return new Promise((_, reject) => {
setTimeout(() => {
reject(new Error(errorMsg));
}, timeout);
});
}

// 异步检查文件是否存在
async function checkFile(path: string): Promise<void> {
try {
await stat(path);
} catch (error: any) {
if (error.code === 'ENOENT') {
// 如果文件不存在,则抛出一个错误
throw new Error(`文件不存在: ${path}`);
} else {
// 对于 stat 调用的其他错误,重新抛出
throw error;
}
}
// 如果文件存在,则无需做任何事情,Promise 解决(resolve)自身
}
export async function file2base64(path: string) {
const readFile = util.promisify(fs.readFile);
const result = {
Expand Down
2 changes: 1 addition & 1 deletion src/core
Submodule core updated from 12a3fc to cd5ac2
7 changes: 6 additions & 1 deletion src/onebot11/action/msg/SendMsg/create-send-elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
NTQQFileApi,
SendArkElement,
SendMessageElement,
SendMsgElementConstructor
SendMsgElementConstructor,
SignMusicWrapper
} from '@/core';
import { getGroupMember } from '@/core/data';
import { dbUtil } from '@/common/utils/db';
Expand Down Expand Up @@ -185,6 +186,10 @@ const _handlers: {

const signUrl = ob11Config.musicSignUrl;
if (!signUrl) {
if (data.type === 'qq') {
const musicJson = (await SignMusicWrapper(data.id.toString())).data.arkResult.slice(0, -1);
return SendMsgElementConstructor.ark(musicJson);
}
throw Error('音乐消息签名地址未配置');
}
try {
Expand Down

0 comments on commit 32b4278

Please sign in to comment.