@@ -477,6 +477,12 @@ func postBotCreate(c *gin.Context) {
477477 respondError (c , http .StatusInternalServerError , "error.config_save_failed" , err )
478478 return
479479 }
480+ last := & cfg .Bots [len (cfg .Bots )- 1 ]
481+ if err := syncBotConfigSnapshotFromMainBot (botID , last , "post_bot_create" ); err != nil {
482+ logger .Error ("同步 bot_configs 失敗 (post_bot_create %s): %v" , botID , err )
483+ respondError (c , http .StatusInternalServerError , "error.config_save_failed" , err )
484+ return
485+ }
480486 c .JSON (http .StatusOK , gin.H {"ok" : true , "bot_id" : botID })
481487}
482488
@@ -622,13 +628,31 @@ func deleteBot(c *gin.Context) {
622628 respondError (c , http .StatusInternalServerError , "error.config_save_failed" , err )
623629 return
624630 }
631+ removeBotConfigSnapshotBestEffort (botID )
625632 if botManagerProvider != nil {
626633 _ = botManagerProvider .StopBot (botID )
627634 }
628635 logger .Info ("✅ [Bot刪除] 已移除 %s" , botID )
629636 c .JSON (http .StatusOK , gin.H {"ok" : true , "bot_id" : botID })
630637}
631638
639+ // botCfgByID 在主配置快照中按 ID 查找 Bot(與 GenerateBotID 回退規則一致)。
640+ func botCfgByID (cfg * config.Config , botID string ) * config.BotConfig {
641+ if cfg == nil || botID == "" {
642+ return nil
643+ }
644+ for i := range cfg .Bots {
645+ id := cfg .Bots [i ].ID
646+ if id == "" {
647+ id = config .GenerateBotID (cfg .Bots [i ].Exchange , cfg .Bots [i ].Symbol , cfg .Bots [i ].GetMarketType ())
648+ }
649+ if id == botID {
650+ return & cfg .Bots [i ]
651+ }
652+ }
653+ return nil
654+ }
655+
632656// postBotStop 停止 Bot
633657// POST /api/bots/:id/stop
634658func postBotStop (c * gin.Context ) {
@@ -1072,6 +1096,16 @@ func postBotGroupCreate(c *gin.Context) {
10721096 respondError (c , http .StatusInternalServerError , "error.config_save_failed" , err )
10731097 return
10741098 }
1099+ if err := syncBotConfigSnapshotFromMainBot (spotID , botCfgByID (cfg , spotID ), "post_bot_group_create" ); err != nil {
1100+ logger .Error ("同步 bot_configs 失敗 (spot %s): %v" , spotID , err )
1101+ respondError (c , http .StatusInternalServerError , "error.config_save_failed" , err )
1102+ return
1103+ }
1104+ if err := syncBotConfigSnapshotFromMainBot (futuresID , botCfgByID (cfg , futuresID ), "post_bot_group_create" ); err != nil {
1105+ logger .Error ("同步 bot_configs 失敗 (futures %s): %v" , futuresID , err )
1106+ respondError (c , http .StatusInternalServerError , "error.config_save_failed" , err )
1107+ return
1108+ }
10751109 c .JSON (http .StatusOK , gin.H {"ok" : true , "group_id" : groupID , "bot_ids" : []string {futuresID , spotID }})
10761110}
10771111
@@ -1158,6 +1192,9 @@ func deleteBotGroup(c *gin.Context) {
11581192 respondError (c , http .StatusInternalServerError , "error.config_save_failed" , err )
11591193 return
11601194 }
1195+ for _ , id := range botIDsToRemove {
1196+ removeBotConfigSnapshotBestEffort (id )
1197+ }
11611198 c .JSON (http .StatusOK , gin.H {"ok" : true , "group_id" : groupID })
11621199}
11631200
@@ -1349,7 +1386,7 @@ func putBotStrategy(c *gin.Context) {
13491386 if id != botID {
13501387 continue
13511388 }
1352- if err := syncBotConfigSnapshotFromMainBot (botID , & cfg .Bots [i ]); err != nil {
1389+ if err := syncBotConfigSnapshotFromMainBot (botID , & cfg .Bots [i ], "put_bot_strategy" ); err != nil {
13531390 logger .Error ("同步 bot_configs 失敗 (bot_id=%s): %v" , botID , err )
13541391 respondError (c , http .StatusInternalServerError , "error.config_save_failed" , err )
13551392 return
0 commit comments