koishi-plugin-recorder
Advanced tools
Comparing version
@@ -1,16 +0,11 @@ | ||
/// <reference types="node" /> | ||
import { Context, Meta } from 'koishi-core'; | ||
import { WriteStream } from 'fs'; | ||
import { Meta, Context } from 'koishi-core'; | ||
declare module 'koishi-core/dist/context' { | ||
interface EventMap { | ||
'record-opened'(stream: WriteStream, path: string): any; | ||
'record-closing'(stream: WriteStream, path: string): any; | ||
'record-writing'(chunk: string, meta: Meta): any; | ||
'record-written'(chunk: string, meta: Meta): any; | ||
'before-record'(meta: Meta<'message' | 'send'>): any; | ||
} | ||
} | ||
export interface RecorderOptions { | ||
transform?(meta: Meta): string; | ||
target?(meta: Meta): string | void; | ||
folder?: string; | ||
} | ||
export declare const name = "recorder"; | ||
export declare function apply(ctx: Context, options?: RecorderOptions): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const koishi_core_1 = require("koishi-core"); | ||
const fs_1 = require("fs"); | ||
@@ -9,45 +8,25 @@ const path_1 = require("path"); | ||
} | ||
const refs = new WeakSet(); | ||
const defaultOptions = { | ||
target(meta) { | ||
if (refs.has(meta) || meta.$ctxType !== 'group') | ||
return; | ||
return `messages/${meta.groupId}.txt`; | ||
}, | ||
transform(meta) { | ||
refs.add(meta); | ||
return JSON.stringify(pick(meta, ['$ctxType', '$ctxId', 'userId', 'message'])); | ||
}, | ||
}; | ||
const streams = {}; | ||
const cwd = process.cwd(); | ||
exports.name = 'recorder'; | ||
function apply(ctx, options = {}) { | ||
options = { ...defaultOptions, ...options }; | ||
function handleMessage(meta) { | ||
const target = options.target(meta); | ||
if (!target) | ||
async function handleMessage(meta) { | ||
if (meta.$ctxType !== 'group' || meta.postType === 'message' && meta.$group.assignee !== ctx.app.selfId) | ||
return; | ||
const output = options.transform(meta) + '\n'; | ||
const path = path_1.resolve(process.cwd(), target); | ||
if (await ctx.serialize('before-record', meta)) | ||
return; | ||
const output = JSON.stringify(pick(meta, ['time', 'userId', 'message'])) + '\n'; | ||
const path = path_1.resolve(cwd, options.folder || 'messages', `${meta.groupId}.txt`); | ||
if (!streams[path]) { | ||
const folder = path_1.dirname(path); | ||
if (!fs_1.existsSync(folder)) | ||
if (!fs_1.existsSync(folder)) { | ||
fs_1.mkdirSync(folder, { recursive: true }); | ||
} | ||
streams[path] = fs_1.createWriteStream(path, { flags: 'a' }); | ||
streams[path].on('close', () => delete streams[path]); | ||
ctx.app.receiver.emit('record-opened', streams[path], path); | ||
} | ||
ctx.app.receiver.emit('record-writing', output, meta); | ||
streams[path].write(output, () => { | ||
ctx.app.receiver.emit('record-written', output, meta); | ||
}); | ||
streams[path].write(output); | ||
} | ||
koishi_core_1.onStop(() => { | ||
for (const key in streams) { | ||
ctx.app.receiver.emit('record-closing', streams[key], key); | ||
streams[key].close(); | ||
} | ||
}); | ||
ctx.receiver.on('message', handleMessage); | ||
ctx.receiver.on('before-send', handleMessage); | ||
ctx.on('attach-group', handleMessage); | ||
ctx.on('before-send', handleMessage); | ||
} | ||
exports.apply = apply; |
{ | ||
"name": "koishi-plugin-recorder", | ||
"description": "Save Chat Records for Koishi", | ||
"version": "1.0.0-alpha.10", | ||
"version": "2.0.0-alpha.0", | ||
"main": "dist/index.js", | ||
"typings": "dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"author": "Shigma <1700011071@pku.edu.cn>", | ||
@@ -11,3 +14,4 @@ "license": "MIT", | ||
"build": "tsc -b", | ||
"lint": "eslint src --ext .ts" | ||
"lint": "eslint src --ext .ts", | ||
"prepack": "tsc -b" | ||
}, | ||
@@ -35,8 +39,8 @@ "repository": { | ||
"del": "^5.1.0", | ||
"koishi-test-utils": "^3.2.1" | ||
"koishi-test-utils": "^4.0.0-alpha.0" | ||
}, | ||
"dependencies": { | ||
"koishi-core": "^1.11.1", | ||
"koishi-core": "^2.0.0-alpha.0", | ||
"koishi-utils": "^1.0.4" | ||
} | ||
} |
# [koishi-plugin-recorder](https://koishi.js.org/plugins/recorder.html) | ||
[](https://www.npmjs.com/package/koishi-plugin-recorder) |
2838
-98.56%4
-55.56%42
-75%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
Updated