using Lagrange.Core.Common; using Lagrange.Core.Common.Interface; using Lagrange.Core.Common.Interface.Api; using Lagrange.Core.Message; using RoBot.Core.Helper; using RoBot.Start.Cmd; using RoBot.Start.Device; using RoBot.Start.Dto.ConfigDto; using RoBot.Start.Global; using RoBot.Start.Keystore; using RoBot.Start.LogConfig; using RoBot.Start.RoBotConfig; Init(); // 登录状态 bool LoginSuccess = false; // 重新登录 bool ReLogin = false; bool RecordLog = true; ConfigSetting systemConfig = await RoBotsConfig.GetConfig(); if (systemConfig.BotQQ is null || systemConfig.BotQQ is null || systemConfig.XiaoXiaoQQ is null) { Logs.Write("请在配置文件中配置所有必要参数", true); Console.ReadLine(); return; } BotDeviceInfo _deviceInfo = await DeviceConfig.GetDeviceInfo(); KeystoreInfo _keyStore = await KeystoreConfig.GetBotKeystore(); ReLogin = _keyStore.ReLogin; Lagrange.Core.BotContext bot = BotFactory.Create(new BotConfig(), _deviceInfo, _keyStore.BotKeystore); bot.Invoker.OnGroupMessageReceived += async (sender, e) => { if (e.Chain.GroupUin == systemConfig.GroupQQ) { await HandleGroupMessage(e.Chain); } }; bot.Invoker.OnBotLogEvent += (sender, e) => { if (RecordLog) Logs.Write($"{e.EventMessage}\n{e.Level}\n{e.Tag}"); if (e.EventMessage.Contains("Login Success") || e.EventMessage.Contains("AutoReLogin Enabled")) { RecordLog = false; LoginSuccess = true; _ = KeystoreConfig.SaveBotKeystore(bot.UpdateKeystore()); LoadLoginConfig(); } }; if ((args.Length > 0 && args[0] == "qr") || !ReLogin) { var qrCode = await bot.FetchQrCode(); if (!qrCode.HasValue) { Logs.Write($"Error: qrCode is null"); return; } { using var qr = File.Create("qr.png"); await qr.WriteAsync(qrCode.Value.QrCode.AsMemory(0, qrCode.Value.QrCode.Length)); qr.Close(); } Logs.Write("二维码已经生成,扫描当前目录下的qr.png文件"); await bot.LoginByQrCode(); File.Delete("qr.png"); } else { await bot.LoginByPassword(); LoadLoginConfig(); } async Task HandleGroupMessage(MessageChain chain) { // 坊市生成上架命令 await QueryGoodsUpShopPriceCmd.Execute(chain); // 保存或更新物品价格 await InertOrUpdateGoodsCmd.Execute(chain); // 查看物品信息命令 价格/数据更新时间 await QueryGoodsInfoPriceCmd.Execute(chain); return true; } void Init() { _ = new RedisHelper(); } void LoadLoginConfig() { GlobalConfig.ConfigSetting = systemConfig; GlobalConfig.KeystoreInfo = _keyStore; GlobalConfig.BotDeviceInfo = _deviceInfo; GlobalConfig.BotContext = bot; }