From c6eeaa5b4a61a32e60a5ab4db9fda8598690c90f Mon Sep 17 00:00:00 2001 From: Linwenxuan <116782992+Linwenxuan05@users.noreply.github.com> Date: Thu, 9 Nov 2023 19:23:09 +0800 Subject: [PATCH] [OneBot] Try to support node --- Lagrange.Core/Message/MessageBuilder.cs | 16 ++++++++++++++++ .../Core/Message/Entity/ForwardSegment.cs | 7 +++++-- .../Core/Message/SegmentSubscriberAttribute.cs | 4 +++- .../Core/Operation/Message/MessageCommon.cs | 2 +- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Lagrange.Core/Message/MessageBuilder.cs b/Lagrange.Core/Message/MessageBuilder.cs index 6abc15288..c8d678ea9 100644 --- a/Lagrange.Core/Message/MessageBuilder.cs +++ b/Lagrange.Core/Message/MessageBuilder.cs @@ -82,6 +82,22 @@ public MessageBuilder Image(byte[] file) return this; } + public MessageBuilder File(byte[] file, string fileName) + { + var fileEntity = new FileEntity(file, fileName); + _chain.Add(fileEntity); + + return this; + } + + public MessageBuilder File(string filePath) + { + var fileEntity = new FileEntity(filePath); + _chain.Add(fileEntity); + + return this; + } + public MessageBuilder Add(IMessageEntity entity) { _chain.Add(entity); diff --git a/Lagrange.OneBot/Core/Message/Entity/ForwardSegment.cs b/Lagrange.OneBot/Core/Message/Entity/ForwardSegment.cs index 4ebd227b3..5a4f920d9 100644 --- a/Lagrange.OneBot/Core/Message/Entity/ForwardSegment.cs +++ b/Lagrange.OneBot/Core/Message/Entity/ForwardSegment.cs @@ -12,12 +12,15 @@ public ForwardSegment() : this("") { } [JsonPropertyName("id")] public string Name { get; set; } = name; } -[SegmentSubscriber(typeof(MultiMsgEntity), "forward")] +[SegmentSubscriber(typeof(MultiMsgEntity), "forward", "node")] public partial class ForwardSegment : ISegment { public IMessageEntity ToEntity() => throw new InvalidOperationException("Only Receive but not send"); - public void Build(MessageBuilder builder, ISegment segment) => throw new InvalidOperationException(); + public void Build(MessageBuilder builder, ISegment segment) + { + if (segment is ForwardSegment forward) builder.Add(new MultiMsgEntity(forward.Name)); + } public ISegment FromEntity(IMessageEntity entity) { diff --git a/Lagrange.OneBot/Core/Message/SegmentSubscriberAttribute.cs b/Lagrange.OneBot/Core/Message/SegmentSubscriberAttribute.cs index 9931ae32c..7e4e8fecd 100644 --- a/Lagrange.OneBot/Core/Message/SegmentSubscriberAttribute.cs +++ b/Lagrange.OneBot/Core/Message/SegmentSubscriberAttribute.cs @@ -1,9 +1,11 @@ namespace Lagrange.OneBot.Core.Message; [AttributeUsage(AttributeTargets.Class)] -public class SegmentSubscriberAttribute(Type entity, string type) : Attribute +public class SegmentSubscriberAttribute(Type entity, string type, string? sendType = null) : Attribute { public Type Entity { get; } = entity; public string Type { get; } = type; + + public string SendType { get; } = sendType ?? type; } \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Operation/Message/MessageCommon.cs b/Lagrange.OneBot/Core/Operation/Message/MessageCommon.cs index 03150da87..0bae8d05e 100644 --- a/Lagrange.OneBot/Core/Operation/Message/MessageCommon.cs +++ b/Lagrange.OneBot/Core/Operation/Message/MessageCommon.cs @@ -19,7 +19,7 @@ static MessageCommon() foreach (var type in Assembly.GetExecutingAssembly().GetTypes()) { var attribute = type.GetCustomAttribute(); - if (attribute != null) TypeToSegment[attribute.Type] = (ISegment)type.CreateInstance(false); + if (attribute != null) TypeToSegment[attribute.SendType] = (ISegment)type.CreateInstance(false); } }