From 322e222bd858a87e214dc031a7b5c5b5d34f40fe Mon Sep 17 00:00:00 2001 From: Linwenxuan <116782992+Linwenxuan05@users.noreply.github.com> Date: Thu, 15 Feb 2024 02:34:44 +0800 Subject: [PATCH] [OneBot] Fixed OneBotSender.cs --- .../Action/Response/OneBotGetMessageResponse.cs | 6 +++--- .../Core/Entity/Message/OneBotPrivateMsg.cs | 4 ++-- Lagrange.OneBot/Core/Entity/Message/OneBotSender.cs | 12 ++++++++++-- .../Core/Operation/Message/GetMessageOperation.cs | 6 +++++- Lagrange.OneBot/Message/MessageService.cs | 9 ++------- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/Lagrange.OneBot/Core/Entity/Action/Response/OneBotGetMessageResponse.cs b/Lagrange.OneBot/Core/Entity/Action/Response/OneBotGetMessageResponse.cs index a3297b334..c910ae780 100644 --- a/Lagrange.OneBot/Core/Entity/Action/Response/OneBotGetMessageResponse.cs +++ b/Lagrange.OneBot/Core/Entity/Action/Response/OneBotGetMessageResponse.cs @@ -4,7 +4,7 @@ namespace Lagrange.OneBot.Core.Entity.Action.Response; [Serializable] -public class OneBotGetMessageResponse(DateTime time, string messageType, int messageId, List message) +public class OneBotGetMessageResponse(DateTime time, string messageType, int messageId, OneBotSender sender, List message) { [JsonPropertyName("time")] public int Time { get; set; } = (int)(time - DateTime.UnixEpoch).TotalSeconds; @@ -14,7 +14,7 @@ public class OneBotGetMessageResponse(DateTime time, string messageType, int mes [JsonPropertyName("real_id")] public int RealId { get; set; } = messageId; - [JsonPropertyName("sender")] public OneBotSender Sender { get; set; } = new(); - + [JsonPropertyName("sender")] public OneBotSender Sender { get; set; } = sender; + [JsonPropertyName("message")] public List Message { get; set; } = message; } \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Entity/Message/OneBotPrivateMsg.cs b/Lagrange.OneBot/Core/Entity/Message/OneBotPrivateMsg.cs index b4ee792a8..f4bf2e5cc 100644 --- a/Lagrange.OneBot/Core/Entity/Message/OneBotPrivateMsg.cs +++ b/Lagrange.OneBot/Core/Entity/Message/OneBotPrivateMsg.cs @@ -3,7 +3,7 @@ namespace Lagrange.OneBot.Core.Entity.Message; [Serializable] -public class OneBotPrivateMsg(uint selfId) : OneBotEntityBase(selfId, "message") +public class OneBotPrivateMsg(uint selfId, OneBotSender groupSender) : OneBotEntityBase(selfId, "message") { [JsonPropertyName("message_type")] public string MessageType { get; } = "private"; @@ -19,5 +19,5 @@ public class OneBotPrivateMsg(uint selfId) : OneBotEntityBase(selfId, "message") [JsonPropertyName("font")] public int Font { get; set; } = 0; - [JsonPropertyName("sender")] public OneBotSender GroupSender { get; set; } = new(); + [JsonPropertyName("sender")] public OneBotSender GroupSender { get; set; } = groupSender; } \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Entity/Message/OneBotSender.cs b/Lagrange.OneBot/Core/Entity/Message/OneBotSender.cs index d4a7d1973..45d087409 100644 --- a/Lagrange.OneBot/Core/Entity/Message/OneBotSender.cs +++ b/Lagrange.OneBot/Core/Entity/Message/OneBotSender.cs @@ -1,7 +1,15 @@ +using System.Text.Json.Serialization; + namespace Lagrange.OneBot.Core.Entity.Message; [Serializable] -public class OneBotSender +public class OneBotSender(uint userId, string nickName) { - + [JsonPropertyName("user_id")] public uint UserId { get; set; } = userId; + + [JsonPropertyName("nickname")] public string NickName { get; set; } = nickName; + + [JsonPropertyName("sex")] public string Sex { get; set; } = "unknown"; + + [JsonPropertyName("age")] public int Age = -1; } \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Operation/Message/GetMessageOperation.cs b/Lagrange.OneBot/Core/Operation/Message/GetMessageOperation.cs index b0ffe9c5c..8259c8152 100644 --- a/Lagrange.OneBot/Core/Operation/Message/GetMessageOperation.cs +++ b/Lagrange.OneBot/Core/Operation/Message/GetMessageOperation.cs @@ -4,6 +4,7 @@ using Lagrange.Core.Message; using Lagrange.OneBot.Core.Entity.Action; using Lagrange.OneBot.Core.Entity.Action.Response; +using Lagrange.OneBot.Core.Entity.Message; using Lagrange.OneBot.Database; using Lagrange.OneBot.Message; using LiteDB; @@ -19,8 +20,11 @@ public Task HandleOperation(BotContext context, JsonObject? payloa { var record = database.GetCollection().FindOne(x => x.MessageHash == getMsg.MessageId); var chain = (MessageChain)record; + var sender = chain.GroupMemberInfo == null + ? new OneBotSender(chain.GroupMemberInfo?.Uin ?? 0, chain.GroupMemberInfo?.MemberName ?? string.Empty) + : new OneBotSender(chain.FriendUin, chain.FriendInfo?.Nickname ?? string.Empty); var elements = service.Convert(chain); - var response = new OneBotGetMessageResponse(chain.Time, chain.IsGroup ? "group" : "private", record.MessageHash, elements); + var response = new OneBotGetMessageResponse(chain.Time, chain.IsGroup ? "group" : "private", record.MessageHash, sender, elements); return Task.FromResult(new OneBotResult(response, 0, "ok")); } diff --git a/Lagrange.OneBot/Message/MessageService.cs b/Lagrange.OneBot/Message/MessageService.cs index 45f5cd3b1..75671262b 100644 --- a/Lagrange.OneBot/Message/MessageService.cs +++ b/Lagrange.OneBot/Message/MessageService.cs @@ -63,19 +63,14 @@ public MessageService(BotContext bot, LagrangeWebSvcCollection service, LiteData private void OnFriendMessageReceived(BotContext bot, FriendMessageEvent e) { - var record = (MessageRecord)e.Chain; _context.GetCollection().Insert(new BsonValue(record.MessageHash), record); var segments = Convert(e.Chain); - var request = new OneBotPrivateMsg(bot.BotUin) + var request = new OneBotPrivateMsg(bot.BotUin, new OneBotSender(e.Chain.FriendUin, e.Chain.FriendInfo?.Nickname ?? string.Empty)) { - MessageId = (int)record.MessageHash, + MessageId = record.MessageHash, UserId = e.Chain.FriendUin, - GroupSender = new OneBotSender - { - - }, Message = segments, RawMessage = ToRawMessage(segments) };