-
Notifications
You must be signed in to change notification settings - Fork 10
/
QQClientService.cs
501 lines (432 loc) · 17.1 KB
/
QQClientService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
using CnGalWebSite.RobotClientX.Models.Messages;
using CnGalWebSite.RobotClientX.Models.Robots;
using CnGalWebSite.RobotClientX.DataRepositories;
using CnGalWebSite.RobotClientX.Extentions;
using CnGalWebSite.RobotClientX.Services.Events;
using CnGalWebSite.RobotClientX.Services.Messages;
using Masuda.Net;
using MeowMiraiLib;
using MeowMiraiLib.Msg.Sender;
using MeowMiraiLib.Msg.Type;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Message = MeowMiraiLib.Msg.Type.Message;
using CnGalWebSite.RobotClientX.DataModels;
namespace CnGalWebSite.RobotClientX.Services.QQClients
{
public class QQClientService : IQQClientService, IDisposable
{
private readonly IRepository<RobotGroup> _robotGroupRepository;
private readonly IRepository<RobotEvent> _robotEventRepository;
private readonly IRepository<PostLog> _postLogRepository;
private readonly IConfiguration _configuration;
private readonly IMessageService _messageService;
private readonly IEventService _eventService;
private readonly ILogger<QQClientService> _logger;
public MasudaBot MasudaClient { get; set; }
public Client MiraiClient { get; set; }
private string _miraiSession;
System.Timers.Timer t = new(1000 * 60);
System.Timers.Timer t2 = new(1000 * 60);
public QQClientService(IRepository<RobotGroup> robotGroupRepository, IRepository<PostLog> postLogRepository,
IConfiguration configuration, ILogger<QQClientService> logger, IEventService eventService,
IMessageService messageService, IRepository<RobotEvent> robotEventRepository)
{
_robotGroupRepository = robotGroupRepository;
_configuration = configuration;
_messageService = messageService;
_postLogRepository = postLogRepository;
_logger = logger;
_eventService = eventService;
_robotEventRepository = robotEventRepository;
}
public void InitMasuda()
{
if (int.TryParse(_configuration["ChannelAppId"], out int appId) == false)
{
_logger.LogError("初始化频道失败,ChannelAppId无效");
return;
}
try
{
MasudaClient = new MasudaBot(appId, _configuration["ChannelAppKey"], _configuration["ChannelToken"], BotType.Private);
_logger.LogInformation("成功初始化 Masuda 客户端");
}
catch (Exception ex)
{
_logger.LogError(ex, "初始化 Masuda 客户端失败,配置文件错误或 依赖项 未正确启动");
}
}
public async Task InitMirai()
{
try
{
MiraiClient = new($"ws://{_configuration["MiraiUrl"]}/all?verifyKey={_configuration["NormalVerifyKey"]}&qq={_configuration["QQ"]}");
MiraiClient._OnServeiceConnected += MiraiClient__OnServeiceConnected; ;
if(await MiraiClient.ConnectAsync())
{
_logger.LogInformation("成功初始化 Mirai 客户端");
}
else
{
_logger.LogError("初始化 Mirai 客户端失败");
}
}
catch (Exception ex)
{
_logger.LogError(ex, "初始化 Mirai 客户端失败");
}
}
private void MiraiClient__OnServeiceConnected(string e)
{
if(string.IsNullOrWhiteSpace(e))
{
return;
}
var session = e.MidStrEx("\"session\": \"", "\"");
if (string.IsNullOrWhiteSpace(session))
{
return;
}
_miraiSession= session;
_logger.LogInformation("成功连接Mirai服务器,Session:{Session}", session);
}
public async Task Init()
{
//初始化QQ群
await InitMirai();
if (MiraiClient != null)
{
//定时任务计时器
t.Start(); //启动计时器
t.Elapsed += async (s, e) =>
{
try
{
var message = _eventService.GetCurrentTimeEvent();
if (string.IsNullOrWhiteSpace(message) == false)
{
var result = await _messageService.ProcMessageAsync(RobotReplyRange.Group, message, "", null, 0, null);
if (result != null)
{
foreach (var item in _robotGroupRepository.GetAll().Where(s => s.IsHidden == false && s.ForceMatch == false))
{
result.SendTo = item.GroupId;
await SendMessage(result);
}
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "定时任务异常");
}
};
//随机任务计时器
t2.Start(); //启动计时器
t2.Elapsed += async (s, e) =>
{
try
{
var message = _eventService.GetProbabilityEvents();
if (string.IsNullOrWhiteSpace(message) == false)
{
var result = await _messageService.ProcMessageAsync(RobotReplyRange.Group, message, "", null, 0, null);
if (result != null)
{
foreach (var item in _robotGroupRepository.GetAll().Where(s => s.IsHidden == false && s.ForceMatch == false))
{
result.SendTo = item.GroupId;
await SendMessage(result);
}
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "随机任务异常");
}
};
//好友消息事件
MiraiClient.OnFriendMessageReceive += async (s, e) =>
{
try
{
await ReplyFromFriendAsync(s, e);
}
catch (Exception ex)
{
_logger.LogError(ex, "无法回复好友消息");
}
};
//群聊消息事件
MiraiClient.OnGroupMessageReceive += async (s, e) =>
{
try
{
await ReplyFromGroupAsync(s, e);
}
catch (Exception ex)
{
_logger.LogError(ex, "无法回复群聊消息");
}
};
}
//初始化频道
InitMasuda();
if (MasudaClient != null)
{
// 普通消息事件注册(需要私域)
MasudaClient.MessageAction += async (bot, msg, type) =>
{
try
{
await ReplyFromChannelAsync(msg);
}
catch (Exception ex)
{
_logger.LogError(ex, "回复消息失败");
}
};
}
_logger.LogInformation("CnGal资料站 看板娘 v3.4.6");
}
/// <summary>
/// 回复频道消息
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public async Task ReplyFromChannelAsync(Masuda.Net.Models.Message msg)
{
//过滤非水区的发言
if ((await MasudaClient.GetChannelAsync(msg.ChannelId)).Name.Contains("水区") == false)
{
return;
}
var messgae = MessageHelper.GetPureMessage(msg.Content);
if (string.IsNullOrWhiteSpace(messgae))
{
return;
}
await ReplyMessageAsync(RobotReplyRange.Channel, MessageHelper.GetPureMessage(msg.Content), 0, 0, msg.Author.Username, msg);
}
/// <summary>
/// 回复好友消息
/// </summary>
/// <param name="s"></param>
/// <param name="e"></param>
/// <param name="range"></param>
/// <returns></returns>
public async Task ReplyFromFriendAsync(FriendMessageSender s, MeowMiraiLib.Msg.Type.Message[] msg)
{
await ReplyMessageAsync(RobotReplyRange.Friend, ConversionMeaasge(msg), s.id, s.id, s.nickname);
}
/// <summary>
/// 回复群聊消息
/// </summary>
/// <param name="s"></param>
/// <param name="e"></param>
/// <returns></returns>
public async Task ReplyFromGroupAsync(GroupMessageSender s, MeowMiraiLib.Msg.Type.Message[] msg)
{
//忽略未关注的群聊
RobotGroup group = _robotGroupRepository.GetAll().FirstOrDefault(x => x.GroupId == s.group.id && x.IsHidden == false);
if (group == null)
{
return;
}
//检查是否符合强制匹配
string message = ConversionMeaasge(msg);
if (group.ForceMatch)
{
string name = _configuration["RobotName"] ?? "看板娘";
if ((name != null && message.Contains(name) == false) || message.Contains("介绍") == false)
{
return;
}
}
await ReplyMessageAsync(RobotReplyRange.Group, message, s.group.id, s.id, s.memberName);
}
/// <summary>
/// 转换消息
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
private string ConversionMeaasge(MeowMiraiLib.Msg.Type.Message[] e)
{
string message = e.MGetPlainString();
Message at = e.FirstOrDefault(s => s.type == "At");
if (at != null)
{
long atTarget = (at as At).target;
message += "[@" + atTarget.ToString() + "]";
}
return message;
}
/// <summary>
/// 回复消息
/// </summary>
private async Task ReplyMessageAsync(RobotReplyRange range, string message, long sendto, long memberId, string memberName, Masuda.Net.Models.Message msg = null)
{
//尝试找出所有匹配的回复
RobotReply reply = _messageService.GetAutoReply(message, range);
if (reply == null)
{
return;
}
SendMessageModel result = new();
try
{
//处理消息
result = await _messageService.ProcMessageAsync(range, reply.Value, message, reply.Key, memberId, memberName);
}
catch (ArgError ae)
{
if (long.TryParse(_configuration["WarningQQGroup"], out long warningQQGroup))
{
//发送警告
await SendMessage(new SendMessageModel { SendTo = warningQQGroup, MiraiMessage = new MeowMiraiLib.Msg.Type.Message[] { new Plain(ae.Error) }, Range = RobotReplyRange.Group });
}
}
if (result == null)
{
return;
}
//添加发送记录 并不代表真实发送
_ = _postLogRepository.Insert(new PostLog
{
Message = message,
PostTime = DateTime.Now.ToCstTime(),
QQ = memberId,
Reply = reply.Value
});
//检查上限
if (await CheckLimit(range, sendto, memberId, memberName, msg))
{
return;
}
//发送消息
result.SendTo = sendto;
await SendMessage(result, msg);
}
/// <summary>
/// 检查是否超过限制
/// </summary>
/// <param name="sendto"></param>
/// <param name="memberId"></param>
/// <param name="memberName"></param>
/// <returns>是否超过限制</returns>
private async Task<bool> CheckLimit(RobotReplyRange range, long sendto, long memberId, string memberName, Masuda.Net.Models.Message msg = null)
{
//判断该用户是否连续10次互动 1分钟内
int singleCount = _postLogRepository.GetAll().Count(x => (DateTime.Now.ToCstTime() - x.PostTime).TotalMinutes <= 1 && x.QQ == memberId);
int totalCount = _postLogRepository.GetAll().Count(x => (DateTime.Now.ToCstTime() - x.PostTime).TotalMinutes <= 1);
//读取上限次数配置
if (!long.TryParse(_configuration["SingleLimit"], out long singleLimit))
{
singleLimit = 5;
}
if (!long.TryParse(_configuration["TotalLimit"], out long totalLimit))
{
totalLimit = 10;
}
//检查上限
if (singleCount == singleLimit)
{
SendMessageModel result = await _messageService.ProcMessageAsync(range, _robotEventRepository.GetAll().FirstOrDefault(s => s.Note == "消息上限警告")?.Text ?? $"[image=https://image.cngal.org/kanbanFace/hhzywx.png][@{memberId}]如果恶意骚扰人家的话,我会请你离开哦…", null, null, memberId, memberName);
result.SendTo = sendto;
await SendMessage(result, msg);
return true;
}
if (totalCount == totalLimit)
{
SendMessageModel result = await _messageService.ProcMessageAsync(range, $"核心温度过高,正在冷却......", null, null, memberId, memberName);
result.SendTo = sendto;
await SendMessage(result, msg);
return true;
}
if (singleCount > singleLimit)
{
return true;
}
if (totalCount > totalLimit)
{
return true;
}
return false;
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="range"></param>
/// <param name="id"></param>
/// <param name="text"></param>
/// <returns></returns>
public async Task SendMessage(RobotReplyRange range, long sendto, string text, Masuda.Net.Models.Message msg = null)
{
if (range == RobotReplyRange.Channel)
{
await SendMessage(new SendMessageModel
{
Text = text,
MasudaMessage = _messageService.ProcMessageToMasuda(text),
Range = range,
}, msg);
}
else
{
await SendMessage(new SendMessageModel
{
Text = text,
SendTo = sendto,
MiraiMessage = _messageService.ProcMessageToMirai(text),
Range = range,
});
}
}
public async Task SendMessage(SendMessageModel model, Masuda.Net.Models.Message msg = null)
{
if (model.MiraiMessage == null && model.MasudaMessage == null)
{
return;
}
if (model.Range == RobotReplyRange.Channel)
{
if (model.MasudaMessage == null)
{
return;
}
_ = await MasudaClient.ReplyMessageAsync(msg, model.MasudaMessage);
_logger.LogInformation("向频道发送“{消息}”", model.Text);
}
else if (model.Range == RobotReplyRange.Friend)
{
model.MiraiMessage.SendToFriend(model.SendTo, MiraiClient);
_logger.LogInformation("向 {group} 发送“{消息}”成功", model.SendTo, model.Text);
}
else
{
model.MiraiMessage.SendToGroup(model.SendTo, MiraiClient);
_logger.LogInformation("向 {group} 发送“{消息}”成功", model.SendTo, model.Text);
}
return;
}
public void Dispose()
{
if (t != null)
{
t.Dispose();
t = null;
}
if (t2 != null)
{
t2.Dispose();
t2 = null;
}
}
public string GetMiraiSession()
{
return _miraiSession;
}
}
}