-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mahoshojoHCG
committed
Jun 27, 2020
1 parent
2752eb0
commit cc9ba07
Showing
13 changed files
with
411 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
...ns/20200626153153_Application.Designer.cs → ...ns/20200627045848_Application.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
HCGStudio.DongBot.App/SystemService/BroadcastMessageSender.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using HCGStudio.DongBot.App.Models; | ||
using HCGStudio.DongBot.Core.Messages; | ||
using HCGStudio.DongBot.Core.Service; | ||
|
||
namespace HCGStudio.DongBot.App.SystemService | ||
{ | ||
public class BroadcastMessageSender : IBroadcastMessageSender | ||
{ | ||
private readonly IMessageSender _messageSender; | ||
public string ServiceName { get; } | ||
public BroadcastMessageSender(IMessageSender messageSender, string serviceName) | ||
{ | ||
_messageSender = messageSender; | ||
ServiceName = serviceName; | ||
} | ||
public async Task BroadcastAllEnabled(Message message, int interval = 100) | ||
{ | ||
await using var context = new ApplicationContext(); | ||
var enabledGroup = from record in context.ServiceRecords | ||
where record.ServiceName == ServiceName && record.IsEnabled | ||
select record.GroupId; | ||
foreach (var groupId in enabledGroup) | ||
{ | ||
await _messageSender.SendGroupAsync(groupId, message); | ||
await Task.Delay(interval); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using HCGStudio.DongBot.App.Models; | ||
using HCGStudio.DongBot.Core.Attributes; | ||
using HCGStudio.DongBot.Core.Messages; | ||
using HCGStudio.DongBot.Core.Service; | ||
|
||
namespace HCGStudio.DongBot.App.SystemService | ||
{ | ||
[Service("Core")] | ||
public class ServiceService | ||
{ | ||
private readonly IMessageSender _messageSender; | ||
|
||
public ServiceService(IMessageSender messageSender) | ||
{ | ||
_messageSender = messageSender; | ||
} | ||
|
||
[OnKeyword("查看服务", InvokePolicies = InvokePolicies.Group, RequireSuperUser = true)] | ||
public async Task ListServices(long groupId, long userId) | ||
{ | ||
await using var context = new ApplicationContext(); | ||
var services = from record in context.ServiceRecords where record.GroupId == groupId select record; | ||
var messageBuilder = new MessageBuilder(); | ||
messageBuilder.Append(new AtMessage(userId)); | ||
messageBuilder.Append((SimpleMessage)$"群{groupId}服务列表:\n"); | ||
foreach (var serviceRecord in services) | ||
{ | ||
messageBuilder.Append((SimpleMessage)$"{(serviceRecord.IsEnabled ? "√" : "×")} {serviceRecord.ServiceName}\n"); | ||
} | ||
|
||
await _messageSender.SendGroupAsync(groupId, messageBuilder.ToMessage()); | ||
} | ||
|
||
[OnKeyword("启用", InvokePolicies = InvokePolicies.Group, KeywordPolicy = KeywordPolicy.Begin, RequireSuperUser = true)] | ||
public async Task EnableService(long groupId, long userId, Message message) | ||
{ | ||
var name = message.ToPureString().Substring(2); | ||
await using var context = new ApplicationContext(); | ||
var services = from record in context.ServiceRecords where record.GroupId == groupId && record.ServiceName == name select record; | ||
var messageBuilder = new MessageBuilder(); | ||
messageBuilder.Append(new AtMessage(userId)); | ||
if (!services.Any()) | ||
{ | ||
messageBuilder.Append((SimpleMessage)$"未能找到服务{services.First().ServiceName}!"); | ||
} | ||
else | ||
{ | ||
services.First().IsEnabled = true; | ||
messageBuilder.Append((SimpleMessage)$"服务{services.First().ServiceName}启用成功!"); | ||
await context.SaveChangesAsync(); | ||
} | ||
|
||
await _messageSender.SendGroupAsync(groupId, messageBuilder.ToMessage()); | ||
} | ||
|
||
[OnKeyword("禁用", InvokePolicies = InvokePolicies.Group, KeywordPolicy = KeywordPolicy.Begin, RequireSuperUser = true)] | ||
public async Task DisableService(long groupId, long userId, Message message) | ||
{ | ||
var name = message.ToPureString().Substring(2); | ||
await using var context = new ApplicationContext(); | ||
var services = from record in context.ServiceRecords where record.GroupId == groupId && record.ServiceName == name select record; | ||
var messageBuilder = new MessageBuilder(); | ||
messageBuilder.Append(new AtMessage(userId)); | ||
if (!services.Any()) | ||
{ | ||
messageBuilder.Append((SimpleMessage)$"未能找到服务{services.First().ServiceName}!"); | ||
} | ||
else if(services.First().ServiceName != "Core") | ||
{ | ||
services.First().IsEnabled = false; | ||
messageBuilder.Append((SimpleMessage)$"服务{services.First().ServiceName}禁用成功!"); | ||
await context.SaveChangesAsync(); | ||
} | ||
else | ||
{ | ||
messageBuilder.Append((SimpleMessage) "不能禁用Core服务!"); | ||
} | ||
|
||
await _messageSender.SendGroupAsync(groupId, messageBuilder.ToMessage()); | ||
} | ||
} | ||
} |
24 changes: 0 additions & 24 deletions
24
HCGStudio.DongBot.App/SystemService/SystemBroadcastService.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters