实现API功能并编写日志过滤方法

Signed-off-by: Eicy <im@crash.work>
This commit is contained in:
2024-12-21 01:05:49 +08:00
parent 2537d11f53
commit 3a41f098fe
7 changed files with 87 additions and 100 deletions

View 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;
}
}