koishi-plugin-oni-sync-bot 0.8.8 → 0.9.0

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
@@ -503,6 +503,20 @@ var RouteRedirect = class {
503
503
  )}`;
504
504
  router.redirect(targetUrl);
505
505
  });
506
+ ctx.server.use("/ggwiki", async (router, next) => {
507
+ const fullPath = router.path.replace("/ggwiki", "") || "";
508
+ const decodedPath = decodeURIComponent(fullPath);
509
+ if (decodedPath.includes("..")) {
510
+ return router.body = "❌ 非法路径访问!";
511
+ }
512
+ if (!/^(\/[\p{L}\p{N}_\-:.%]+)*\/?$/u.test(fullPath)) {
513
+ return router.body = "❌ 路径包含非法字符!";
514
+ }
515
+ const queryString = router.querystring ? `?${router.querystring}` : "";
516
+ const targetUrl = `https://${this.config.main_site}${fullPath}${queryString}`;
517
+ router.redirect(targetUrl);
518
+ return;
519
+ });
506
520
  }
507
521
  };
508
522
  ((RouteRedirect2) => {
@@ -1839,45 +1853,65 @@ bwiki: https://${this.config.domain}/bw/${match.id}`;
1839
1853
  // src/plugins/updateCommands.ts
1840
1854
  var import_koishi10 = require("koishi");
1841
1855
  var UpdateCommands = class {
1856
+ constructor(ctx, config) {
1857
+ this.ctx = ctx;
1858
+ this.config = config;
1859
+ this.log = ctx.logger("oni-sync");
1860
+ this.registerCommands(ctx);
1861
+ this.registerCronJobs(ctx);
1862
+ }
1842
1863
  static {
1843
1864
  __name(this, "UpdateCommands");
1844
1865
  }
1845
- static inject = ["database", "wikiBot"];
1866
+ static inject = ["database", "wikiBot", "cron"];
1846
1867
  config;
1847
1868
  log;
1848
- constructor(ctx, config) {
1849
- this.config = config;
1850
- this.log = ctx.logger("oni-sync");
1851
- this.registerCommands(ctx);
1869
+ registerCronJobs(ctx) {
1870
+ const cronExpression = this.config.updateCron || "0 2 * * *";
1871
+ ctx.cron(cronExpression, async () => {
1872
+ if (!this.ctx.wikiBot.isReady) {
1873
+ this.log.warn("定时任务:WikiBot 服务未就绪,跳过页面缓存更新");
1874
+ return;
1875
+ }
1876
+ this.log.info("定时任务:开始每日自动更新本地页面缓存(主站)");
1877
+ try {
1878
+ const result = await this.updatePageCache();
1879
+ this.log.info(`定时任务完成:更新 ${result.count} 条页面到本地缓存`);
1880
+ } catch (err) {
1881
+ this.log.error("定时任务失败:自动更新页面缓存", err);
1882
+ }
1883
+ });
1884
+ }
1885
+ async updatePageCache() {
1886
+ const res = await this.ctx.wikiBot.getGGBot().request({
1887
+ action: "query",
1888
+ list: "allpages",
1889
+ format: "json",
1890
+ aplimit: "max"
1891
+ });
1892
+ logger.info("主站页面查询成功");
1893
+ const pages = res.query.allpages || [];
1894
+ const pageData = pages.map((page) => {
1895
+ const { pinyin_full, pinyin_first } = generatePinyinInfo(page.title);
1896
+ return {
1897
+ id: page.pageid,
1898
+ title: page.title,
1899
+ pinyin_full,
1900
+ pinyin_first
1901
+ };
1902
+ });
1903
+ if (pageData.length > 0) {
1904
+ await this.ctx.database.upsert("wikipages", pageData);
1905
+ }
1906
+ logger.info(`检索到 ${pages.length} 个页面,已更新至数据库`);
1907
+ return { count: pages.length };
1852
1908
  }
1853
1909
  registerCommands(ctx) {
1854
1910
  ctx.command("update", "更新本地页面缓存(主站)", { authority: 2 }).action(async ({ session }) => {
1855
1911
  await session.execute("update.status");
1856
1912
  try {
1857
- const res = await ctx.wikiBot.getGGBot().request({
1858
- action: "query",
1859
- list: "allpages",
1860
- format: "json",
1861
- aplimit: "max"
1862
- });
1863
- logger.info("主站页面查询成功");
1864
- const pages = res.query.allpages || [];
1865
- const pageData = pages.map((page) => {
1866
- const { pinyin_full, pinyin_first } = generatePinyinInfo(
1867
- page.title
1868
- );
1869
- return {
1870
- id: page.pageid,
1871
- title: page.title,
1872
- pinyin_full,
1873
- pinyin_first
1874
- };
1875
- });
1876
- if (pageData.length > 0) {
1877
- await ctx.database.upsert("wikipages", pageData);
1878
- }
1879
- session.send(`✅ 检索到 ${pages.length} 个页面,已更新至数据库`);
1880
- logger.info(`检索到 ${pages.length} 个页面,已更新至数据库`);
1913
+ const result = await this.updatePageCache();
1914
+ session.send(`✅ 检索到 ${result.count} 个页面,已更新至数据库`);
1881
1915
  } catch (err) {
1882
1916
  this.log.error("主站缓存更新失败", err);
1883
1917
  session.send("❌ 主站缓存更新失败,请联系管理员查看日志");
@@ -1950,7 +1984,8 @@ var UpdateCommands = class {
1950
1984
  };
1951
1985
  ((UpdateCommands2) => {
1952
1986
  UpdateCommands2.Config = import_koishi10.Schema.object({
1953
- logsUrl: import_koishi10.Schema.string().description("日志查看地址").default("https://klei.vip/onilogs")
1987
+ logsUrl: import_koishi10.Schema.string().description("日志查看地址").default("https://klei.vip/onilogs"),
1988
+ updateCron: import_koishi10.Schema.string().description("页面缓存自动更新 cron 表达式(默认每天凌晨 2 点)").default("0 2 * * *")
1954
1989
  });
1955
1990
  })(UpdateCommands || (UpdateCommands = {}));
1956
1991
 
@@ -1,12 +1,16 @@
1
1
  import { Context, Logger, Schema } from "koishi";
2
2
  export interface UpdateCommandsConfig {
3
3
  logsUrl: string;
4
+ updateCron?: string;
4
5
  }
5
6
  export declare class UpdateCommands {
7
+ ctx: Context;
6
8
  static readonly inject: string[];
7
9
  config: UpdateCommandsConfig;
8
10
  log: Logger;
9
11
  constructor(ctx: Context, config: UpdateCommandsConfig);
12
+ private registerCronJobs;
13
+ private updatePageCache;
10
14
  private registerCommands;
11
15
  }
12
16
  export declare namespace UpdateCommands {
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.8",
4
+ "version": "0.9.0",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
@@ -30,10 +30,10 @@
30
30
  },
31
31
  "dependencies": {
32
32
  "@types/node-fetch": "^2.6.13",
33
- "form-data": "^4.0.5",
33
+ "form-data": "^4.0.6",
34
34
  "koishi-plugin-cron": "^3.1.0",
35
- "mwn": "^3.0.1",
35
+ "mwn": "^3.0.3",
36
36
  "node-fetch": "^3.3.2",
37
- "pinyin-pro": "^3.28.0"
37
+ "pinyin-pro": "^3.28.1"
38
38
  }
39
39
  }