Skip to content

Commit

Permalink
feat(qqstatplugin): 移除Groups,收集全部群,发送图表优化
Browse files Browse the repository at this point in the history
  • Loading branch information
yiyungent committed Apr 18, 2022
1 parent 40182b4 commit 727adbd
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 168 deletions.
63 changes: 18 additions & 45 deletions plugins/QQStatPlugin/Controllers/CalendarController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class CalendarController : ControllerBase

[Route("/Plugins/QQStatPlugin/Calendar")]
[HttpGet]
public async Task<ActionResult> Get([FromQuery] string memeberUin = "", [FromQuery] string groupUin = "")
public async Task<ActionResult> Get([FromQuery] string groupUin = "", [FromQuery] string memeberUin = "")
{
if (CreateTime.AddHours(1) < DateTime.Now)
{
Expand Down Expand Up @@ -85,61 +85,34 @@ public async Task<BaseResponseModel> EChartsOption([FromQuery] string memeberUin
{
var messageList = DbContext.QueryAllMessage();

Dictionary<string, int> keyValuePairs = new Dictionary<string, int>();
if (!string.IsNullOrEmpty(memeberUin) && uint.TryParse(memeberUin, out uint mUin))
#region 过滤
if (!string.IsNullOrEmpty(groupUin) && uint.TryParse(groupUin, out uint gUin))
{
// 个人
foreach (var item in messageList)
// 某群
messageList = messageList.Where(m => m.GroupUin == groupUin).ToList();
if (!string.IsNullOrEmpty(memeberUin) && uint.TryParse(memeberUin, out uint mUin))
{
// 某人
if (item.QQUin == memeberUin)
{
if (keyValuePairs.ContainsKey(item.CreateTime.ToDateTime13().ToString("yyyy-MM-dd")))
{
keyValuePairs[item.CreateTime.ToDateTime13().ToString("yyyy-MM-dd")] += 1;
}
else
{
keyValuePairs.Add(item.CreateTime.ToDateTime13().ToString("yyyy-MM-dd"), 1);
}
}
messageList = messageList.Where(m => m.QQUin == memeberUin).ToList();
}
}
else if (!string.IsNullOrEmpty(groupUin) && uint.TryParse(groupUin, out uint gUin))
}
#endregion

Dictionary<string, int> keyValuePairs = new Dictionary<string, int>();

foreach (var item in messageList)
{
// 群
foreach (var item in messageList)
if (keyValuePairs.ContainsKey(item.CreateTime.ToDateTime13().ToString("yyyy-MM-dd")))
{
// 某群
if (item.GroupUin == groupUin)
{
if (keyValuePairs.ContainsKey(item.CreateTime.ToDateTime13().ToString("yyyy-MM-dd")))
{
keyValuePairs[item.CreateTime.ToDateTime13().ToString("yyyy-MM-dd")] += 1;
}
else
{
keyValuePairs.Add(item.CreateTime.ToDateTime13().ToString("yyyy-MM-dd"), 1);
}
}
keyValuePairs[item.CreateTime.ToDateTime13().ToString("yyyy-MM-dd")] += 1;
}
}
else
{
// 全部
foreach (var item in messageList)
else
{
if (keyValuePairs.ContainsKey(item.CreateTime.ToDateTime13().ToString("yyyy-MM-dd")))
{
keyValuePairs[item.CreateTime.ToDateTime13().ToString("yyyy-MM-dd")] += 1;
}
else
{
keyValuePairs.Add(item.CreateTime.ToDateTime13().ToString("yyyy-MM-dd"), 1);
}
keyValuePairs.Add(item.CreateTime.ToDateTime13().ToString("yyyy-MM-dd"), 1);
}
}


// 排序 转换
List<List<string>> calendarList = keyValuePairs
.Select(keyValue => (key: DateTime.Parse(keyValue.Key), value: keyValue.Value)
Expand Down
25 changes: 13 additions & 12 deletions plugins/QQStatPlugin/Controllers/StackedAreaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class StackedAreaController : ControllerBase

[Route("/Plugins/QQStatPlugin/StackedArea")]
[HttpGet]
public async Task<ActionResult> Get([FromQuery] string memeberUin = "", [FromQuery] string groupUin = "")
public async Task<ActionResult> Get([FromQuery] string groupUin = "", [FromQuery] string memeberUin = "")
{
if (CreateTime.AddHours(1) < DateTime.Now)
{
Expand Down Expand Up @@ -64,22 +64,23 @@ public async Task<BaseResponseModel> EChartsOption([FromQuery] string memeberUin
{
var messageList = DbContext.QueryAllMessage();

Dictionary<string, int> keyValuePairs = new Dictionary<string, int>();
if (!string.IsNullOrEmpty(memeberUin) && uint.TryParse(memeberUin, out uint mUin))
{
// 个人
messageList = messageList.Where(m => m.QQUin == memeberUin).ToList();
}
else if (!string.IsNullOrEmpty(groupUin) && uint.TryParse(groupUin, out uint gUin))
#region 过滤
if (!string.IsNullOrEmpty(groupUin) && uint.TryParse(groupUin, out uint gUin))
{
// 某群
messageList = messageList.Where(m => m.GroupUin == groupUin).ToList();
if (!string.IsNullOrEmpty(memeberUin) && uint.TryParse(memeberUin, out uint mUin))
{
// 某人
messageList = messageList.Where(m => m.QQUin == memeberUin).ToList();
}
}
else
{
// 全部
#endregion


Dictionary<string, int> keyValuePairs = new Dictionary<string, int>();


}
// 只要前10 发言最多的人
#region 只要 top10
//Dictionary<string, int> tempDic = new Dictionary<string, int>();
Expand Down
216 changes: 111 additions & 105 deletions plugins/QQStatPlugin/QQStatPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,33 @@ public void OnGroupMessage((Bot s, GroupMessageEvent e) obj, string message, str
SettingsModel settingsModel = PluginCore.PluginSettingsModelFactory.Create<SettingsModel>(nameof(QQStatPlugin));

#region 收集群消息
if (settingsModel.Groups != null && settingsModel.Groups.Count >= 1 && settingsModel.Groups.Contains(groupUin.ToString()))
try
{
#region 收集群消息
try
// 保存数据库
int successRow = DbContext.InsertIntoMessage(new Models.Message()
{
// 保存数据库
int successRow = DbContext.InsertIntoMessage(new Models.Message()
{
Content = message,
CreateTime = DateTime.Now.ToTimeStamp13(),
GroupName = groupName,
GroupUin = groupUin.ToString(),
QQName = obj.e.Message.Sender.Name,
QQUin = memberUin.ToString()
});
Console.WriteLine($"成功插入 {successRow} 行");
}
catch (Exception ex)
{
Console.WriteLine("插入表 Message 出错:");
Console.WriteLine(ex.ToString());
}
#endregion
Content = message,
CreateTime = DateTime.Now.ToTimeStamp13(),
GroupName = groupName,
GroupUin = groupUin.ToString(),
QQName = obj.e.Message.Sender.Name,
QQUin = memberUin.ToString()
});
Console.WriteLine($"成功插入 {successRow} 行");
}
catch (Exception ex)
{
Console.WriteLine("插入表 Message 出错:");
Console.WriteLine(ex.ToString());
}
#endregion

#region 图表
Console.WriteLine(groupUin);
bool isAdminGroup = settingsModel.ChartGroups != null && settingsModel.ChartGroups.Count >= 1 && settingsModel.ChartGroups.Contains(groupUin.ToString());
bool isChartGroup = settingsModel.AdminGroups != null && settingsModel.AdminGroups.Count >= 1 && settingsModel.AdminGroups.Contains(groupUin.ToString());
Console.WriteLine($"{groupName} ({groupUin})");
bool isChartGroup = settingsModel.ChartGroups != null && settingsModel.ChartGroups.Count >= 1 && settingsModel.ChartGroups.Contains(groupUin.ToString());
bool isAdminGroup = settingsModel.AdminGroups != null && settingsModel.AdminGroups.Count >= 1 && settingsModel.AdminGroups.Contains(groupUin.ToString());
Console.WriteLine($"{nameof(isChartGroup)}:{isChartGroup}");
Console.WriteLine($"{nameof(isAdminGroup)}:{isAdminGroup}");
if (isAdminGroup || isChartGroup)
{
Console.WriteLine($"进入 {DateTime.Now.ToString()}");
Expand All @@ -78,82 +75,10 @@ public void OnGroupMessage((Bot s, GroupMessageEvent e) obj, string message, str
}
if (member != null)
{
if (member.Uin.ToString() == settingsModel.AdminQQ || member.Role == Konata.Core.Common.RoleType.Owner || member.Role == Konata.Core.Common.RoleType.Admin)
bool isAdmin = member.Uin.ToString() == settingsModel.AdminQQ || member.Role == Konata.Core.Common.RoleType.Owner || member.Role == Konata.Core.Common.RoleType.Admin;
if (isAdmin || isChartGroup)
{
// 群管理员
if (message.Contains("#日历"))
{
#region 日历
string token = Guid.NewGuid().ToString();
Controllers.CalendarController.CreateTime = DateTime.Now;

// 下方获取当前群聊
string urlParam = $"{settingsModel.BaseUrl}/Plugins/QQStatPlugin/Calendar?groupUin={groupUin.ToString()}";
string targetMemberUinStr = message.Replace("#日历", "")?.Trim();
if (uint.TryParse(targetMemberUinStr, out uint targetMemberUin))
{
// 仅此人 日历
urlParam += $"&memeberUin={targetMemberUin}";
}
// 注意: url 编码, 这样才能正确传参
urlParam = System.Web.HttpUtility.UrlEncode(urlParam, System.Text.Encoding.UTF8);
// 加个time 防止缓存
// ScreenshotUrl: xxx.com?url=
string imageUrl = $"{settingsModel.ScreenshotUrl}{urlParam}&time={DateTime.Now.ToTimeStamp13()}";

Console.WriteLine(imageUrl);
try
{
var image = ImageChain.CreateFromUrl(imageUrl);
obj.s.SendGroupMessage(groupUin, image);
}
catch (Exception ex)
{
obj.s.SendGroupMessage(groupUin, "发送 日历 图片失败");
//obj.s.SendGroupMessage(groupUin, imageUrl);

Console.WriteLine("发送 日历 图片失败");
Console.WriteLine(ex.ToString());
}
#endregion
}
else if (message.Contains("#折线"))
{
#region 折线
string token = Guid.NewGuid().ToString();
Controllers.StackedAreaController.CreateTime = DateTime.Now;

// 下方获取当前群聊
string urlParam = $"{settingsModel.BaseUrl}/Plugins/QQStatPlugin/StackedArea?groupUin={groupUin.ToString()}";
string targetMemberUinStr = message.Replace("#折线", "")?.Trim();
if (uint.TryParse(targetMemberUinStr, out uint targetMemberUin))
{
// 仅此人 日历
urlParam += $"&memeberUin={targetMemberUin}";
}
// 注意: url 编码, 这样才能正确传参
urlParam = System.Web.HttpUtility.UrlEncode(urlParam, System.Text.Encoding.UTF8);
// 加个time 防止缓存
// ScreenshotUrl: xxx.com?url=
string imageUrl = $"{settingsModel.ScreenshotUrl}{urlParam}&time={DateTime.Now.ToTimeStamp13()}";

Console.WriteLine(imageUrl);
try
{
var image = ImageChain.CreateFromUrl(imageUrl);
obj.s.SendGroupMessage(groupUin, image);
}
catch (Exception ex)
{
obj.s.SendGroupMessage(groupUin, "发送 折线 图片失败");
//obj.s.SendGroupMessage(groupUin, imageUrl);

Console.WriteLine("发送 折线 图片失败");
Console.WriteLine(ex.ToString());
}
#endregion
}
else if (message.Contains("#帮助"))
if (message.Contains("#帮助"))
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("#日历");
Expand All @@ -162,21 +87,28 @@ public void OnGroupMessage((Bot s, GroupMessageEvent e) obj, string message, str
stringBuilder.AppendLine("#折线 指定某人QQ");
obj.s.SendGroupMessage(groupUin, stringBuilder.ToString());
}
else
else if (message.Contains("#日历"))
{
Console.WriteLine("未包含命令");
#region 日历
SendCalendar(obj, message, groupUin, memberUin, settingsModel);
#endregion
}
else if (message.Contains("#折线"))
{
#region 折线
SendStackedArea(obj, message, groupUin, memberUin, settingsModel);
#endregion
}
}
else
{
Console.WriteLine("非管理员/群主");
}
}
}
#endregion

}




public void OnFriendMessage((Bot s, FriendMessageEvent e) obj, string message, uint friendUin)
{
// 保存数据库
Expand Down Expand Up @@ -244,5 +176,79 @@ public void OnBotOffline((Bot s, BotOfflineEvent e) obj, string botName, uint bo
#endregion


public void SendCalendar((Bot s, GroupMessageEvent e) obj, string message, uint groupUin, uint memberUin, SettingsModel settingsModel)
{
string token = Guid.NewGuid().ToString();
Controllers.CalendarController.CreateTime = DateTime.Now;

// 下方获取当前群聊
string urlParam = $"{settingsModel.BaseUrl}/Plugins/QQStatPlugin/Calendar?groupUin={groupUin.ToString()}";
string targetMemberUinStr = message.Replace("#日历", "")?.Trim();
if (uint.TryParse(targetMemberUinStr, out uint targetMemberUin))
{
// 仅此人 日历
urlParam += $"&memeberUin={targetMemberUin}";
}
// 注意: url 编码, 这样才能正确传参
urlParam = System.Web.HttpUtility.UrlEncode(urlParam, System.Text.Encoding.UTF8);
// 加个time 防止缓存
// ScreenshotUrl: xxx.com?url=
string imageUrl = $"{settingsModel.ScreenshotUrl}{urlParam}&time={DateTime.Now.ToTimeStamp13()}";

Console.WriteLine(imageUrl);
try
{
var image = ImageChain.CreateFromUrl(imageUrl);
obj.s.SendGroupMessage(groupUin, image);
obj.s.SendGroupPoke(groupUin: groupUin, memberUin: memberUin);
obj.s.SendGroupMessage(groupUin, AtChain.Create(memberUin));
}
catch (Exception ex)
{
obj.s.SendGroupMessage(groupUin, "发送 日历 图片失败");
//obj.s.SendGroupMessage(groupUin, imageUrl);

Console.WriteLine("发送 日历 图片失败");
Console.WriteLine(ex.ToString());
}
}

public void SendStackedArea((Bot s, GroupMessageEvent e) obj, string message, uint groupUin, uint memberUin, SettingsModel settingsModel)
{
string token = Guid.NewGuid().ToString();
Controllers.StackedAreaController.CreateTime = DateTime.Now;

// 下方获取当前群聊
string urlParam = $"{settingsModel.BaseUrl}/Plugins/QQStatPlugin/StackedArea?groupUin={groupUin.ToString()}";
string targetMemberUinStr = message.Replace("#折线", "")?.Trim();
if (uint.TryParse(targetMemberUinStr, out uint targetMemberUin))
{
// 仅此人 日历
urlParam += $"&memeberUin={targetMemberUin}";
}
// 注意: url 编码, 这样才能正确传参
urlParam = System.Web.HttpUtility.UrlEncode(urlParam, System.Text.Encoding.UTF8);
// 加个time 防止缓存
// ScreenshotUrl: xxx.com?url=
string imageUrl = $"{settingsModel.ScreenshotUrl}{urlParam}&time={DateTime.Now.ToTimeStamp13()}";

Console.WriteLine(imageUrl);
try
{
var image = ImageChain.CreateFromUrl(imageUrl);
obj.s.SendGroupMessage(groupUin, image);
obj.s.SendGroupPoke(groupUin: groupUin, memberUin: memberUin);
obj.s.SendGroupMessage(groupUin, AtChain.Create(memberUin));
}
catch (Exception ex)
{
obj.s.SendGroupMessage(groupUin, "发送 折线 图片失败");
//obj.s.SendGroupMessage(groupUin, imageUrl);

Console.WriteLine("发送 折线 图片失败");
Console.WriteLine(ex.ToString());
}
}

}
}
Loading

0 comments on commit 727adbd

Please sign in to comment.