Skip to content

Commit

Permalink
Mostly usable
Browse files Browse the repository at this point in the history
  • Loading branch information
mahoshojoHCG committed Jun 27, 2020
1 parent 2752eb0 commit cc9ba07
Show file tree
Hide file tree
Showing 13 changed files with 411 additions and 113 deletions.
4 changes: 3 additions & 1 deletion HCGStudio.DongBot.App/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace HCGStudio.DongBot.App
Expand All @@ -21,5 +22,6 @@ public class Configuration
public string EventUrl { get; set; }
public string ApiPath { get; set; }
public string EventPath { get; set; }
public List<long> SuperUserIds { get; set; }
}
}
5 changes: 5 additions & 0 deletions HCGStudio.DongBot.App/HCGStudio.DongBot.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Migrations\20200626153153_Application.cs" />
<Compile Remove="Migrations\20200626153153_Application.Designer.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.5">
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
ServiceRecordId = table.Column<int>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
GroupId = table.Column<long>(nullable: false),
ServiceName = table.Column<string>(nullable: true),
ServiceName = table.Column<string>(maxLength: 500, nullable: true),
IsEnabled = table.Column<bool>(nullable: false)
},
constraints: table =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasColumnType("INTEGER");

b.Property<string>("ServiceName")
.HasColumnType("TEXT");
.HasColumnType("TEXT")
.HasMaxLength(500);

b.HasKey("ServiceRecordId");

Expand Down
2 changes: 2 additions & 0 deletions HCGStudio.DongBot.App/Models/Service.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Security.Authentication.ExtendedProtection;
using System.Text;
using Microsoft.EntityFrameworkCore;
Expand All @@ -10,6 +11,7 @@ public class ServiceRecord
{
public int ServiceRecordId { get; set; }
public long GroupId { get; set; }
[MaxLength(500)]
public string ServiceName { get; set; }
public bool IsEnabled { get; set; }
}
Expand Down
353 changes: 275 additions & 78 deletions HCGStudio.DongBot.App/Program.cs

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions HCGStudio.DongBot.App/SystemService/BroadcastMessageSender.cs
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);
}
}
}
}
87 changes: 87 additions & 0 deletions HCGStudio.DongBot.App/SystemService/ServiceService.cs
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 HCGStudio.DongBot.App/SystemService/SystemBroadcastService.cs

This file was deleted.

5 changes: 0 additions & 5 deletions HCGStudio.DongBot.App/SystemService/VersionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,5 @@ public async Task VersionQuery(long senderUserId)
await _messageSender.SendPrivateAsync(senderUserId, (SimpleMessage)$"DongBot {Assembly.GetExecutingAssembly().GetName().Version}");
}

[OnKeyword("图灵测试", InvokePolicies = InvokePolicies.Private, KeywordPolicy = KeywordPolicy.Trim)]
public async Task Turing(long senderUserId)
{
await _messageSender.SendPrivateAsync(senderUserId, (SimpleMessage)"Hello, world! 父亲!");
}
}
}
1 change: 1 addition & 0 deletions HCGStudio.DongBot.Core/Attributes/OnKeywordAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public OnKeywordAttribute(params string[] keywords)
public List<string> Keywords { get; }
public KeywordPolicy KeywordPolicy { get; set; } = KeywordPolicy.Trim;
public InvokePolicies InvokePolicies { get; set; } = InvokePolicies.Private;
public bool RequireSuperUser { get; set; } = false;
}

public enum KeywordPolicy
Expand Down
2 changes: 1 addition & 1 deletion HCGStudio.DongBot.Core/Messages/AtMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public class AtMessage : Message, IAtMessage
{
public AtMessage(long atAccount, bool atAll)
public AtMessage(long atAccount, bool atAll = false)
{
Content = atAccount;
AtAll = atAll;
Expand Down

0 comments on commit cc9ba07

Please sign in to comment.