Skip to content

Commit

Permalink
fix: save keystore after login success
Browse files Browse the repository at this point in the history
  • Loading branch information
Deliay committed Apr 25, 2024
1 parent c66bc79 commit d5061ee
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions Mikibot.Analyze/MiraiHttp/LagrangeBotBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ public class LagrangeBotBridge(ILogger<LagrangeBotBridge> logger) : IMiraiServic
await using var fileStream = File.OpenRead(BotLoginStore);
return await JsonSerializer.DeserializeAsync<BotKeystore>(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<BotDeviceInfo> GetDeviceInfo()
{
Expand Down Expand Up @@ -60,7 +70,6 @@ private async ValueTask WaitBotOnlineAsync(TimeSpan timeout)

tcs.SetCanceled(cts.Token);
bot.Invoker.OnBotOnlineEvent += InvokerOnOnBotOnlineEvent;
bot.Invoker.OnBotOfflineEvent += InvokerOnOnBotOfflineEvent;

try
{
Expand All @@ -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();
Expand All @@ -103,7 +106,8 @@ public async ValueTask Run()
logger.LogInformation("等待登录中...");
await WaitBotOnlineAsync(TimeSpan.FromMinutes(2));
logger.LogInformation("登录完成");


await SaveKeyStore(bot.UpdateKeystore());
}

private static IEnumerable<MessageBase> ConvertMessageToMiraiCore(MessageChain lagrange)
Expand Down

0 comments on commit d5061ee

Please sign in to comment.