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;
|
||||
}
|
||||
}
|
||||
96
Service/BotService.cs
Normal file
96
Service/BotService.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using System.Text.Json;
|
||||
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.Logger;
|
||||
using Shrink.Utility;
|
||||
using Console = System.Console;
|
||||
|
||||
namespace Shrink.Service;
|
||||
|
||||
public class BotService
|
||||
{
|
||||
private static readonly Lazy<BotService> _instance = new(() => new BotService());
|
||||
public static BotService Instance => _instance.Value;
|
||||
|
||||
public BotContext? Client;
|
||||
private bool _isOnline;
|
||||
private static string KeystoreFilePath => "Keystore.json";
|
||||
private static string DeviceInfoFilePath => "DeviceInfo.json";
|
||||
|
||||
|
||||
private static BotDeviceInfo GetDeviceInfo() =>
|
||||
JsonUtility.ReadOrCreateJsonFile(DeviceInfoFilePath, BotDeviceInfo.GenerateInfo);
|
||||
|
||||
// 登录方法
|
||||
public async Task Login()
|
||||
{
|
||||
const int port = 50051;
|
||||
var server = new Server
|
||||
{
|
||||
Services = { APIService.BindService(new ApiService()) },
|
||||
Ports = { new ServerPort("localhost", port, ServerCredentials.Insecure) }
|
||||
};
|
||||
|
||||
var deviceInfo = File.Exists(DeviceInfoFilePath)
|
||||
? JsonUtility.ReadJsonFromFile<BotDeviceInfo>(DeviceInfoFilePath)
|
||||
: GetDeviceInfo();
|
||||
|
||||
var keyStore = File.Exists(KeystoreFilePath)
|
||||
? JsonUtility.ReadJsonFromFile<BotKeystore>(KeystoreFilePath)
|
||||
: new BotKeystore();
|
||||
|
||||
Client = BotFactory.Create(new BotConfig
|
||||
{
|
||||
UseIPv6Network = false,
|
||||
GetOptimumServer = true,
|
||||
AutoReconnect = true,
|
||||
Protocol = Protocols.Linux
|
||||
}, deviceInfo, keyStore);
|
||||
|
||||
// Log模式
|
||||
Client.Invoker.OnBotLogEvent += (_, @event) =>
|
||||
{
|
||||
@event.Level.ChangeColorByTitle();
|
||||
Console.WriteLine(@event.ToString());
|
||||
};
|
||||
|
||||
// bot信息保存
|
||||
Client.Invoker.OnBotOnlineEvent += (_, @event) =>
|
||||
{
|
||||
Console.WriteLine(@event.ToString());
|
||||
_isOnline = true;
|
||||
server.Start();
|
||||
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) =>
|
||||
{
|
||||
Console.WriteLine(@event.ToString());
|
||||
await server.ShutdownAsync();
|
||||
_isOnline = false;
|
||||
};
|
||||
|
||||
if (File.Exists(KeystoreFilePath))
|
||||
{
|
||||
await Client.LoginByPassword();
|
||||
if (!_isOnline)
|
||||
{
|
||||
Console.WriteLine("账密登录失败,请尝试二维码登录。");
|
||||
var qrCode = await Client.FetchQrCode();
|
||||
if (qrCode != null)
|
||||
{
|
||||
await File.WriteAllBytesAsync("qr.png", qrCode.Value.QrCode);
|
||||
await Client.LoginByQrCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await File.WriteAllTextAsync(KeystoreFilePath, JsonSerializer.Serialize(Client.UpdateKeystore()));
|
||||
await File.WriteAllTextAsync(DeviceInfoFilePath, JsonSerializer.Serialize(Client.UpdateDeviceInfo()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user