mirror of
https://github.com/Jerryplusy/rc-plugin.git
synced 2025-10-14 16:19:18 +00:00
✨ feat: 增加错误处理和日志记录功能
在 switchers.js 中,为 setOversea 和 setLagrange 方法添加了 try-catch 错误处理结构,并在捕获到错误时向用户发送错误消息。同时,对 autoclearTrash 函数进行了重构,使其成为异步函数,并添加了错误日志记录。 此外,对 file.js 中的日志记录语句进行了格式调整,以提高代码的可读性和一致性。 ```
This commit is contained in:
parent
61bb052f68
commit
5fcdc95b1a
@ -2,12 +2,12 @@ import config from "../model/config.js";
|
|||||||
import schedule from 'node-schedule';
|
import schedule from 'node-schedule';
|
||||||
import { REDIS_YUNZAI_ISOVERSEA, REDIS_YUNZAI_LAGRANGE, REDIS_YUNZAI_WHITELIST } from "../constants/constant.js";
|
import { REDIS_YUNZAI_ISOVERSEA, REDIS_YUNZAI_LAGRANGE, REDIS_YUNZAI_WHITELIST } from "../constants/constant.js";
|
||||||
import { deleteFolderRecursive, readCurrentDir } from "../utils/file.js";
|
import { deleteFolderRecursive, readCurrentDir } from "../utils/file.js";
|
||||||
import { redisExistAndGetKey, redisExistKey, redisGetKey, redisSetKey } from "../utils/redis-util.js";
|
import { redisExistAndGetKey, redisGetKey, redisSetKey } from "../utils/redis-util.js";
|
||||||
|
|
||||||
//自动清理定时
|
// 自动清理定时
|
||||||
const autotime = config.getConfig("tools").autoclearTrashtime
|
const autotime = config.getConfig("tools").autoclearTrashtime;
|
||||||
// 视频保存路径
|
// 视频保存路径
|
||||||
const defaultPath = config.getConfig("tools").defaultPath
|
const defaultPath = config.getConfig("tools").defaultPath;
|
||||||
|
|
||||||
export class switchers extends plugin {
|
export class switchers extends plugin {
|
||||||
constructor() {
|
constructor() {
|
||||||
@ -61,22 +61,23 @@ export class switchers extends plugin {
|
|||||||
* @returns {Promise<boolean>}
|
* @returns {Promise<boolean>}
|
||||||
*/
|
*/
|
||||||
async setOversea(e) {
|
async setOversea(e) {
|
||||||
// 查看当前设置
|
try {
|
||||||
let os = (await redisGetKey(REDIS_YUNZAI_ISOVERSEA))?.os;
|
// 查看当前设置
|
||||||
// 如果是第一次
|
let os = (await redisGetKey(REDIS_YUNZAI_ISOVERSEA))?.os;
|
||||||
if (os === undefined) {
|
// 如果是第一次
|
||||||
await redisSetKey(REDIS_YUNZAI_ISOVERSEA, {
|
if (os === undefined) {
|
||||||
os: false,
|
await redisSetKey(REDIS_YUNZAI_ISOVERSEA, { os: false });
|
||||||
});
|
os = false;
|
||||||
os = false;
|
}
|
||||||
|
// 设置
|
||||||
|
os = ~os;
|
||||||
|
await redisSetKey(REDIS_YUNZAI_ISOVERSEA, { os });
|
||||||
|
e.reply(`当前服务器:${ os ? '海外服务器' : '国内服务器' }`);
|
||||||
|
return true;
|
||||||
|
} catch (err) {
|
||||||
|
e.reply(`设置海外模式时发生错误: ${ err.message }`);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
// 设置
|
|
||||||
os = ~os;
|
|
||||||
await redisSetKey(REDIS_YUNZAI_ISOVERSEA, {
|
|
||||||
os: os,
|
|
||||||
});
|
|
||||||
e.reply(`当前服务器:${ os ? '海外服务器' : '国内服务器' }`)
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -85,26 +86,27 @@ export class switchers extends plugin {
|
|||||||
* @returns {Promise<boolean>}
|
* @returns {Promise<boolean>}
|
||||||
*/
|
*/
|
||||||
async setLagrange(e) {
|
async setLagrange(e) {
|
||||||
// 查看当前设置
|
try {
|
||||||
let driver = (await redisGetKey(REDIS_YUNZAI_LAGRANGE))?.driver;
|
// 查看当前设置
|
||||||
// 如果是第一次
|
let driver = (await redisGetKey(REDIS_YUNZAI_LAGRANGE))?.driver;
|
||||||
if (driver === undefined) {
|
// 如果是第一次
|
||||||
await redisSetKey(REDIS_YUNZAI_LAGRANGE, {
|
if (driver === undefined) {
|
||||||
driver: 1,
|
await redisSetKey(REDIS_YUNZAI_LAGRANGE, { driver: 1 });
|
||||||
})
|
driver = 1;
|
||||||
driver = 1;
|
}
|
||||||
|
// 异常检测,之前算法出现问题,如果出现异常就检测纠正
|
||||||
|
if (driver === -1) {
|
||||||
|
driver = 1;
|
||||||
|
}
|
||||||
|
// 设置
|
||||||
|
driver ^= 1;
|
||||||
|
await redisSetKey(REDIS_YUNZAI_LAGRANGE, { driver });
|
||||||
|
e.reply(`当前驱动:${ driver ? '拉格朗日' : '其他驱动' }`);
|
||||||
|
return true;
|
||||||
|
} catch (err) {
|
||||||
|
e.reply(`设置拉格朗日时发生错误: ${ err.message }`);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
// 异常检测,之前算法出现问题,如果出现异常就检测纠正
|
|
||||||
if (driver === -1) {
|
|
||||||
driver = 1;
|
|
||||||
}
|
|
||||||
// 设置
|
|
||||||
driver ^= 1;
|
|
||||||
await redisSetKey(REDIS_YUNZAI_LAGRANGE, {
|
|
||||||
driver: driver,
|
|
||||||
})
|
|
||||||
e.reply(`当前驱动:${ driver ? '拉格朗日' : '其他驱动' }`)
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -129,33 +131,27 @@ export class switchers extends plugin {
|
|||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
async setWhiteList(e) {
|
async setWhiteList(e) {
|
||||||
let trustUserId;
|
try {
|
||||||
// 判断是不是回复用户命令
|
let trustUserId = e?.reply_id !== undefined ? (await e.getReply()).user_id : e.msg.replace("#设置R信任用户", "").trim();
|
||||||
if (e?.reply_id !== undefined) {
|
trustUserId = trustUserId.toString();
|
||||||
trustUserId = (await e.getReply()).user_id;
|
// 用户ID检测
|
||||||
} else {
|
if (!trustUserId) {
|
||||||
// 如果不是回复就看发送内容
|
e.reply("无效的R信任用户");
|
||||||
trustUserId = e.msg.replace("#设置R信任用户", "");
|
return;
|
||||||
|
}
|
||||||
|
let whiteList = await redisExistAndGetKey(REDIS_YUNZAI_WHITELIST) || [];
|
||||||
|
// 重复检测
|
||||||
|
if (whiteList.includes(trustUserId)) {
|
||||||
|
e.reply("R信任用户已存在,无须添加!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
whiteList.push(trustUserId);
|
||||||
|
// 放置到Redis里
|
||||||
|
await redisSetKey(REDIS_YUNZAI_WHITELIST, whiteList);
|
||||||
|
e.reply(`成功添加R信任用户:${ trustUserId }`);
|
||||||
|
} catch (err) {
|
||||||
|
e.reply(`设置R信任用户时发生错误: ${ err.message }`);
|
||||||
}
|
}
|
||||||
// 用户ID检测
|
|
||||||
if (trustUserId == null || trustUserId === "") {
|
|
||||||
e.reply("无效的R信任用户");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let whiteList = await redisExistAndGetKey(REDIS_YUNZAI_WHITELIST);
|
|
||||||
// 不存在就创建
|
|
||||||
if (whiteList == null) {
|
|
||||||
whiteList = [];
|
|
||||||
}
|
|
||||||
// 重复检测
|
|
||||||
if (whiteList.includes(trustUserId)) {
|
|
||||||
e.reply("R信任用户已存在,无须添加!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
whiteList = [...whiteList, trustUserId];
|
|
||||||
// 放置到Redis里
|
|
||||||
await redisSetKey(REDIS_YUNZAI_WHITELIST, whiteList);
|
|
||||||
e.reply(`成功添加R信任用户:${ trustUserId }`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -164,16 +160,17 @@ export class switchers extends plugin {
|
|||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
async getWhiteList(e) {
|
async getWhiteList(e) {
|
||||||
let whiteList = await redisExistAndGetKey(REDIS_YUNZAI_WHITELIST);
|
try {
|
||||||
if (whiteList == null) {
|
let whiteList = await redisExistAndGetKey(REDIS_YUNZAI_WHITELIST) || [];
|
||||||
whiteList = [];
|
const message = `R信任用户列表:\n${ whiteList.join(",\n") }`;
|
||||||
}
|
if (this.e.isGroup) {
|
||||||
const message = `R信任用户列表:${ whiteList.join(",\n") }`;
|
await Bot.pickUser(this.e.user_id).sendMsg(await this.e.runtime.common.makeForwardMsg(this.e, message));
|
||||||
if (this.e.isGroup) {
|
await this.reply('R插件的信任用户名单已发送至您的私信了~');
|
||||||
await Bot.pickUser(this.e.user_id).sendMsg(await this.e.runtime.common.makeForwardMsg(this.e, message));
|
} else {
|
||||||
await this.reply('R插件的信任用户名单已发送至您的私信了~');
|
await e.reply(await makeForwardMsg(this.e, message));
|
||||||
} else {
|
}
|
||||||
await e.reply(await makeForwardMsg(this.e, message));
|
} catch (err) {
|
||||||
|
e.reply(`获取R信任用户时发生错误: ${ err.message }`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -183,64 +180,46 @@ export class switchers extends plugin {
|
|||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
async searchWhiteList(e) {
|
async searchWhiteList(e) {
|
||||||
let trustUserId;
|
try {
|
||||||
// 判断是不是回复用户命令
|
let trustUserId = e?.reply_id !== undefined ? (await e.getReply()).user_id : e.msg.replace("#查询R信任用户", "").trim();
|
||||||
if (e?.reply_id !== undefined) {
|
let whiteList = await redisExistAndGetKey(REDIS_YUNZAI_WHITELIST) || [];
|
||||||
trustUserId = (await e.getReply()).user_id;
|
const isInWhiteList = whiteList.includes(trustUserId);
|
||||||
} else {
|
e.reply(isInWhiteList ? `✅ ${ trustUserId }已经是R插件的信任用户哦~` : `⚠️ ${ trustUserId }不是R插件的信任用户哦~`);
|
||||||
// 如果不是回复就看发送内容
|
} catch (err) {
|
||||||
trustUserId = e.msg.replace("#设置R信任用户", "");
|
e.reply(`查询R信任用户时发生错误: ${ err.message }`);
|
||||||
}
|
}
|
||||||
let whiteList = await redisExistAndGetKey(REDIS_YUNZAI_WHITELIST);
|
|
||||||
if (whiteList == null) {
|
|
||||||
e.reply("R插件当前没有任何信任用户!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const isInWhiteList = whiteList.includes(trustUserId);
|
|
||||||
if (isInWhiteList) {
|
|
||||||
e.reply(`✅ ${trustUserId}已经是R插件的信任用户哦~`);
|
|
||||||
} else {
|
|
||||||
e.reply(`⚠️ ${trustUserId}不是R插件的信任用户哦~`);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除信任用户
|
||||||
|
* @param e
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
async deleteWhiteList(e) {
|
async deleteWhiteList(e) {
|
||||||
let trustUserId;
|
try {
|
||||||
// 判断是不是回复用户命令
|
let trustUserId = e?.reply_id !== undefined ? (await e.getReply()).user_id : e.msg.replace("#删除R信任用户", "").trim();
|
||||||
if (e?.reply_id !== undefined) {
|
// 校准不是string的用户
|
||||||
trustUserId = (await e.getReply()).user_id;
|
let whiteList = (await redisExistAndGetKey(REDIS_YUNZAI_WHITELIST))?.map(item => item.toString()) || [];
|
||||||
} else {
|
// 重复检测
|
||||||
// 如果不是回复就看发送内容
|
if (!whiteList.includes(trustUserId)) {
|
||||||
trustUserId = e.msg.replace("#删除R信任用户", "");
|
e.reply("R信任用户不存在,无须删除!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
whiteList = whiteList.filter(item => item !== trustUserId);
|
||||||
|
// 放置到Redis里
|
||||||
|
await redisSetKey(REDIS_YUNZAI_WHITELIST, whiteList);
|
||||||
|
e.reply(`成功删除R信任用户:${ trustUserId }`);
|
||||||
|
} catch (err) {
|
||||||
|
e.reply(`删除R信任用户时发生错误: ${ err.message }`);
|
||||||
}
|
}
|
||||||
// 校准不是string的用户
|
|
||||||
let whiteList = (await redisExistAndGetKey(REDIS_YUNZAI_WHITELIST)).map(item =>
|
|
||||||
typeof item === 'string' ? item : item.toString()
|
|
||||||
);
|
|
||||||
if (whiteList == null) {
|
|
||||||
e.reply("R插件当前没有任何信任用户:");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// 重复检测
|
|
||||||
if (!whiteList.includes(trustUserId)) {
|
|
||||||
e.reply("R信任用户不存在,无须删除!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
whiteList = whiteList.filter((item) => item !== trustUserId);
|
|
||||||
// 放置到Redis里
|
|
||||||
await redisSetKey(REDIS_YUNZAI_WHITELIST, whiteList);
|
|
||||||
e.reply(`成功删除R信任用户:${ trustUserId }`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 清理垃圾文件
|
* 清理垃圾文件
|
||||||
* @param e
|
* @returns {Promise<Object>}
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
*/
|
||||||
async function autoclearTrash(e) {
|
async function autoclearTrash() {
|
||||||
const dataDirectory = "./data/";
|
const dataDirectory = "./data/";
|
||||||
try {
|
try {
|
||||||
const files = await readCurrentDir(dataDirectory);
|
const files = await readCurrentDir(dataDirectory);
|
||||||
@ -252,10 +231,7 @@ async function autoclearTrash(e) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const rTempFileLen = await deleteFolderRecursive(defaultPath);
|
const rTempFileLen = await deleteFolderRecursive(defaultPath);
|
||||||
return {
|
return { dataClearFileLen, rTempFileLen };
|
||||||
dataClearFileLen,
|
|
||||||
rTempFileLen
|
|
||||||
};
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error(err);
|
logger.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
@ -272,8 +248,8 @@ function autoclear(time) {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(`自动清理垃圾时发生错误: ${ err.message }`);
|
console.error(`自动清理垃圾时发生错误: ${ err.message }`);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
//自动清理垃圾
|
// 自动清理垃圾
|
||||||
autoclear(autotime)
|
autoclear(autotime);
|
||||||
|
@ -14,7 +14,7 @@ const mimeTypes = {
|
|||||||
|
|
||||||
// 通用错误处理函数
|
// 通用错误处理函数
|
||||||
function handleError(err) {
|
function handleError(err) {
|
||||||
logger.error(`错误: ${err.message}\n堆栈: ${err.stack}`);
|
logger.error(`错误: ${ err.message }\n堆栈: ${ err.stack }`);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,7 +27,7 @@ export async function checkAndRemoveFile(file) {
|
|||||||
try {
|
try {
|
||||||
await fs.access(file);
|
await fs.access(file);
|
||||||
await fs.unlink(file);
|
await fs.unlink(file);
|
||||||
logger.info(`文件 ${file} 删除成功。`);
|
logger.info(`文件 ${ file } 删除成功。`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.code !== 'ENOENT') {
|
if (err.code !== 'ENOENT') {
|
||||||
handleError(err);
|
handleError(err);
|
||||||
@ -46,7 +46,7 @@ export async function mkdirIfNotExists(dir) {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.code === 'ENOENT') {
|
if (err.code === 'ENOENT') {
|
||||||
await fs.mkdir(dir, { recursive: true });
|
await fs.mkdir(dir, { recursive: true });
|
||||||
logger.info(`目录 ${dir} 创建成功。`);
|
logger.info(`目录 ${ dir } 创建成功。`);
|
||||||
} else {
|
} else {
|
||||||
handleError(err);
|
handleError(err);
|
||||||
}
|
}
|
||||||
@ -72,7 +72,7 @@ export async function deleteFolderRecursive(folderPath) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await Promise.allSettled(actions);
|
await Promise.allSettled(actions);
|
||||||
logger.info(`文件夹 ${folderPath} 中的所有文件删除成功。`);
|
logger.info(`文件夹 ${ folderPath } 中的所有文件删除成功。`);
|
||||||
return files.length;
|
return files.length;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
handleError(error);
|
handleError(error);
|
||||||
@ -109,7 +109,7 @@ export async function copyFiles(srcDir, destDir, specificFiles = []) {
|
|||||||
? files.filter(file => specificFiles.includes(file))
|
? files.filter(file => specificFiles.includes(file))
|
||||||
: files;
|
: files;
|
||||||
|
|
||||||
logger.info(`[R插件][拷贝文件] 正在将 ${srcDir} 的文件拷贝到 ${destDir} 中`);
|
logger.info(`[R插件][拷贝文件] 正在将 ${ srcDir } 的文件拷贝到 ${ destDir } 中`);
|
||||||
|
|
||||||
const copiedFiles = [];
|
const copiedFiles = [];
|
||||||
|
|
||||||
@ -138,7 +138,7 @@ export async function toBase64(filePath) {
|
|||||||
try {
|
try {
|
||||||
const fileData = await fs.readFile(filePath);
|
const fileData = await fs.readFile(filePath);
|
||||||
const base64Data = fileData.toString('base64');
|
const base64Data = fileData.toString('base64');
|
||||||
return `data:${getMimeType(filePath)};base64,${base64Data}`;
|
return `data:${ getMimeType(filePath) };base64,${ base64Data }`;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
handleError(error);
|
handleError(error);
|
||||||
}
|
}
|
||||||
@ -184,4 +184,4 @@ export async function getMediaFilesAndOthers(folderPath) {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
handleError(err);
|
handleError(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user