koishi-plugin-wordpress-notifier 2.4.1 → 2.4.2

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.
Files changed (2) hide show
  1. package/lib/index.js +49 -41
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -171,16 +171,32 @@ function apply(ctx, config) {
171
171
  });
172
172
  }
173
173
  async function updatePostUpdateRecord(postId, modifiedDate) {
174
- const record = await getPostUpdateRecord(postId);
175
- if (record) {
176
- // Koishi database API 不支持 update 方法,使用 remove + create 代替
177
- await ctx.database.remove('wordpress_post_updates', { postId });
178
- }
179
- await ctx.database.create('wordpress_post_updates', {
180
- postId,
181
- lastModified: modifiedDate,
182
- pushedAt: new Date()
183
- });
174
+ try {
175
+ ctx.logger.info(`开始更新文章更新记录,文章 ID: ${postId},修改时间: ${modifiedDate}`);
176
+ const record = await getPostUpdateRecord(postId);
177
+ if (record) {
178
+ ctx.logger.info(`发现现有记录,文章 ID: ${postId},上次修改时间: ${record.lastModified}`);
179
+ // Koishi database API 不支持 update 方法,使用 remove + create 代替
180
+ await ctx.database.remove('wordpress_post_updates', { postId });
181
+ ctx.logger.info(`已删除旧记录,文章 ID: ${postId}`);
182
+ }
183
+ // 创建新记录
184
+ const newRecord = {
185
+ postId,
186
+ lastModified: modifiedDate,
187
+ pushedAt: new Date()
188
+ };
189
+ ctx.logger.info(`准备创建新记录,文章 ID: ${postId},记录内容: ${JSON.stringify(newRecord)}`);
190
+ await ctx.database.create('wordpress_post_updates', newRecord);
191
+ ctx.logger.info(`已成功更新文章更新记录,文章 ID: ${postId}`);
192
+ }
193
+ catch (error) {
194
+ const errorMessage = error instanceof Error ? error.message : String(error);
195
+ ctx.logger.error(`更新文章更新记录失败,文章 ID: ${postId}`);
196
+ ctx.logger.error(`错误信息: ${errorMessage}`);
197
+ ctx.logger.error(`错误栈: ${error instanceof Error ? error.stack : '无'}`);
198
+ throw error;
199
+ }
184
200
  }
185
201
  async function markGroupAsPushed(groupId, postId, isUpdate) {
186
202
  try {
@@ -333,7 +349,9 @@ function apply(ctx, config) {
333
349
  ctx.logger.error('没有可用的 Bot 实例');
334
350
  return;
335
351
  }
336
- ctx.logger.info(`使用 bot ${bot.platform}:${bot.selfId} 进行推送`);
352
+ // 修复 Bot 标识 undefined 问题
353
+ const botId = bot.selfId || 'unknown';
354
+ ctx.logger.info(`使用 bot ${bot.platform}:${botId} 进行推送`);
337
355
  // 推送新文章
338
356
  if (config.enableAutoPush) {
339
357
  const posts = await fetchLatestPosts();
@@ -382,14 +400,9 @@ function apply(ctx, config) {
382
400
  if (!updateRecord || postModifiedDate > new Date(updateRecord.lastModified)) {
383
401
  for (const target of config.targets) {
384
402
  try {
385
- // 验证目标格式,确保是有效的数字字符串
386
- const numericTarget = Number(target);
387
- if (isNaN(numericTarget)) {
388
- ctx.logger.error(`无效的目标 ${target},必须是数字类型`);
389
- continue;
390
- }
391
- // 保持字符串类型,但确保内容是有效的数字格式
392
- const stringTarget = numericTarget.toString();
403
+ ctx.logger.info(`正在处理目标: ${target}`);
404
+ // 直接使用原始目标字符串,与新文章推送逻辑保持一致
405
+ const stringTarget = target;
393
406
  // 检查该群是否已推送过此文章
394
407
  if (await isGroupPushed(stringTarget, post.id)) {
395
408
  const segments = formatPostMessage(post, true, true);
@@ -419,14 +432,9 @@ function apply(ctx, config) {
419
432
  if (!(await isUserPushed(user.id))) {
420
433
  for (const target of config.targets) {
421
434
  try {
422
- // 验证目标格式,确保是有效的数字字符串
423
- const numericTarget = Number(target);
424
- if (isNaN(numericTarget)) {
425
- ctx.logger.error(`无效的目标 ${target},必须是数字类型`);
426
- continue;
427
- }
428
- // 保持字符串类型,但确保内容是有效的数字格式
429
- const stringTarget = numericTarget.toString();
435
+ ctx.logger.info(`正在处理目标: ${target}`);
436
+ // 直接使用原始目标字符串,与新文章推送逻辑保持一致
437
+ const stringTarget = target;
430
438
  const segments = formatUserMessage(user, true);
431
439
  ctx.logger.info(`准备推送新用户到目标: ${stringTarget}`);
432
440
  await bot.sendMessage(stringTarget, segments);
@@ -666,20 +674,20 @@ ${targetText}
666
674
  ctx.command('wordpress', 'WordPress 推送插件菜单')
667
675
  .action(() => {
668
676
  ctx.logger.info('命令 wordpress 被调用');
669
- return `📚 WordPress 推送插件菜单:
670
-
671
- 🔹 /wordpress.status - 查看插件状态
672
- 🔹 /wordpress.latest - 查看最新文章
673
- 🔹 /wordpress.list - 查看文章列表
674
- 🔹 /wordpress.push - 手动推送最新文章
675
- 🔹 /wordpress.set-url <url> - 修改 WordPress 站点地址
676
- 🔹 /wordpress.pushed - 查看已推送文章列表
677
- 🔹 /wordpress.clean [days] - 清理旧推送记录
678
- 🔹 /wordpress.toggle - 切换自动推送开关(仅超级管理员)
679
- 🔹 /wordpress.toggle-update - 切换文章更新推送开关(仅超级管理员)
680
- 🔹 /wordpress.toggle-user - 切换新用户注册推送开关(仅超级管理员)
681
- 🔹 /wordpress.mention - 切换 @全体成员 开关(仅超级管理员)
682
-
677
+ return `📚 WordPress 推送插件菜单:
678
+
679
+ 🔹 /wordpress.status - 查看插件状态
680
+ 🔹 /wordpress.latest - 查看最新文章
681
+ 🔹 /wordpress.list - 查看文章列表
682
+ 🔹 /wordpress.push - 手动推送最新文章
683
+ 🔹 /wordpress.set-url <url> - 修改 WordPress 站点地址
684
+ 🔹 /wordpress.pushed - 查看已推送文章列表
685
+ 🔹 /wordpress.clean [days] - 清理旧推送记录
686
+ 🔹 /wordpress.toggle - 切换自动推送开关(仅超级管理员)
687
+ 🔹 /wordpress.toggle-update - 切换文章更新推送开关(仅超级管理员)
688
+ 🔹 /wordpress.toggle-user - 切换新用户注册推送开关(仅超级管理员)
689
+ 🔹 /wordpress.mention - 切换 @全体成员 开关(仅超级管理员)
690
+
683
691
  💡 提示:所有命令都需要加 / 前缀`;
684
692
  });
685
693
  ctx.on('ready', async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-wordpress-notifier",
3
- "version": "2.4.1",
3
+ "version": "2.4.2",
4
4
  "description": "WordPress 文章自动推送到 QQ",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",