koishi-plugin-bind-bot 2.2.0 → 2.2.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 +35 -15
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -723,30 +723,45 @@ function apply(ctx, config) {
723
723
  updateData.reminderCount = 0;
724
724
  needUpdate = true;
725
725
  }
726
- // 检查并添加hasMcBind字段(数据迁移:根据 mcUsername 判断)
726
+ // 检查并修复hasMcBind字段(数据迁移 + 数据一致性检查)
727
727
  const currentHasMcBind = record.hasMcBind;
728
+ const mcUsername = record.mcUsername;
729
+ const hasValidMc = !!(mcUsername && !mcUsername.startsWith('_temp_'));
730
+ // 情况1:字段不存在,需要添加
728
731
  if (currentHasMcBind === undefined || currentHasMcBind === null) {
729
- // 有有效的MC用户名(非空且不是_temp_开头)则认为已绑定
730
- const mcUsername = record.mcUsername;
731
- const hasValidMc = !!(mcUsername && !mcUsername.startsWith('_temp_'));
732
732
  updateData.hasMcBind = hasValidMc;
733
- // 同时清空临时用户名,保持数据一致性
734
- if (!hasValidMc && mcUsername && mcUsername.startsWith('_temp_')) {
735
- updateData.mcUsername = '';
736
- updateData.mcUuid = '';
737
- updateData.whitelist = [];
738
- logger.info(`[数据迁移] 清理QQ(${qqId})的临时用户名: ${mcUsername}`);
739
- }
740
733
  needUpdate = true;
734
+ logger.debug(`[数据迁移] 添加hasMcBind字段 QQ(${qqId}): ${hasValidMc}`);
735
+ }
736
+ // 情况2:字段存在但值不正确,需要修复
737
+ else if (currentHasMcBind !== hasValidMc) {
738
+ updateData.hasMcBind = hasValidMc;
739
+ needUpdate = true;
740
+ logger.info(`[数据修复] 修正hasMcBind QQ(${qqId}): ${currentHasMcBind} -> ${hasValidMc}`);
741
741
  }
742
- // 检查并添加hasBuidBind字段(数据迁移:根据 buidUid 判断)
742
+ // 清理临时用户名(无论hasMcBind字段是否存在)
743
+ if (!hasValidMc && mcUsername && mcUsername.startsWith('_temp_')) {
744
+ updateData.mcUsername = '';
745
+ updateData.mcUuid = '';
746
+ updateData.whitelist = [];
747
+ needUpdate = true;
748
+ logger.info(`[数据清理] 清理QQ(${qqId})的临时用户名: ${mcUsername}`);
749
+ }
750
+ // 检查并修复hasBuidBind字段(数据迁移 + 数据一致性检查)
743
751
  const currentHasBuidBind = record.hasBuidBind;
752
+ const buidUid = record.buidUid;
753
+ const hasValidBuid = !!(buidUid && buidUid.length > 0);
754
+ // 情况1:字段不存在,需要添加
744
755
  if (currentHasBuidBind === undefined || currentHasBuidBind === null) {
745
- // 有有效的B站UID(非空)则认为已绑定
746
- const buidUid = record.buidUid;
747
- const hasValidBuid = !!(buidUid && buidUid.length > 0);
748
756
  updateData.hasBuidBind = hasValidBuid;
749
757
  needUpdate = true;
758
+ logger.debug(`[数据迁移] 添加hasBuidBind字段 QQ(${qqId}): ${hasValidBuid}`);
759
+ }
760
+ // 情况2:字段存在但值不正确,需要修复
761
+ else if (currentHasBuidBind !== hasValidBuid) {
762
+ updateData.hasBuidBind = hasValidBuid;
763
+ needUpdate = true;
764
+ logger.info(`[数据修复] 修正hasBuidBind QQ(${qqId}): ${currentHasBuidBind} -> ${hasValidBuid}`);
750
765
  }
751
766
  // 如果需要更新,执行更新操作
752
767
  if (needUpdate) {
@@ -767,6 +782,11 @@ function apply(ctx, config) {
767
782
  return false;
768
783
  }
769
784
  };
785
+ // 在插件启动时执行数据迁移(放在函数定义之后)
786
+ ctx.on('ready', async () => {
787
+ logger.info('[初始化] 开始数据迁移和一致性检查...');
788
+ await addMissingFields();
789
+ });
770
790
  // 重建MCIDBIND表
771
791
  const rebuildMcidBindTable = async () => {
772
792
  try {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-bind-bot",
3
3
  "description": "[WittF自用] BIND-BOT - 账号绑定管理机器人,支持Minecraft账号和B站账号绑定与管理。",
4
- "version": "2.2.0",
4
+ "version": "2.2.2",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [