koishi-plugin-bind-bot 2.2.0 → 2.2.1
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 +35 -15
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -670,6 +670,11 @@ function apply(ctx, config) {
|
|
|
670
670
|
// 添加isAdmin索引,提高查询效率
|
|
671
671
|
indexes: [['isAdmin'], ['buidUid']]
|
|
672
672
|
});
|
|
673
|
+
// 在插件启动时执行数据迁移
|
|
674
|
+
ctx.on('ready', async () => {
|
|
675
|
+
logger.info('[初始化] 开始数据迁移和一致性检查...');
|
|
676
|
+
await addMissingFields();
|
|
677
|
+
});
|
|
673
678
|
// 检查表结构是否包含旧字段
|
|
674
679
|
const checkTableStructure = async () => {
|
|
675
680
|
try {
|
|
@@ -723,30 +728,45 @@ function apply(ctx, config) {
|
|
|
723
728
|
updateData.reminderCount = 0;
|
|
724
729
|
needUpdate = true;
|
|
725
730
|
}
|
|
726
|
-
//
|
|
731
|
+
// 检查并修复hasMcBind字段(数据迁移 + 数据一致性检查)
|
|
727
732
|
const currentHasMcBind = record.hasMcBind;
|
|
733
|
+
const mcUsername = record.mcUsername;
|
|
734
|
+
const hasValidMc = !!(mcUsername && !mcUsername.startsWith('_temp_'));
|
|
735
|
+
// 情况1:字段不存在,需要添加
|
|
728
736
|
if (currentHasMcBind === undefined || currentHasMcBind === null) {
|
|
729
|
-
// 有有效的MC用户名(非空且不是_temp_开头)则认为已绑定
|
|
730
|
-
const mcUsername = record.mcUsername;
|
|
731
|
-
const hasValidMc = !!(mcUsername && !mcUsername.startsWith('_temp_'));
|
|
732
737
|
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
738
|
needUpdate = true;
|
|
739
|
+
logger.debug(`[数据迁移] 添加hasMcBind字段 QQ(${qqId}): ${hasValidMc}`);
|
|
740
|
+
}
|
|
741
|
+
// 情况2:字段存在但值不正确,需要修复
|
|
742
|
+
else if (currentHasMcBind !== hasValidMc) {
|
|
743
|
+
updateData.hasMcBind = hasValidMc;
|
|
744
|
+
needUpdate = true;
|
|
745
|
+
logger.info(`[数据修复] 修正hasMcBind QQ(${qqId}): ${currentHasMcBind} -> ${hasValidMc}`);
|
|
741
746
|
}
|
|
742
|
-
//
|
|
747
|
+
// 清理临时用户名(无论hasMcBind字段是否存在)
|
|
748
|
+
if (!hasValidMc && mcUsername && mcUsername.startsWith('_temp_')) {
|
|
749
|
+
updateData.mcUsername = '';
|
|
750
|
+
updateData.mcUuid = '';
|
|
751
|
+
updateData.whitelist = [];
|
|
752
|
+
needUpdate = true;
|
|
753
|
+
logger.info(`[数据清理] 清理QQ(${qqId})的临时用户名: ${mcUsername}`);
|
|
754
|
+
}
|
|
755
|
+
// 检查并修复hasBuidBind字段(数据迁移 + 数据一致性检查)
|
|
743
756
|
const currentHasBuidBind = record.hasBuidBind;
|
|
757
|
+
const buidUid = record.buidUid;
|
|
758
|
+
const hasValidBuid = !!(buidUid && buidUid.length > 0);
|
|
759
|
+
// 情况1:字段不存在,需要添加
|
|
744
760
|
if (currentHasBuidBind === undefined || currentHasBuidBind === null) {
|
|
745
|
-
// 有有效的B站UID(非空)则认为已绑定
|
|
746
|
-
const buidUid = record.buidUid;
|
|
747
|
-
const hasValidBuid = !!(buidUid && buidUid.length > 0);
|
|
748
761
|
updateData.hasBuidBind = hasValidBuid;
|
|
749
762
|
needUpdate = true;
|
|
763
|
+
logger.debug(`[数据迁移] 添加hasBuidBind字段 QQ(${qqId}): ${hasValidBuid}`);
|
|
764
|
+
}
|
|
765
|
+
// 情况2:字段存在但值不正确,需要修复
|
|
766
|
+
else if (currentHasBuidBind !== hasValidBuid) {
|
|
767
|
+
updateData.hasBuidBind = hasValidBuid;
|
|
768
|
+
needUpdate = true;
|
|
769
|
+
logger.info(`[数据修复] 修正hasBuidBind QQ(${qqId}): ${currentHasBuidBind} -> ${hasValidBuid}`);
|
|
750
770
|
}
|
|
751
771
|
// 如果需要更新,执行更新操作
|
|
752
772
|
if (needUpdate) {
|