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.

169 lines
5.1 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 Lagrange.Core.Message.Entity;
using RoBot.Core.ConstValue;
using RoBot.Core.Redis;
using RoBot.Goods.Dto;
using RoBot.Goods.Service;
using RoBot.Start.Device;
using RoBot.Start.Dto.ConfigDto;
using RoBot.Start.Keystore;
using RoBot.Start.LogConfig;
using RoBot.Start.Message;
using RoBot.Start.RoBotConfig;
new RedisHelper();
// 登录状态
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());
}
};
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();
}
async Task<bool> HandleGroupMessage(MessageChain chain)
{
// 判断是否是“@消息 + 文本”
if (chain.Count == 2 &&
chain.FirstOrDefault(e => e is MentionEntity) is MentionEntity mention &&
chain.FirstOrDefault(e => e is TextEntity) is TextEntity text)
{
// 判断是否@了机器人
if (mention.Uin == systemConfig.BotQQ)
{
string inputText = text.Text?.Trim();
// 判断是否包含“查价格”
if (!string.IsNullOrEmpty(inputText) && inputText.Contains("查价格"))
{
// 提取物品名称
var goodsName = inputText
.Split(' ', StringSplitOptions.RemoveEmptyEntries)
.FirstOrDefault(word => word != "查价格")
?.Replace("查价格", "")?.Trim();
if (!string.IsNullOrWhiteSpace(goodsName))
{
var goodsInfo = RedisHelper.Client.HGet<GoodsInfo>(RedisPrefix.GoodsKey, goodsName);
if (goodsInfo is not null)
{
await bot.SendMsg((uint)systemConfig.GroupQQ,
$"物品:{goodsInfo.Name}\n价格{goodsInfo.Price}万\n收录时间{goodsInfo.LastUpdateTime:yyyy-MM-dd HH:mm:ss}");
}
else
{
await bot.SendMsg((uint)systemConfig.GroupQQ, "未收录");
}
}
else
{
await bot.SendMsg((uint)systemConfig.GroupQQ, "请输入要查询的物品名称,例如:查价格 七星草");
}
}
}
}
foreach (var item in chain)
{
// 艾特消息
if (item is MentionEntity me)
{
Logs.Write($"提及用户的 Uin {me.Uin} 提及用户的 Uid {me.Uid} 提及用户的昵称{me.Name}");
}
// 文本消息
else if (item is TextEntity)
{
TextEntity txe = item as TextEntity;
if (string.IsNullOrWhiteSpace(txe.Text) is false)
{
Logs.Write(txe.Text);
if (txe.Text.Contains("不鼓励不保障"))
{
GoodsService.AnalysisGoodsText(txe.Text);
}
}
}
// 回复消息
else if (item is ForwardEntity)
{
ForwardEntity frd = item as ForwardEntity;
Logs.Write($"回复的目标消息 Seq {frd.Sequence} 发送者 Uid {frd.Uid} 回复的目标消息的发送者 Uin{frd.TargetUin}");
}
// 合并转发消息
else if (item is MultiMsgEntity)
{
MultiMsgEntity mme = item as MultiMsgEntity;
//Logs.Write($"回复的目标消息 Seq {frd.Sequence} 发送者 Uid {frd.Uid} 回复的目标消息的发送者 Uin{frd.TargetUin}");
}
}
return true;
}