koishi-plugin-oni-sync-bot 0.8.3 → 0.8.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -448,7 +448,7 @@ var WikiBotService = class _WikiBotService extends import_koishi2.Service {
448
448
  });
449
449
  })(WikiBotService || (WikiBotService = {}));
450
450
 
451
- // src/plugins/consoleLogProvider.ts
451
+ // src/services/consoleLogProvider.ts
452
452
  var import_koishi3 = require("koishi");
453
453
  var import_plugin_console = require("@koishijs/plugin-console");
454
454
  var logBuffer = [];
@@ -1054,8 +1054,8 @@ __name(incrementalUpdate, "incrementalUpdate");
1054
1054
  // src/sync/moduleSync.ts
1055
1055
  var import_koishi7 = require("koishi");
1056
1056
  var CONFIG3 = {
1057
- MODLE_NAMESPACE: 828,
1058
- // 模块命名空间 (注意:这里原代码拼写为 MODLE,保留原样)
1057
+ MODULE_NAMESPACE: 828,
1058
+ // 模块命名空间
1059
1059
  IGNORED_MODULES: [],
1060
1060
  // 忽略的模块列表
1061
1061
  SYNC_INTERVAL_SUCCESS: 500,
@@ -1102,13 +1102,13 @@ async function syncSingleModule(oldSite, newSite, moduleTitle, user) {
1102
1102
  __name(syncSingleModule, "syncSingleModule");
1103
1103
  async function getAllModules(site) {
1104
1104
  logger.info(
1105
- `[SyncAllModules] 📥 开始获取原站点所有模块(命名空间${CONFIG3.MODLE_NAMESPACE})`
1105
+ `[SyncAllModules] 📥 开始获取原站点所有模块(命名空间${CONFIG3.MODULE_NAMESPACE})`
1106
1106
  );
1107
1107
  const allModules = [];
1108
1108
  const queryGen = site.continuedQueryGen({
1109
1109
  action: "query",
1110
1110
  list: "allpages",
1111
- apnamespace: CONFIG3.MODLE_NAMESPACE,
1111
+ apnamespace: CONFIG3.MODULE_NAMESPACE,
1112
1112
  // 模块命名空间
1113
1113
  aplimit: "max",
1114
1114
  apdir: "ascending"
@@ -1128,7 +1128,6 @@ async function syncModules(oldSite, newSite) {
1128
1128
  try {
1129
1129
  const oldModuleList = await getAllModules(oldSite);
1130
1130
  const total = oldModuleList.length;
1131
- console.log(oldModuleList);
1132
1131
  if (total === 0) {
1133
1132
  logger.info(`[SyncAllModules] 📭 原站点无模块可同步,结束`);
1134
1133
  return;
@@ -1225,16 +1224,11 @@ var SyncCommands = class {
1225
1224
  }
1226
1225
  static inject = ["wikiBot", "cron"];
1227
1226
  config;
1228
- log;
1229
1227
  constructor(ctx, config) {
1230
1228
  this.config = config;
1231
- this.log = ctx.logger("oni-sync");
1232
1229
  logger.info("WikiBot 服务已就绪,初始化定时任务和指令");
1233
1230
  ctx.cron("15 * * * *", async () => {
1234
- if (!ctx.wikiBot.isGGBotReady() || !ctx.wikiBot.isBWikiBotReady()) {
1235
- logger.warn("增量更新跳过:Wiki 机器人未就绪");
1236
- return;
1237
- }
1231
+ if (!await this.ensureBotsReady(ctx, "增量更新")) return;
1238
1232
  await incrementalUpdate(
1239
1233
  ctx.wikiBot.getGGBot(),
1240
1234
  ctx.wikiBot.getBWikiBot(),
@@ -1242,22 +1236,15 @@ var SyncCommands = class {
1242
1236
  );
1243
1237
  });
1244
1238
  ctx.cron("30 8 * * 4", async () => {
1245
- if (!ctx.wikiBot.isGGBotReady() || !ctx.wikiBot.isBWikiBotReady()) {
1246
- logger.warn("同步所有页面跳过:Wiki 机器人未就绪");
1247
- return;
1248
- }
1239
+ if (!await this.ensureBotsReady(ctx, "同步所有页面")) return;
1249
1240
  await syncPages(ctx.wikiBot.getGGBot(), ctx.wikiBot.getBWikiBot()).then(() => {
1250
1241
  logger.info("自动任务:尝试同步所有页面,从 WIKIGG 到 bwiki");
1251
1242
  }).catch((err) => {
1252
- logger.error(`同步所有页面失败`);
1253
- this.log.error(`,错误信息:${err}`);
1243
+ logger.error(`同步所有页面失败,错误信息:${err}`);
1254
1244
  });
1255
1245
  });
1256
1246
  ctx.cron("30 8 * * 3", async () => {
1257
- if (!ctx.wikiBot.isGGBotReady() || !ctx.wikiBot.isBWikiBotReady()) {
1258
- logger.warn("同步所有图片跳过:Wiki 机器人未就绪");
1259
- return;
1260
- }
1247
+ if (!await this.ensureBotsReady(ctx, "同步所有图片")) return;
1261
1248
  await syncAllImages(
1262
1249
  ctx.wikiBot.getGGBot(),
1263
1250
  ctx.wikiBot.getBWikiBot(),
@@ -1265,159 +1252,148 @@ var SyncCommands = class {
1265
1252
  ).then(() => {
1266
1253
  logger.info("自动任务:尝试同步所有图片,从 WIKIGG 到 bwiki");
1267
1254
  }).catch((err) => {
1268
- logger.error(`同步所有图片失败`);
1269
- this.log.error(`,错误信息:${err}`);
1255
+ logger.error(`同步所有图片失败,错误信息:${err}`);
1270
1256
  });
1271
1257
  });
1272
1258
  this.registerCommands(ctx);
1273
1259
  }
1274
- checkBotsReady(ctx) {
1260
+ /**
1261
+ * 确保机器人就绪,如果未就绪则尝试重新登录
1262
+ * @param ctx Koishi 上下文
1263
+ * @param taskName 任务名称(用于日志)
1264
+ * @returns 机器人是否已就绪
1265
+ */
1266
+ async ensureBotsReady(ctx, taskName) {
1267
+ if (!ctx.wikiBot.isGGBotReady() || !ctx.wikiBot.isBWikiBotReady()) {
1268
+ logger.warn(`检测到部分机器人未就绪,尝试重新登录...`);
1269
+ try {
1270
+ await ctx.wikiBot.relogin();
1271
+ } catch (error) {
1272
+ logger.error(`重新登录失败: ${error}`);
1273
+ return false;
1274
+ }
1275
+ }
1275
1276
  const ggReady = ctx.wikiBot.isGGBotReady();
1276
- const bwReady = ctx.wikiBot.isBWikiBotReady();
1277
- return ggReady && bwReady;
1277
+ const bwikiReady = ctx.wikiBot.isBWikiBotReady();
1278
+ if (!ggReady || !bwikiReady) {
1279
+ logger.warn(
1280
+ `${taskName} 跳过:Wiki 机器人仍未就绪 - WIKIGG: ${ggReady ? "✅" : "❌"}, bwiki: ${bwikiReady ? "✅" : "❌"}`
1281
+ );
1282
+ return false;
1283
+ }
1284
+ return true;
1278
1285
  }
1279
1286
  registerCommands(ctx) {
1280
1287
  ctx.command("sync <pageTitle:string>", "同步指定页面", { authority: 2 }).action(async ({ session }, pageTitle) => {
1281
- if (!this.checkBotsReady(ctx)) {
1282
- return session.send("❌ Wiki 机器人未就绪,请检查登录配置或查看日志");
1288
+ if (!await this.ensureBotsReady(ctx, "同步页面")) {
1289
+ return "❌ Wiki 机器人未就绪,请检查登录配置或查看日志";
1283
1290
  }
1284
- await syncSinglePage(
1285
- ctx.wikiBot.getGGBot(),
1286
- ctx.wikiBot.getBWikiBot(),
1287
- pageTitle,
1288
- "sync-bot"
1289
- ).then(() => {
1290
- session.send(
1291
- `✅ 已尝试同步页面:${pageTitle},请前往控制台查看:${this.config.logsUrl}`
1291
+ try {
1292
+ await syncSinglePage(
1293
+ ctx.wikiBot.getGGBot(),
1294
+ ctx.wikiBot.getBWikiBot(),
1295
+ pageTitle,
1296
+ "sync-bot"
1292
1297
  );
1293
- }).catch((err) => {
1294
- session.send(`❌ 同步页面失败:${pageTitle}`);
1295
- this.log.error(`,错误信息:${err}`);
1296
- });
1298
+ return `✅ 已尝试同步页面:${pageTitle},请前往控制台查看:${this.config.logsUrl}`;
1299
+ } catch (err) {
1300
+ logger.error(`同步页面 ${pageTitle} 失败,错误信息:${err}`);
1301
+ return `❌ 同步页面失败:${pageTitle}`;
1302
+ }
1297
1303
  });
1298
1304
  ctx.command("sync.incrementalUpdate", "获取3h内的编辑并尝试更新", {
1299
1305
  authority: 2
1300
1306
  }).alias("增量更新").action(async ({ session }) => {
1301
- if (!this.checkBotsReady(ctx)) {
1302
- return session.send("❌ Wiki 机器人未就绪,请检查登录配置或查看日志");
1307
+ if (!await this.ensureBotsReady(ctx, "增量更新")) {
1308
+ return "❌ Wiki 机器人未就绪,请检查登录配置或查看日志";
1303
1309
  }
1304
- session.send(
1305
- `🚀 获取3h内的编辑并尝试更新,任务耗时可能较长,请前往控制台查看日志:${this.config.logsUrl}`
1306
- );
1307
- await incrementalUpdate(
1308
- ctx.wikiBot.getGGBot(),
1309
- ctx.wikiBot.getBWikiBot(),
1310
- this.config
1311
- ).then(() => {
1312
- session.send(
1313
- `✅ 已尝试获取三小时前的编辑并同步,请前往控制台查看:${this.config.logsUrl}`
1314
- );
1315
- }).catch((err) => {
1316
- session.send(
1317
- `❌ 同步所有页面失败,请前往控制台查看日志:${this.config.logsUrl}`
1310
+ try {
1311
+ await incrementalUpdate(
1312
+ ctx.wikiBot.getGGBot(),
1313
+ ctx.wikiBot.getBWikiBot(),
1314
+ this.config
1318
1315
  );
1319
- this.log.error(`同步所有页面失败,错误信息:${err}`);
1320
- });
1316
+ return `✅ 已尝试获取三小时前的编辑并同步,请前往控制台查看:${this.config.logsUrl}`;
1317
+ } catch (err) {
1318
+ logger.error(`增量更新失败,错误信息:${err}`);
1319
+ return `❌ 增量更新失败,请前往控制台查看日志:${this.config.logsUrl}`;
1320
+ }
1321
1321
  });
1322
1322
  ctx.command("sync.allpages", "同步所有页面", { authority: 2 }).action(async ({ session }) => {
1323
- if (!this.checkBotsReady(ctx)) {
1324
- return session.send("❌ Wiki 机器人未就绪,请检查登录配置或查看日志");
1323
+ if (!await this.ensureBotsReady(ctx, "同步所有页面")) {
1324
+ return "❌ Wiki 机器人未就绪,请检查登录配置或查看日志";
1325
+ }
1326
+ try {
1327
+ await syncPages(ctx.wikiBot.getGGBot(), ctx.wikiBot.getBWikiBot());
1328
+ return `✅ 已尝试同步所有页面,请前往控制台查看:${this.config.logsUrl}`;
1329
+ } catch (err) {
1330
+ logger.error(`同步所有页面失败,错误信息:${err}`);
1331
+ return `❌ 同步所有页面失败,请前往控制台查看日志:${this.config.logsUrl}`;
1325
1332
  }
1326
- session.send(
1327
- `🚀 开始同步所有页面,任务耗时较长,请前往控制台查看日志:${this.config.logsUrl}`
1328
- );
1329
- await syncPages(ctx.wikiBot.getGGBot(), ctx.wikiBot.getBWikiBot()).then(() => {
1330
- session.send(
1331
- `✅ 已尝试同步所有页面,请前往控制台查看:${this.config.logsUrl}`
1332
- );
1333
- }).catch((err) => {
1334
- session.send(
1335
- `❌ 同步所有页面失败,请前往控制台查看日志:${this.config.logsUrl}`
1336
- );
1337
- this.log.error(`同步所有页面失败,错误信息:${err}`);
1338
- });
1339
1333
  });
1340
1334
  ctx.command("sync.module <moduleTitle:string>", "同步指定模块", {
1341
1335
  authority: 2
1342
1336
  }).action(async ({ session }, moduleTitle) => {
1343
- if (!this.checkBotsReady(ctx)) {
1344
- return session.send("❌ Wiki 机器人未就绪,请检查登录配置或查看日志");
1337
+ if (!await this.ensureBotsReady(ctx, "同步模块")) {
1338
+ return "❌ Wiki 机器人未就绪,请检查登录配置或查看日志";
1345
1339
  }
1346
- await session.send(
1347
- `✅ 同步中,请前往控制台查看:${this.config.logsUrl}`
1348
- );
1349
- await syncSingleModule(
1350
- ctx.wikiBot.getGGBot(),
1351
- ctx.wikiBot.getBWikiBot(),
1352
- moduleTitle,
1353
- "sync-bot"
1354
- ).then(() => {
1355
- session.send(
1356
- `✅ 已尝试同步模块:${moduleTitle},请前往控制台查看:${this.config.logsUrl}`
1340
+ try {
1341
+ await syncSingleModule(
1342
+ ctx.wikiBot.getGGBot(),
1343
+ ctx.wikiBot.getBWikiBot(),
1344
+ moduleTitle,
1345
+ "sync-bot"
1357
1346
  );
1358
- }).catch((err) => {
1359
- session.send(`❌ 同步模块失败:${moduleTitle}`);
1360
- this.log.error(`错误信息:${err}`);
1361
- });
1347
+ return `✅ 已尝试同步模块:${moduleTitle},请前往控制台查看:${this.config.logsUrl}`;
1348
+ } catch (err) {
1349
+ logger.error(`同步模块 ${moduleTitle} 失败,错误信息:${err}`);
1350
+ return `❌ 同步模块失败:${moduleTitle}`;
1351
+ }
1362
1352
  });
1363
1353
  ctx.command("sync.allmodules", "同步所有模块", { authority: 2 }).action(async ({ session }) => {
1364
- if (!this.checkBotsReady(ctx)) {
1365
- return session.send("❌ Wiki 机器人未就绪,请检查登录配置或查看日志");
1354
+ if (!await this.ensureBotsReady(ctx, "同步所有模块")) {
1355
+ return "❌ Wiki 机器人未就绪,请检查登录配置或查看日志";
1356
+ }
1357
+ try {
1358
+ await syncModules(ctx.wikiBot.getGGBot(), ctx.wikiBot.getBWikiBot());
1359
+ return `✅ 已尝试同步所有模块,请前往控制台查看:${this.config.logsUrl}`;
1360
+ } catch (err) {
1361
+ logger.error(`同步所有模块失败,错误信息:${err}`);
1362
+ return `❌ 同步所有模块失败,请前往控制台查看日志:${this.config.logsUrl}`;
1366
1363
  }
1367
- await session.send(
1368
- `🚀 开始同步所有模块,任务耗时较长,请前往控制台查看:${this.config.logsUrl}`
1369
- );
1370
- await syncModules(ctx.wikiBot.getGGBot(), ctx.wikiBot.getBWikiBot()).then(() => {
1371
- session.send(
1372
- `✅ 已尝试同步所有模块,请前往控制台查看:${this.config.logsUrl}`
1373
- );
1374
- }).catch((err) => {
1375
- session.send(
1376
- `❌ 同步所有模块失败,请前往控制台查看日志:${this.config.logsUrl}`
1377
- );
1378
- this.log.error(`同步所有模块失败,错误信息:${err}`);
1379
- });
1380
1364
  });
1381
1365
  ctx.command("sync.img <imgTitle:string>", "同步指定图片", { authority: 2 }).action(async ({ session }, imgTitle) => {
1382
- if (!this.checkBotsReady(ctx)) {
1383
- return session.send("❌ Wiki 机器人未就绪,请检查登录配置或查看日志");
1366
+ if (!await this.ensureBotsReady(ctx, "同步图片")) {
1367
+ return "❌ Wiki 机器人未就绪,请检查登录配置或查看日志";
1368
+ }
1369
+ try {
1370
+ await syncSingleImage(
1371
+ ctx.wikiBot.getGGBot(),
1372
+ ctx.wikiBot.getBWikiBot(),
1373
+ `${imgTitle.startsWith("File:") ? "" : "File:"}${imgTitle}`,
1374
+ this.config
1375
+ );
1376
+ return `✅ 已尝试同步图片:${imgTitle}`;
1377
+ } catch (err) {
1378
+ logger.error(`同步图片 ${imgTitle} 失败,错误信息:${err}`);
1379
+ return `❌ 同步图片失败:${imgTitle}`;
1384
1380
  }
1385
- await session.send(
1386
- `🚀 开始同步,任务可能耗时较长,请前往控制台查看:${this.config.logsUrl}`
1387
- );
1388
- await syncSingleImage(
1389
- ctx.wikiBot.getGGBot(),
1390
- ctx.wikiBot.getBWikiBot(),
1391
- `${imgTitle.startsWith("File:") ? "" : "File:"}${imgTitle}`,
1392
- this.config
1393
- ).then(() => {
1394
- session.send(`✅ 已尝试同步图片:${imgTitle}`);
1395
- }).catch((err) => {
1396
- session.send(`❌ 同步图片失败:${imgTitle}`);
1397
- this.log.error(`同步图片失败:${imgTitle},错误信息:${err}`);
1398
- });
1399
1381
  });
1400
1382
  ctx.command("sync.allimgs", "同步所有图片", { authority: 2 }).action(async ({ session }) => {
1401
- if (!this.checkBotsReady(ctx)) {
1402
- return session.send("❌ Wiki 机器人未就绪,请检查登录配置或查看日志");
1383
+ if (!await this.ensureBotsReady(ctx, "同步所有图片")) {
1384
+ return "❌ Wiki 机器人未就绪,请检查登录配置或查看日志";
1403
1385
  }
1404
- session.send(
1405
- `🚀 开始同步所有图片,任务耗时较长,请前往控制台查看:${this.config.logsUrl}`
1406
- );
1407
- await syncAllImages(
1408
- ctx.wikiBot.getGGBot(),
1409
- ctx.wikiBot.getBWikiBot(),
1410
- this.config
1411
- ).then(() => {
1412
- session.send(
1413
- `✅ 已尝试同步所有图片,请前往控制台查看:${this.config.logsUrl}`
1414
- );
1415
- }).catch((err) => {
1416
- session.send(
1417
- `❌ 同步所有图片失败,请前往控制台查看日志:${this.config.logsUrl}`
1386
+ try {
1387
+ await syncAllImages(
1388
+ ctx.wikiBot.getGGBot(),
1389
+ ctx.wikiBot.getBWikiBot(),
1390
+ this.config
1418
1391
  );
1419
- this.log.error(`同步所有图片失败,错误信息:${err}`);
1420
- });
1392
+ return `✅ 已尝试同步所有图片,请前往控制台查看:${this.config.logsUrl}`;
1393
+ } catch (err) {
1394
+ logger.error(`同步所有图片失败,错误信息:${err}`);
1395
+ return `❌ 同步所有图片失败,请前往控制台查看日志:${this.config.logsUrl}`;
1396
+ }
1421
1397
  });
1422
1398
  }
1423
1399
  };
@@ -1,4 +1,4 @@
1
- import { Context, Logger, Schema } from "koishi";
1
+ import { Context, Schema } from "koishi";
2
2
  export interface SyncCommandsConfig {
3
3
  logsUrl: string;
4
4
  ggUsername: string;
@@ -12,9 +12,14 @@ export interface SyncCommandsConfig {
12
12
  export declare class SyncCommands {
13
13
  static readonly inject: string[];
14
14
  config: SyncCommandsConfig;
15
- log: Logger;
16
15
  constructor(ctx: Context, config: SyncCommandsConfig);
17
- private checkBotsReady;
16
+ /**
17
+ * 确保机器人就绪,如果未就绪则尝试重新登录
18
+ * @param ctx Koishi 上下文
19
+ * @param taskName 任务名称(用于日志)
20
+ * @returns 机器人是否已就绪
21
+ */
22
+ private ensureBotsReady;
18
23
  private registerCommands;
19
24
  }
20
25
  export declare namespace SyncCommands {
@@ -0,0 +1,13 @@
1
+ import { Context, Logger } from "koishi";
2
+ declare module "@koishijs/console" {
3
+ namespace Console {
4
+ interface Services {
5
+ onilogs: DataService<Logger.Record[]>;
6
+ }
7
+ }
8
+ }
9
+ export declare class ConsoleLogProvider {
10
+ static readonly inject: string[];
11
+ constructor(ctx: Context);
12
+ }
13
+ export declare function apply(ctx: Context): void;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-oni-sync-bot",
3
3
  "description": "缺氧Wiki站镜像点同步-测试",
4
- "version": "0.8.3",
4
+ "version": "0.8.5",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
package/readme.md CHANGED
@@ -156,12 +156,12 @@ yarn add koishi-plugin-oni-sync-bot
156
156
  koishi-plugin-oni-sync-bot/
157
157
  ├── src/
158
158
  │ ├── services/ # 服务层
159
- │ │ └── wikiBotService.ts # Wiki 机器人服务
159
+ │ │ ├── wikiBotService.ts # Wiki 机器人服务
160
+ │ │ └── consoleLogProvider.ts # 日志控制台服务
160
161
  │ ├── plugins/ # 插件层
161
162
  │ │ ├── queryCommands.ts # 查询命令插件
162
163
  │ │ ├── syncCommands.ts # 同步命令插件
163
164
  │ │ ├── updateCommands.ts # 更新命令插件
164
- │ │ ├── consoleLogProvider.ts # 日志控制台插件
165
165
  │ │ ├── todoList.ts # TodoList 插件
166
166
  │ │ ├── databaseExtension.ts # 数据库扩展
167
167
  │ │ └── routeRedirect.ts # 路由重定向