You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.0 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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 += (sender, e) =>
{
Task.Run(() =>
{
if (e.Chain.GroupUin == systemConfig.GroupQQ)
{
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;
Logs.Write($"Login success: {bot.BotUin} {bot.BotName} {DateTime.Now}");
_ = 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();
}
void HandleGroupMessage(MessageChain chain)
{
// 悬赏令查价
_ = WantedPriceCmd.Execute(chain);
// 坊市生成上架命令
_ = GoodsUpShopCmd.Execute(chain);
// 保存或更新物品价格
_ = InertOrUpdateGoodsInfoCmd.Execute(chain);
// 查看物品信息命令 价格/数据更新时间
_ = QueryGoodsPriceCmd.Execute(chain);
// 灵田结算价格
_ = LingTianCmd.Execute(chain);
// 妖塔猜成语
_ = ChineseIdiomsCmd.Execute(chain);
// 查丹方
_ = ImmortalElixirCmd.Execute(chain);
}
void Init()
{
_ = new RedisHelper();
}
void LoadLoginConfig()
{
GlobalConfig.ConfigSetting = systemConfig;
GlobalConfig.KeystoreInfo = _keyStore;
GlobalConfig.BotDeviceInfo = _deviceInfo;
GlobalConfig.BotContext = bot;
}