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.

72 lines
2.9 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.Message;
using Lagrange.Core.Message.Entity;
using RoBot.Core.ConstValue;
using RoBot.Core.Helper;
using RoBot.Start.Global;
using RoBot.Start.LogConfig;
using RoBot.Start.Message;
using RoBot.Start.Service.Dto;
namespace RoBot.Start.Cmd
{
/// <summary>
/// 查看物品信息命令 价格/数据更新时间
/// </summary>
public class QueryGoodsPriceCmd
{
public static async Task<bool> Execute(MessageChain chain)
{
try
{
var systemConfig = GlobalConfig.ConfigSetting;
var bot = GlobalConfig.BotContext;
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.ShowPriceDesc}\n收录时间{goodsInfo.LastUpdateTime:yyyy-MM-dd HH:mm:ss}");
}
else
{
await bot.SendMsg((uint)systemConfig.GroupQQ, "未收录");
}
}
else
{
await bot.SendMsg((uint)systemConfig.GroupQQ, "请输入要查询的物品名称,例如:查价格 七星草");
}
}
}
}
}
catch (Exception ex)
{
Logs.Write($@"{DateTime.Now:yyyy-MM-dd HH:mm:ss} 查看物品信息命令 发生异常,异常信息:{ex.Message},异常堆栈:{ex.StackTrace}");
}
return await Task.FromResult(true);
}
}
}