Skip to content

Commit

Permalink
[OneBot] Fixed OneBotSender.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored and Linwenxuan committed Feb 14, 2024
1 parent 160ae25 commit 322e222
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace Lagrange.OneBot.Core.Entity.Action.Response;

[Serializable]
public class OneBotGetMessageResponse(DateTime time, string messageType, int messageId, List<OneBotSegment> message)
public class OneBotGetMessageResponse(DateTime time, string messageType, int messageId, OneBotSender sender, List<OneBotSegment> message)
{
[JsonPropertyName("time")] public int Time { get; set; } = (int)(time - DateTime.UnixEpoch).TotalSeconds;

Expand All @@ -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<OneBotSegment> Message { get; set; } = message;
}
4 changes: 2 additions & 2 deletions Lagrange.OneBot/Core/Entity/Message/OneBotPrivateMsg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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;
}
12 changes: 10 additions & 2 deletions Lagrange.OneBot/Core/Entity/Message/OneBotSender.cs
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,8 +20,11 @@ public Task<OneBotResult> HandleOperation(BotContext context, JsonObject? payloa
{
var record = database.GetCollection<MessageRecord>().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"));
}
Expand Down
9 changes: 2 additions & 7 deletions Lagrange.OneBot/Message/MessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MessageRecord>().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)
};
Expand Down

0 comments on commit 322e222

Please sign in to comment.