From d5061ee50b1c895784c8cf5d925d210c57f5141c Mon Sep 17 00:00:00 2001 From: longxuan Date: Thu, 25 Apr 2024 20:45:46 +0800 Subject: [PATCH] fix: save keystore after login success --- .../MiraiHttp/LagrangeBotBridge.cs | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/Mikibot.Analyze/MiraiHttp/LagrangeBotBridge.cs b/Mikibot.Analyze/MiraiHttp/LagrangeBotBridge.cs index f6b39de..32d0b91 100644 --- a/Mikibot.Analyze/MiraiHttp/LagrangeBotBridge.cs +++ b/Mikibot.Analyze/MiraiHttp/LagrangeBotBridge.cs @@ -31,6 +31,16 @@ public class LagrangeBotBridge(ILogger logger) : IMiraiServic await using var fileStream = File.OpenRead(BotLoginStore); return await JsonSerializer.DeserializeAsync(fileStream); } + private static async ValueTask SaveKeyStore(BotKeystore keystore) + { + if (File.Exists(BotLoginStore)) + { + File.Move(BotLoginStore, BotLoginStore + "-" + Guid.NewGuid()); + } + + await using var fileStream = File.OpenWrite(BotLoginStore); + await JsonSerializer.SerializeAsync(fileStream, keystore); + } private static async ValueTask GetDeviceInfo() { @@ -60,7 +70,6 @@ private async ValueTask WaitBotOnlineAsync(TimeSpan timeout) tcs.SetCanceled(cts.Token); bot.Invoker.OnBotOnlineEvent += InvokerOnOnBotOnlineEvent; - bot.Invoker.OnBotOfflineEvent += InvokerOnOnBotOfflineEvent; try { @@ -69,15 +78,9 @@ private async ValueTask WaitBotOnlineAsync(TimeSpan timeout) finally { bot.Invoker.OnBotOnlineEvent -= InvokerOnOnBotOnlineEvent; - bot.Invoker.OnBotOfflineEvent -= InvokerOnOnBotOfflineEvent; } return; - void InvokerOnOnBotOfflineEvent(BotContext context, BotOfflineEvent e) - { - tcs.SetCanceled(); - } - void InvokerOnOnBotOnlineEvent(BotContext context, BotOnlineEvent @event) { tcs.TrySetResult(); @@ -103,7 +106,8 @@ public async ValueTask Run() logger.LogInformation("等待登录中..."); await WaitBotOnlineAsync(TimeSpan.FromMinutes(2)); logger.LogInformation("登录完成"); - + + await SaveKeyStore(bot.UpdateKeystore()); } private static IEnumerable ConvertMessageToMiraiCore(MessageChain lagrange)