koishi-plugin-wowsreplay-to-video
Version:
135 lines (133 loc) • 5.63 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name2 in all)
__defProp(target, name2, { get: all[name2], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
Config: () => Config,
apply: () => apply,
inject: () => inject,
name: () => name
});
module.exports = __toCommonJS(src_exports);
var import_koishi = require("koishi");
var import_child_process = require("child_process");
var import_path = __toESM(require("path"));
var import_fs = __toESM(require("fs"));
var import_node_os = require("node:os");
var Config = import_koishi.Schema.object({
pythonPath: import_koishi.Schema.string().default("D:/wows/renderer/venv/Scripts/python.exe").description("Python 执行路径"),
replayMessages: import_koishi.Schema.array(import_koishi.Schema.string()).default([
"这把是不是你打得有问题?",
"你潜艇觉得是我的锅,那就是我的锅。",
"你AP打断别人的占点呀,我阐释你的梦",
"他都已经玩潜艇了,你为什么不顺从他呢?"
]).description("复盘时的随机评论句子")
});
var name = "koishi-plugin-wowsreplay-to-video";
var inject = {
required: ["http"]
};
function apply(ctx, config) {
const TMP_DIR = (0, import_node_os.tmpdir)();
ctx.command("replay <fileUrl>", "上传 wowsreplay 文件链接生成视频").alias("复盘").action(async ({ session }, fileUrl) => {
if (!fileUrl) return "请提供一个文件链接!可以使用wowsreplay.mihoyo.su上传.wowsreplay文件,然后将生成的链接发送至此";
if (!fileUrl.endsWith(".wowsreplay")) return "请提供后缀为 .wowsreplay 的文件链接!";
const fileName = import_path.default.basename(fileUrl);
const tempDir = import_path.default.resolve(__dirname, "../uploads");
if (!import_fs.default.existsSync(tempDir)) import_fs.default.mkdirSync(tempDir);
const replayPath = import_path.default.join(tempDir, fileName);
const mp4Path = replayPath.replace(".wowsreplay", ".mp4");
const pythonPath = config.pythonPath;
if (!pythonPath) {
return "Python 路径未配置,请在后台设置。";
}
try {
await downloadFile(fileUrl, replayPath);
await runPythonScript(pythonPath, replayPath);
if (!import_fs.default.existsSync(mp4Path)) {
return "视频生成失败,请检查脚本逻辑或环境配置。";
}
const video_temp = import_fs.default.readFileSync(mp4Path);
const randomMessage = getRandomMessage(config.replayMessages);
session.send(randomMessage);
return import_koishi.h.video(video_temp, "video/mp4");
} finally {
if (import_fs.default.existsSync(replayPath)) import_fs.default.unlinkSync(replayPath);
if (import_fs.default.existsSync(mp4Path)) import_fs.default.unlinkSync(mp4Path);
}
});
}
__name(apply, "apply");
function getRandomMessage(messages) {
const randomIndex = Math.floor(Math.random() * messages.length);
return messages[randomIndex];
}
__name(getRandomMessage, "getRandomMessage");
function runPythonScript(pythonPath, replayPath) {
return new Promise((resolve, reject) => {
const command = `${pythonPath} -m render --replay "${replayPath}"`;
(0, import_child_process.exec)(command, (error, stdout, stderr) => {
if (error) {
console.error("执行出错:", stderr);
return reject(error.message);
}
resolve();
});
});
}
__name(runPythonScript, "runPythonScript");
async function downloadFile(url, dest) {
const fs2 = require("fs");
const http = require("http");
const https = require("https");
const protocol = url.startsWith("https") ? https : http;
return new Promise((resolve, reject) => {
const file = fs2.createWriteStream(dest);
protocol.get(url, (response) => {
if (response.statusCode !== 200) {
reject(`下载失败,状态码:${response.statusCode}`);
return;
}
response.pipe(file);
file.on("finish", () => {
file.close(resolve);
});
}).on("error", (err) => {
fs2.unlink(dest, () => reject(err.message));
});
});
}
__name(downloadFile, "downloadFile");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Config,
apply,
inject,
name
});