mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-02-11 23:40:24 +00:00
chore: OneBotApi
This commit is contained in:
10
src/onebot/action/system/CanSendImage.ts
Normal file
10
src/onebot/action/system/CanSendImage.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { ActionName } from '../types';
|
||||
import CanSendRecord from './CanSendRecord';
|
||||
|
||||
interface ReturnType {
|
||||
yes: boolean
|
||||
}
|
||||
|
||||
export default class CanSendImage extends CanSendRecord {
|
||||
actionName = ActionName.CanSendImage;
|
||||
}
|
||||
16
src/onebot/action/system/CanSendRecord.ts
Normal file
16
src/onebot/action/system/CanSendRecord.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import BaseAction from '../BaseAction';
|
||||
import { ActionName } from '../types';
|
||||
|
||||
interface ReturnType {
|
||||
yes: boolean
|
||||
}
|
||||
|
||||
export default class CanSendRecord extends BaseAction<any, ReturnType> {
|
||||
actionName = ActionName.CanSendRecord;
|
||||
|
||||
protected async _handle(_payload: void): Promise<ReturnType> {
|
||||
return {
|
||||
yes: true
|
||||
};
|
||||
}
|
||||
}
|
||||
93
src/onebot/action/system/CleanCache.ts
Normal file
93
src/onebot/action/system/CleanCache.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
import BaseAction from '../BaseAction';
|
||||
import { ActionName } from '../types';
|
||||
import fs from 'fs';
|
||||
import Path from 'path';
|
||||
import {
|
||||
ChatType,
|
||||
ChatCacheListItemBasic,
|
||||
CacheFileType
|
||||
} from '@/core/entities';
|
||||
import { NTQQFileApi, NTQQFileCacheApi } from '@/core/apis/file';
|
||||
import { logError } from '@/common/utils/log';
|
||||
|
||||
export default class CleanCache extends BaseAction<void, void> {
|
||||
actionName = ActionName.CleanCache;
|
||||
|
||||
protected _handle(): Promise<void> {
|
||||
return new Promise<void>(async (res, rej) => {
|
||||
try {
|
||||
// dbUtil.clearCache();
|
||||
const cacheFilePaths: string[] = [];
|
||||
|
||||
await NTQQFileCacheApi.setCacheSilentScan(false);
|
||||
|
||||
cacheFilePaths.push((await NTQQFileCacheApi.getHotUpdateCachePath()));
|
||||
cacheFilePaths.push((await NTQQFileCacheApi.getDesktopTmpPath()));
|
||||
(await NTQQFileCacheApi.getCacheSessionPathList()).forEach((e: { value: string; }) => cacheFilePaths.push(e.value));
|
||||
|
||||
// await NTQQApi.addCacheScannedPaths(); // XXX: 调用就崩溃,原因目前还未知
|
||||
const cacheScanResult = await NTQQFileCacheApi.scanCache();
|
||||
const cacheSize = parseInt(cacheScanResult.size[6]);
|
||||
|
||||
if (cacheScanResult.result !== 0) {
|
||||
throw('Something went wrong while scanning cache. Code: ' + cacheScanResult.result);
|
||||
}
|
||||
|
||||
await NTQQFileCacheApi.setCacheSilentScan(true);
|
||||
if (cacheSize > 0 && cacheFilePaths.length > 2) { // 存在缓存文件且大小不为 0 时执行清理动作
|
||||
// await NTQQApi.clearCache([ 'tmp', 'hotUpdate', ...cacheScanResult ]) // XXX: 也是调用就崩溃,调用 fs 删除得了
|
||||
deleteCachePath(cacheFilePaths);
|
||||
}
|
||||
|
||||
// 获取聊天记录列表
|
||||
// NOTE: 以防有人不需要删除聊天记录,暂时先注释掉,日后加个开关
|
||||
// const privateChatCache = await getCacheList(ChatType.friend); // 私聊消息
|
||||
// const groupChatCache = await getCacheList(ChatType.group); // 群聊消息
|
||||
// const chatCacheList = [ ...privateChatCache, ...groupChatCache ];
|
||||
const chatCacheList: ChatCacheListItemBasic[] = [];
|
||||
|
||||
// 获取聊天缓存文件列表
|
||||
const cacheFileList: string[] = [];
|
||||
|
||||
for (const name in CacheFileType) {
|
||||
if (!isNaN(parseInt(name))) continue;
|
||||
|
||||
const fileTypeAny: any = CacheFileType[name];
|
||||
const fileType: CacheFileType = fileTypeAny;
|
||||
|
||||
cacheFileList.push(...(await NTQQFileCacheApi.getFileCacheInfo(fileType)).infos.map((file: { fileKey: any; }) => file.fileKey));
|
||||
}
|
||||
|
||||
// 一并清除
|
||||
await NTQQFileCacheApi.clearChatCache(chatCacheList, cacheFileList);
|
||||
res();
|
||||
} catch(e) {
|
||||
logError('清理缓存时发生了错误');
|
||||
rej(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function deleteCachePath(pathList: string[]) {
|
||||
const emptyPath = (path: string) => {
|
||||
if (!fs.existsSync(path)) return;
|
||||
const files = fs.readdirSync(path);
|
||||
files.forEach(file => {
|
||||
const filePath = Path.resolve(path, file);
|
||||
const stats = fs.statSync(filePath);
|
||||
if (stats.isDirectory()) emptyPath(filePath);
|
||||
else fs.unlinkSync(filePath);
|
||||
});
|
||||
fs.rmdirSync(path);
|
||||
};
|
||||
|
||||
for (const path of pathList) {
|
||||
emptyPath(path);
|
||||
}
|
||||
}
|
||||
|
||||
function getCacheList(type: ChatType) { // NOTE: 做这个方法主要是因为目前还不支持针对频道消息的清理
|
||||
return new Promise<Array<ChatCacheListItemBasic>>((res, rej) => {
|
||||
});
|
||||
}
|
||||
17
src/onebot/action/system/GetLoginInfo.ts
Normal file
17
src/onebot/action/system/GetLoginInfo.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { selfInfo } from '@/core/data';
|
||||
import { OB11User } from '../../types';
|
||||
import { OB11Constructor } from '../../constructor';
|
||||
import BaseAction from '../BaseAction';
|
||||
import { ActionName } from '../types';
|
||||
import { napCatCore } from '@/core';
|
||||
|
||||
|
||||
class GetLoginInfo extends BaseAction<null, OB11User> {
|
||||
actionName = ActionName.GetLoginInfo;
|
||||
|
||||
protected async _handle(payload: null) {
|
||||
return OB11Constructor.selfInfo(selfInfo);
|
||||
}
|
||||
}
|
||||
|
||||
export default GetLoginInfo;
|
||||
17
src/onebot/action/system/GetStatus.ts
Normal file
17
src/onebot/action/system/GetStatus.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import BaseAction from '../BaseAction';
|
||||
import { OB11Status } from '../../types';
|
||||
import { ActionName } from '../types';
|
||||
import { selfInfo, stat } from '@/core/data';
|
||||
|
||||
|
||||
export default class GetStatus extends BaseAction<any, OB11Status> {
|
||||
actionName = ActionName.GetStatus;
|
||||
|
||||
protected async _handle(payload: any): Promise<OB11Status> {
|
||||
return {
|
||||
online: !!selfInfo.online,
|
||||
good: true,
|
||||
stat
|
||||
};
|
||||
}
|
||||
}
|
||||
16
src/onebot/action/system/GetVersionInfo.ts
Normal file
16
src/onebot/action/system/GetVersionInfo.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import BaseAction from '../BaseAction';
|
||||
import { OB11Version } from '../../types';
|
||||
import { ActionName } from '../types';
|
||||
import { version } from '@/onebot11/version';
|
||||
|
||||
export default class GetVersionInfo extends BaseAction<any, OB11Version> {
|
||||
actionName = ActionName.GetVersionInfo;
|
||||
|
||||
protected async _handle(payload: any): Promise<OB11Version> {
|
||||
return {
|
||||
app_name: 'NapCat.Onebot',
|
||||
protocol_version: 'v11',
|
||||
app_version: version
|
||||
};
|
||||
}
|
||||
}
|
||||
43
src/onebot/action/system/Reboot.ts
Normal file
43
src/onebot/action/system/Reboot.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { rebootWithNormolLogin, rebootWithQuickLogin } from '@/common/utils/reboot';
|
||||
import BaseAction from '../BaseAction';
|
||||
import { ActionName } from '../types';
|
||||
import { selfInfo } from '@/core/data';
|
||||
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
|
||||
|
||||
const SchemaData = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
delay: { type: 'number' }
|
||||
}
|
||||
} as const satisfies JSONSchema;
|
||||
|
||||
type Payload = FromSchema<typeof SchemaData>;
|
||||
|
||||
export class Reboot extends BaseAction<Payload, null> {
|
||||
actionName = ActionName.Reboot;
|
||||
|
||||
protected async _handle(payload: Payload): Promise<null> {
|
||||
if (payload.delay) {
|
||||
setTimeout(() => {
|
||||
rebootWithQuickLogin(selfInfo.uin);
|
||||
}, payload.delay);
|
||||
} else {
|
||||
rebootWithQuickLogin(selfInfo.uin);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
export class RebootNormal extends BaseAction<Payload, null> {
|
||||
actionName = ActionName.RebootNormal;
|
||||
|
||||
protected async _handle(payload: Payload): Promise<null> {
|
||||
if (payload.delay) {
|
||||
setTimeout(() => {
|
||||
rebootWithNormolLogin();
|
||||
}, payload.delay);
|
||||
} else {
|
||||
rebootWithNormolLogin();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user