koishi-plugin-oni-sync-bot 0.8.2 → 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
@@ -62,7 +62,10 @@ async function getAndProcessPageContent(site, pageTitle) {
62
62
  try {
63
63
  let rawText = "";
64
64
  try {
65
- const res = await site.read(pageTitle);
65
+ const res = await site.read(pageTitle, {
66
+ redirects: false
67
+ // 不自动跟随重定向
68
+ });
66
69
  if (res) {
67
70
  if (typeof res === "string") {
68
71
  rawText = res;
@@ -445,7 +448,7 @@ var WikiBotService = class _WikiBotService extends import_koishi2.Service {
445
448
  });
446
449
  })(WikiBotService || (WikiBotService = {}));
447
450
 
448
- // src/plugins/consoleLogProvider.ts
451
+ // src/services/consoleLogProvider.ts
449
452
  var import_koishi3 = require("koishi");
450
453
  var import_plugin_console = require("@koishijs/plugin-console");
451
454
  var logBuffer = [];
@@ -815,7 +818,7 @@ __name(syncAllImages, "syncAllImages");
815
818
 
816
819
  // src/sync/pageSync.ts
817
820
  var CONFIG2 = {
818
- IGNORED_PAGES: /* @__PURE__ */ new Set(["教程", "MediaWiki:Common.css"]),
821
+ IGNORED_PAGES: /* @__PURE__ */ new Set(["教程", "MediaWiki:Common.css", "首页", "Main Page"]),
819
822
  SYNC_INTERVAL_SUCCESS: 500,
820
823
  SYNC_INTERVAL_FAILED: 1e3,
821
824
  NAMESPACE: 0,
@@ -1051,8 +1054,8 @@ __name(incrementalUpdate, "incrementalUpdate");
1051
1054
  // src/sync/moduleSync.ts
1052
1055
  var import_koishi7 = require("koishi");
1053
1056
  var CONFIG3 = {
1054
- MODLE_NAMESPACE: 828,
1055
- // 模块命名空间 (注意:这里原代码拼写为 MODLE,保留原样)
1057
+ MODULE_NAMESPACE: 828,
1058
+ // 模块命名空间
1056
1059
  IGNORED_MODULES: [],
1057
1060
  // 忽略的模块列表
1058
1061
  SYNC_INTERVAL_SUCCESS: 500,
@@ -1099,13 +1102,13 @@ async function syncSingleModule(oldSite, newSite, moduleTitle, user) {
1099
1102
  __name(syncSingleModule, "syncSingleModule");
1100
1103
  async function getAllModules(site) {
1101
1104
  logger.info(
1102
- `[SyncAllModules] 📥 开始获取原站点所有模块(命名空间${CONFIG3.MODLE_NAMESPACE})`
1105
+ `[SyncAllModules] 📥 开始获取原站点所有模块(命名空间${CONFIG3.MODULE_NAMESPACE})`
1103
1106
  );
1104
1107
  const allModules = [];
1105
1108
  const queryGen = site.continuedQueryGen({
1106
1109
  action: "query",
1107
1110
  list: "allpages",
1108
- apnamespace: CONFIG3.MODLE_NAMESPACE,
1111
+ apnamespace: CONFIG3.MODULE_NAMESPACE,
1109
1112
  // 模块命名空间
1110
1113
  aplimit: "max",
1111
1114
  apdir: "ascending"
@@ -1125,7 +1128,6 @@ async function syncModules(oldSite, newSite) {
1125
1128
  try {
1126
1129
  const oldModuleList = await getAllModules(oldSite);
1127
1130
  const total = oldModuleList.length;
1128
- console.log(oldModuleList);
1129
1131
  if (total === 0) {
1130
1132
  logger.info(`[SyncAllModules] 📭 原站点无模块可同步,结束`);
1131
1133
  return;
@@ -1222,16 +1224,11 @@ var SyncCommands = class {
1222
1224
  }
1223
1225
  static inject = ["wikiBot", "cron"];
1224
1226
  config;
1225
- log;
1226
1227
  constructor(ctx, config) {
1227
1228
  this.config = config;
1228
- this.log = ctx.logger("oni-sync");
1229
1229
  logger.info("WikiBot 服务已就绪,初始化定时任务和指令");
1230
1230
  ctx.cron("15 * * * *", async () => {
1231
- if (!ctx.wikiBot.isGGBotReady() || !ctx.wikiBot.isBWikiBotReady()) {
1232
- logger.warn("增量更新跳过:Wiki 机器人未就绪");
1233
- return;
1234
- }
1231
+ if (!await this.ensureBotsReady(ctx, "增量更新")) return;
1235
1232
  await incrementalUpdate(
1236
1233
  ctx.wikiBot.getGGBot(),
1237
1234
  ctx.wikiBot.getBWikiBot(),
@@ -1239,22 +1236,15 @@ var SyncCommands = class {
1239
1236
  );
1240
1237
  });
1241
1238
  ctx.cron("30 8 * * 4", async () => {
1242
- if (!ctx.wikiBot.isGGBotReady() || !ctx.wikiBot.isBWikiBotReady()) {
1243
- logger.warn("同步所有页面跳过:Wiki 机器人未就绪");
1244
- return;
1245
- }
1239
+ if (!await this.ensureBotsReady(ctx, "同步所有页面")) return;
1246
1240
  await syncPages(ctx.wikiBot.getGGBot(), ctx.wikiBot.getBWikiBot()).then(() => {
1247
1241
  logger.info("自动任务:尝试同步所有页面,从 WIKIGG 到 bwiki");
1248
1242
  }).catch((err) => {
1249
- logger.error(`同步所有页面失败`);
1250
- this.log.error(`,错误信息:${err}`);
1243
+ logger.error(`同步所有页面失败,错误信息:${err}`);
1251
1244
  });
1252
1245
  });
1253
1246
  ctx.cron("30 8 * * 3", async () => {
1254
- if (!ctx.wikiBot.isGGBotReady() || !ctx.wikiBot.isBWikiBotReady()) {
1255
- logger.warn("同步所有图片跳过:Wiki 机器人未就绪");
1256
- return;
1257
- }
1247
+ if (!await this.ensureBotsReady(ctx, "同步所有图片")) return;
1258
1248
  await syncAllImages(
1259
1249
  ctx.wikiBot.getGGBot(),
1260
1250
  ctx.wikiBot.getBWikiBot(),
@@ -1262,159 +1252,148 @@ var SyncCommands = class {
1262
1252
  ).then(() => {
1263
1253
  logger.info("自动任务:尝试同步所有图片,从 WIKIGG 到 bwiki");
1264
1254
  }).catch((err) => {
1265
- logger.error(`同步所有图片失败`);
1266
- this.log.error(`,错误信息:${err}`);
1255
+ logger.error(`同步所有图片失败,错误信息:${err}`);
1267
1256
  });
1268
1257
  });
1269
1258
  this.registerCommands(ctx);
1270
1259
  }
1271
- 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
+ }
1272
1276
  const ggReady = ctx.wikiBot.isGGBotReady();
1273
- const bwReady = ctx.wikiBot.isBWikiBotReady();
1274
- 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;
1275
1285
  }
1276
1286
  registerCommands(ctx) {
1277
1287
  ctx.command("sync <pageTitle:string>", "同步指定页面", { authority: 2 }).action(async ({ session }, pageTitle) => {
1278
- if (!this.checkBotsReady(ctx)) {
1279
- return session.send("❌ Wiki 机器人未就绪,请检查登录配置或查看日志");
1288
+ if (!await this.ensureBotsReady(ctx, "同步页面")) {
1289
+ return "❌ Wiki 机器人未就绪,请检查登录配置或查看日志";
1280
1290
  }
1281
- await syncSinglePage(
1282
- ctx.wikiBot.getGGBot(),
1283
- ctx.wikiBot.getBWikiBot(),
1284
- pageTitle,
1285
- "sync-bot"
1286
- ).then(() => {
1287
- session.send(
1288
- `✅ 已尝试同步页面:${pageTitle},请前往控制台查看:${this.config.logsUrl}`
1291
+ try {
1292
+ await syncSinglePage(
1293
+ ctx.wikiBot.getGGBot(),
1294
+ ctx.wikiBot.getBWikiBot(),
1295
+ pageTitle,
1296
+ "sync-bot"
1289
1297
  );
1290
- }).catch((err) => {
1291
- session.send(`❌ 同步页面失败:${pageTitle}`);
1292
- this.log.error(`,错误信息:${err}`);
1293
- });
1298
+ return `✅ 已尝试同步页面:${pageTitle},请前往控制台查看:${this.config.logsUrl}`;
1299
+ } catch (err) {
1300
+ logger.error(`同步页面 ${pageTitle} 失败,错误信息:${err}`);
1301
+ return `❌ 同步页面失败:${pageTitle}`;
1302
+ }
1294
1303
  });
1295
1304
  ctx.command("sync.incrementalUpdate", "获取3h内的编辑并尝试更新", {
1296
1305
  authority: 2
1297
1306
  }).alias("增量更新").action(async ({ session }) => {
1298
- if (!this.checkBotsReady(ctx)) {
1299
- return session.send("❌ Wiki 机器人未就绪,请检查登录配置或查看日志");
1307
+ if (!await this.ensureBotsReady(ctx, "增量更新")) {
1308
+ return "❌ Wiki 机器人未就绪,请检查登录配置或查看日志";
1300
1309
  }
1301
- session.send(
1302
- `🚀 获取3h内的编辑并尝试更新,任务耗时可能较长,请前往控制台查看日志:${this.config.logsUrl}`
1303
- );
1304
- await incrementalUpdate(
1305
- ctx.wikiBot.getGGBot(),
1306
- ctx.wikiBot.getBWikiBot(),
1307
- this.config
1308
- ).then(() => {
1309
- session.send(
1310
- `✅ 已尝试获取三小时前的编辑并同步,请前往控制台查看:${this.config.logsUrl}`
1311
- );
1312
- }).catch((err) => {
1313
- session.send(
1314
- `❌ 同步所有页面失败,请前往控制台查看日志:${this.config.logsUrl}`
1310
+ try {
1311
+ await incrementalUpdate(
1312
+ ctx.wikiBot.getGGBot(),
1313
+ ctx.wikiBot.getBWikiBot(),
1314
+ this.config
1315
1315
  );
1316
- this.log.error(`同步所有页面失败,错误信息:${err}`);
1317
- });
1316
+ return `✅ 已尝试获取三小时前的编辑并同步,请前往控制台查看:${this.config.logsUrl}`;
1317
+ } catch (err) {
1318
+ logger.error(`增量更新失败,错误信息:${err}`);
1319
+ return `❌ 增量更新失败,请前往控制台查看日志:${this.config.logsUrl}`;
1320
+ }
1318
1321
  });
1319
1322
  ctx.command("sync.allpages", "同步所有页面", { authority: 2 }).action(async ({ session }) => {
1320
- if (!this.checkBotsReady(ctx)) {
1321
- 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}`;
1322
1332
  }
1323
- session.send(
1324
- `🚀 开始同步所有页面,任务耗时较长,请前往控制台查看日志:${this.config.logsUrl}`
1325
- );
1326
- await syncPages(ctx.wikiBot.getGGBot(), ctx.wikiBot.getBWikiBot()).then(() => {
1327
- session.send(
1328
- `✅ 已尝试同步所有页面,请前往控制台查看:${this.config.logsUrl}`
1329
- );
1330
- }).catch((err) => {
1331
- session.send(
1332
- `❌ 同步所有页面失败,请前往控制台查看日志:${this.config.logsUrl}`
1333
- );
1334
- this.log.error(`同步所有页面失败,错误信息:${err}`);
1335
- });
1336
1333
  });
1337
1334
  ctx.command("sync.module <moduleTitle:string>", "同步指定模块", {
1338
1335
  authority: 2
1339
1336
  }).action(async ({ session }, moduleTitle) => {
1340
- if (!this.checkBotsReady(ctx)) {
1341
- return session.send("❌ Wiki 机器人未就绪,请检查登录配置或查看日志");
1337
+ if (!await this.ensureBotsReady(ctx, "同步模块")) {
1338
+ return "❌ Wiki 机器人未就绪,请检查登录配置或查看日志";
1342
1339
  }
1343
- await session.send(
1344
- `✅ 同步中,请前往控制台查看:${this.config.logsUrl}`
1345
- );
1346
- await syncSingleModule(
1347
- ctx.wikiBot.getGGBot(),
1348
- ctx.wikiBot.getBWikiBot(),
1349
- moduleTitle,
1350
- "sync-bot"
1351
- ).then(() => {
1352
- session.send(
1353
- `✅ 已尝试同步模块:${moduleTitle},请前往控制台查看:${this.config.logsUrl}`
1340
+ try {
1341
+ await syncSingleModule(
1342
+ ctx.wikiBot.getGGBot(),
1343
+ ctx.wikiBot.getBWikiBot(),
1344
+ moduleTitle,
1345
+ "sync-bot"
1354
1346
  );
1355
- }).catch((err) => {
1356
- session.send(`❌ 同步模块失败:${moduleTitle}`);
1357
- this.log.error(`错误信息:${err}`);
1358
- });
1347
+ return `✅ 已尝试同步模块:${moduleTitle},请前往控制台查看:${this.config.logsUrl}`;
1348
+ } catch (err) {
1349
+ logger.error(`同步模块 ${moduleTitle} 失败,错误信息:${err}`);
1350
+ return `❌ 同步模块失败:${moduleTitle}`;
1351
+ }
1359
1352
  });
1360
1353
  ctx.command("sync.allmodules", "同步所有模块", { authority: 2 }).action(async ({ session }) => {
1361
- if (!this.checkBotsReady(ctx)) {
1362
- 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}`;
1363
1363
  }
1364
- await session.send(
1365
- `🚀 开始同步所有模块,任务耗时较长,请前往控制台查看:${this.config.logsUrl}`
1366
- );
1367
- await syncModules(ctx.wikiBot.getGGBot(), ctx.wikiBot.getBWikiBot()).then(() => {
1368
- session.send(
1369
- `✅ 已尝试同步所有模块,请前往控制台查看:${this.config.logsUrl}`
1370
- );
1371
- }).catch((err) => {
1372
- session.send(
1373
- `❌ 同步所有模块失败,请前往控制台查看日志:${this.config.logsUrl}`
1374
- );
1375
- this.log.error(`同步所有模块失败,错误信息:${err}`);
1376
- });
1377
1364
  });
1378
1365
  ctx.command("sync.img <imgTitle:string>", "同步指定图片", { authority: 2 }).action(async ({ session }, imgTitle) => {
1379
- if (!this.checkBotsReady(ctx)) {
1380
- 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}`;
1381
1380
  }
1382
- await session.send(
1383
- `🚀 开始同步,任务可能耗时较长,请前往控制台查看:${this.config.logsUrl}`
1384
- );
1385
- await syncSingleImage(
1386
- ctx.wikiBot.getGGBot(),
1387
- ctx.wikiBot.getBWikiBot(),
1388
- `${imgTitle.startsWith("File:") ? "" : "File:"}${imgTitle}`,
1389
- this.config
1390
- ).then(() => {
1391
- session.send(`✅ 已尝试同步图片:${imgTitle}`);
1392
- }).catch((err) => {
1393
- session.send(`❌ 同步图片失败:${imgTitle}`);
1394
- this.log.error(`同步图片失败:${imgTitle},错误信息:${err}`);
1395
- });
1396
1381
  });
1397
1382
  ctx.command("sync.allimgs", "同步所有图片", { authority: 2 }).action(async ({ session }) => {
1398
- if (!this.checkBotsReady(ctx)) {
1399
- return session.send("❌ Wiki 机器人未就绪,请检查登录配置或查看日志");
1383
+ if (!await this.ensureBotsReady(ctx, "同步所有图片")) {
1384
+ return "❌ Wiki 机器人未就绪,请检查登录配置或查看日志";
1400
1385
  }
1401
- session.send(
1402
- `🚀 开始同步所有图片,任务耗时较长,请前往控制台查看:${this.config.logsUrl}`
1403
- );
1404
- await syncAllImages(
1405
- ctx.wikiBot.getGGBot(),
1406
- ctx.wikiBot.getBWikiBot(),
1407
- this.config
1408
- ).then(() => {
1409
- session.send(
1410
- `✅ 已尝试同步所有图片,请前往控制台查看:${this.config.logsUrl}`
1411
- );
1412
- }).catch((err) => {
1413
- session.send(
1414
- `❌ 同步所有图片失败,请前往控制台查看日志:${this.config.logsUrl}`
1386
+ try {
1387
+ await syncAllImages(
1388
+ ctx.wikiBot.getGGBot(),
1389
+ ctx.wikiBot.getBWikiBot(),
1390
+ this.config
1415
1391
  );
1416
- this.log.error(`同步所有图片失败,错误信息:${err}`);
1417
- });
1392
+ return `✅ 已尝试同步所有图片,请前往控制台查看:${this.config.logsUrl}`;
1393
+ } catch (err) {
1394
+ logger.error(`同步所有图片失败,错误信息:${err}`);
1395
+ return `❌ 同步所有图片失败,请前往控制台查看日志:${this.config.logsUrl}`;
1396
+ }
1418
1397
  });
1419
1398
  }
1420
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.2",
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 # 路由重定向