@@ -1,19 +0,0 @@
|
||||
using Debugger;
|
||||
using Grpc.Core;
|
||||
|
||||
namespace Shrink.API;
|
||||
|
||||
public class BotServiceImpl : APIService.APIServiceBase
|
||||
{
|
||||
public override Task<DataResponse> SendData(DataRequest request, ServerCallContext context)
|
||||
{
|
||||
// 模拟向 QQ 机器人处理数据的逻辑
|
||||
|
||||
// 返回响应
|
||||
return Task.FromResult(new DataResponse
|
||||
{
|
||||
Message = "114514",
|
||||
Success = true
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
using Lagrange.Core.Event;
|
||||
using Shrink.Login;
|
||||
|
||||
namespace Shrink.Event;
|
||||
|
||||
public class BotEventHandler
|
||||
{
|
||||
public BotEventHandler()
|
||||
{
|
||||
Console.WriteLine("Bot EventHandler");
|
||||
}
|
||||
private static readonly Lazy<BotEventHandler> _instance = new(() => new BotEventHandler());
|
||||
public static BotEventHandler Instance => _instance.Value;
|
||||
private EventInvoker _invoker = BotService.Instance.Client!.Invoker;
|
||||
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using BotService = Shrink.Login.BotService;
|
||||
using Shrink.Config;
|
||||
using Shrink.Service;
|
||||
|
||||
namespace Shrink;
|
||||
|
||||
@@ -6,6 +7,8 @@ public static class Program
|
||||
{
|
||||
public static async Task Main()
|
||||
{
|
||||
await Configuration.Instance.Init();
|
||||
await BotService.Instance.Login();
|
||||
await BotPassiveMsgHandler.Instance.Init();
|
||||
}
|
||||
}
|
||||
32
Service/ApiService.cs
Normal file
32
Service/ApiService.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using Debugger;
|
||||
using Grpc.Core;
|
||||
using Shrink.Logger;
|
||||
|
||||
namespace Shrink.Service;
|
||||
|
||||
public class ApiService : APIService.APIServiceBase
|
||||
{
|
||||
private static readonly Lazy<ApiService> _instance = new(() => new ApiService());
|
||||
public static ApiService Instance => _instance.Value;
|
||||
|
||||
|
||||
public override Task<DataResponse> SendData(DataRequest request, ServerCallContext context)
|
||||
{
|
||||
LogManager.Instance.LogChain.LogMessage(request.Data, request.Event, request.LogLevel, request.MsgType,
|
||||
request.Uin, request.Text);
|
||||
var response = new DataResponse();
|
||||
if (LogManager.Instance.IsSent)
|
||||
{
|
||||
response.Message = $"已发送至: {request.Uin}。";
|
||||
response.Success = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
response.Message = "未发送,可能的原因是信息被设置为忽略或出现其他问题。";
|
||||
response.Success = false;
|
||||
}
|
||||
|
||||
LogManager.Instance.IsSent = false;
|
||||
return Task.FromResult(response);
|
||||
}
|
||||
}
|
||||
38
Service/BotPassiveMsgHandler.cs
Normal file
38
Service/BotPassiveMsgHandler.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using Debugger;
|
||||
using Lagrange.Core.Common.Interface.Api;
|
||||
using Lagrange.Core.Event;
|
||||
using Lagrange.Core.Message;
|
||||
using Shrink.Config;
|
||||
|
||||
namespace Shrink.Service;
|
||||
|
||||
public class BotPassiveMsgHandler
|
||||
{
|
||||
private static readonly Lazy<BotPassiveMsgHandler> _instance = new(() => new BotPassiveMsgHandler());
|
||||
public static BotPassiveMsgHandler Instance => _instance.Value;
|
||||
public EventInvoker Invoker { get; } = BotService.Instance.Client!.Invoker;
|
||||
|
||||
public Task Init()
|
||||
{
|
||||
Invoker.OnGroupMessageReceived += async (context, @event) =>
|
||||
{
|
||||
if (!@event.Chain.ToPreviewText().StartsWith("/switchlevel")) return;
|
||||
var temp = @event.Chain.ToPreviewText()[("/switchlevel".Length + 1)..];
|
||||
Configuration.Instance.Config = temp switch
|
||||
{
|
||||
"debug" => new Config.Config(LogLevel.Debug),
|
||||
"info" => new Config.Config(LogLevel.Info),
|
||||
"verbose" => new Config.Config(LogLevel.Verbose),
|
||||
"warn" => new Config.Config(LogLevel.Warn),
|
||||
"error" => new Config.Config(LogLevel.Error),
|
||||
"fatal" => new Config.Config(LogLevel.Fatal),
|
||||
_ => Configuration.Instance.Config
|
||||
};
|
||||
await Configuration.Instance.Save();
|
||||
var chain = MessageBuilder.Group((uint)@event.Chain.GroupUin!)
|
||||
.Text($"已将日志输出级别调整为{Configuration.Instance.Config.LogLevel}");
|
||||
await context.SendMessage(chain.Build());
|
||||
};
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,15 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Debugger;
|
||||
using Grpc.Core;
|
||||
using Lagrange.Core;
|
||||
using Lagrange.Core.Common;
|
||||
using Lagrange.Core.Common.Interface;
|
||||
using Lagrange.Core.Common.Interface.Api;
|
||||
using Shrink.API;
|
||||
using Shrink.Logger;
|
||||
using Shrink.Utility;
|
||||
using Console = System.Console;
|
||||
|
||||
namespace Shrink.Login;
|
||||
namespace Shrink.Service;
|
||||
|
||||
public class BotService
|
||||
{
|
||||
@@ -24,24 +23,24 @@ public class BotService
|
||||
|
||||
|
||||
private static BotDeviceInfo GetDeviceInfo() =>
|
||||
ReadOrCreateJsonFile(DeviceInfoFilePath, BotDeviceInfo.GenerateInfo);
|
||||
|
||||
JsonUtility.ReadOrCreateJsonFile(DeviceInfoFilePath, BotDeviceInfo.GenerateInfo);
|
||||
|
||||
// 登录方法
|
||||
public async Task Login()
|
||||
{
|
||||
const int port = 50051;
|
||||
var server = new Server
|
||||
{
|
||||
Services = { APIService.BindService(new BotServiceImpl()) },
|
||||
Services = { APIService.BindService(new ApiService()) },
|
||||
Ports = { new ServerPort("localhost", port, ServerCredentials.Insecure) }
|
||||
};
|
||||
|
||||
|
||||
var deviceInfo = File.Exists(DeviceInfoFilePath)
|
||||
? ReadJsonFromFile<BotDeviceInfo>(DeviceInfoFilePath)
|
||||
? JsonUtility.ReadJsonFromFile<BotDeviceInfo>(DeviceInfoFilePath)
|
||||
: GetDeviceInfo();
|
||||
|
||||
var keyStore = File.Exists(KeystoreFilePath)
|
||||
? ReadJsonFromFile<BotKeystore>(KeystoreFilePath)
|
||||
? JsonUtility.ReadJsonFromFile<BotKeystore>(KeystoreFilePath)
|
||||
: new BotKeystore();
|
||||
|
||||
Client = BotFactory.Create(new BotConfig
|
||||
@@ -65,7 +64,9 @@ public class BotService
|
||||
Console.WriteLine(@event.ToString());
|
||||
_isOnline = true;
|
||||
server.Start();
|
||||
Console.WriteLine($"gRPC server listening on port {port}");
|
||||
Console.WriteLine($"[{DateTime.Now}] [gRPC Server] [Info]: Listening on port: {port}");
|
||||
/*LogManager.Instance.LogChain.LogMessage($"{DateTime.Now}", "LoginEvent", LogLevel.Info, MsgType.Public,
|
||||
620902312, "Login");*/
|
||||
};
|
||||
Client.Invoker.OnBotOfflineEvent += async (_, @event) =>
|
||||
{
|
||||
@@ -92,45 +93,4 @@ public class BotService
|
||||
await File.WriteAllTextAsync(KeystoreFilePath, JsonSerializer.Serialize(Client.UpdateKeystore()));
|
||||
await File.WriteAllTextAsync(DeviceInfoFilePath, JsonSerializer.Serialize(Client.UpdateDeviceInfo()));
|
||||
}
|
||||
|
||||
private static T ReadJsonFromFile<T>(string filePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
var json = File.ReadAllText(filePath);
|
||||
return JsonSerializer.Deserialize<T>(json,
|
||||
new JsonSerializerOptions { ReferenceHandler = ReferenceHandler.Preserve })!;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"读取文件出错: {filePath}: {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private static T ReadOrCreateJsonFile<T>(string filePath, Func<T> createFunc)
|
||||
{
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
return ReadJsonFromFile<T>(filePath);
|
||||
}
|
||||
|
||||
var newData = createFunc();
|
||||
WriteJsonToFile(filePath, newData);
|
||||
return newData;
|
||||
}
|
||||
|
||||
private static void WriteJsonToFile<T>(string filePath, T data)
|
||||
{
|
||||
try
|
||||
{
|
||||
var json = JsonSerializer.Serialize(data);
|
||||
File.WriteAllText(filePath, json);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"写入文件出错: {filePath}: {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,23 +17,12 @@
|
||||
</PackageReference>
|
||||
<PackageReference Include="Lagrange.Core" Version="0.3.1" />
|
||||
<PackageReference Include="Mono.Cecil" Version="0.11.6" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="SSH.NET" Version="2024.2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="Service\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Protobuf Include="API/service.proto" GrpcServices="Server" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Remove="Service\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="Service\**" />
|
||||
<Protobuf Include="proto\apiService.proto" GrpcServices="Server" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user