Skip to content

Commit

Permalink
Update Progress for Lagrange.OneBot
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored and Linwenxuan committed Oct 12, 2023
1 parent 5ee7d28 commit dc2589e
Show file tree
Hide file tree
Showing 20 changed files with 244 additions and 217 deletions.
45 changes: 17 additions & 28 deletions Lagrange.OneBot/Core/Entity/Message/OneBotGroupMsg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,25 @@
namespace Lagrange.OneBot.Core.Entity.Message;

[Serializable]
public class OneBotGroupMsg : OneBotEntityBase
public class OneBotGroupMsg(uint selfId, uint groupUin, List<OneBotSegment> message, BotGroupMember member) : OneBotEntityBase(selfId, "message")
{
[JsonPropertyName("message_type")] public string MessageType { get; set; }
[JsonPropertyName("sub_type")] public string SubType { get; set; }
[JsonPropertyName("message_type")] public string MessageType { get; set; } = "group";

[JsonPropertyName("sub_type")] public string SubType { get; set; } = "normal";

[JsonPropertyName("message_id")] public int MessageId { get; set; }

[JsonPropertyName("group_id")] public uint GroupId { get; set; } = groupUin;

[JsonPropertyName("group_id")] public uint GroupId { get; set; }

[JsonPropertyName("user_id")] public uint UserId { get; set; }

[JsonPropertyName("anonymous")] public object? Anonymous { get; set; }

[JsonPropertyName("message")] public List<ISegment> Message { get; set; }

[JsonPropertyName("raw_message")] public string RawMessage { get; set; }

[JsonPropertyName("font")] public int Font { get; set; }
[JsonPropertyName("user_id")] public uint UserId { get; set; } = member.Uin;

[JsonPropertyName("sender")] public OneBotGroupSender GroupSender { get; set; }

public OneBotGroupMsg(uint selfId, List<ISegment> message, BotGroupMember member) : base(selfId, "message")
{
MessageType = "group";
SubType = "normal";
Anonymous = null;
Message = message;
RawMessage = string.Empty;
Font = 0;
GroupSender = new OneBotGroupSender(member.Uin, member.MemberName, member.MemberCard, (int)member.GroupLevel, member.Permission);
}
[JsonPropertyName("anonymous")] public object? Anonymous { get; set; } = null;

[JsonPropertyName("message")] public List<OneBotSegment> Message { get; set; } = message;

[JsonPropertyName("raw_message")] public string RawMessage { get; set; } = string.Empty;

[JsonPropertyName("font")] public int Font { get; set; } = 0;

[JsonPropertyName("sender")] public OneBotGroupSender GroupSender { get; set; } = new(member.Uin, member.MemberName, member.MemberCard, (int)member.GroupLevel, member.Permission);
}
59 changes: 23 additions & 36 deletions Lagrange.OneBot/Core/Entity/Message/OneBotGroupSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,29 @@
namespace Lagrange.OneBot.Core.Entity.Message;

[Serializable]
public class OneBotGroupSender
public class OneBotGroupSender(uint userId, string nickname, string card, int level, GroupMemberPermission permission)
{
[JsonPropertyName("user_id")] public uint UserId { get; set; }

[JsonPropertyName("nickname")] public string Nickname { get; set; }

[JsonPropertyName("card")] public string Card { get; set; }

[JsonPropertyName("sex")] public string Sex { get; set; }

[JsonPropertyName("age")] public uint Age { get; set; }

[JsonPropertyName("area")] public string Area { get; set; }

[JsonPropertyName("level")] public string Level { get; set; }

[JsonPropertyName("role")] public string Role { get; set; }

[JsonPropertyName("title")] public string Title { get; set; }

public OneBotGroupSender(uint userId, string nickname, string card, int level, GroupMemberPermission permission)
[JsonPropertyName("user_id")] public uint UserId { get; set; } = userId;

[JsonPropertyName("nickname")] public string Nickname { get; set; } = nickname;

[JsonPropertyName("card")] public string Card { get; set; } = card;

[JsonPropertyName("sex")] public string Sex { get; set; } = "unknown";

[JsonPropertyName("age")] public uint Age { get; set; } = 0;

[JsonPropertyName("area")] public string Area { get; set; } = string.Empty;

[JsonPropertyName("level")] public string Level { get; set; } = level.ToString();

[JsonPropertyName("role")] public string Role { get; set; } = permission switch
{
UserId = userId;
Nickname = nickname;
Card = card;
Sex = "unknown";
Age = 0;
Area = string.Empty;
Level = level.ToString();
Role = permission switch
{
GroupMemberPermission.Owner => "owner",
GroupMemberPermission.Admin => "admin",
GroupMemberPermission.Member => "member",
_ => "unknown"
};
Title = string.Empty;
}
GroupMemberPermission.Owner => "owner",
GroupMemberPermission.Admin => "admin",
GroupMemberPermission.Member => "member",
_ => "unknown"
};

[JsonPropertyName("title")] public string Title { get; set; } = string.Empty;
}
34 changes: 12 additions & 22 deletions Lagrange.OneBot/Core/Entity/Message/OneBotPrivateMsg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,21 @@
namespace Lagrange.OneBot.Core.Entity.Message;

[Serializable]
public class OneBotPrivateMsg : OneBotEntityBase
public class OneBotPrivateMsg(uint selfId) : OneBotEntityBase(selfId, "message")
{
[JsonPropertyName("message_type")] public string MessageType { get; set; }
[JsonPropertyName("sub_type")] public string SubType { get; set; }
[JsonPropertyName("message_type")] public string MessageType { get; } = "private";

[JsonPropertyName("sub_type")] public string SubType { get; } = "friend";

[JsonPropertyName("message_id")] public int MessageId { get; set; }

[JsonPropertyName("user_id")] public uint UserId { get; set; }

[JsonPropertyName("message")] public List<ISegment> Message { get; set; }

[JsonPropertyName("raw_message")] public string RawMessage { get; set; }

[JsonPropertyName("font")] public int Font { get; set; }

[JsonPropertyName("sender")] public OneBotSender GroupSender { get; set; }

public OneBotPrivateMsg(uint selfId) : base(selfId, "message")
{
MessageType = "private";
SubType = "friend";
Message = new List<ISegment>();
RawMessage = string.Empty;
Font = 0;
GroupSender = new OneBotSender();
}
[JsonPropertyName("message")] public List<OneBotSegment> Message { get; set; } = new();

[JsonPropertyName("raw_message")] public string RawMessage { get; set; } = string.Empty;

[JsonPropertyName("font")] public int Font { get; set; } = 0;

[JsonPropertyName("sender")] public OneBotSender GroupSender { get; set; } = new();
}
11 changes: 11 additions & 0 deletions Lagrange.OneBot/Core/Entity/Message/OneBotSegment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Text.Json.Serialization;
using Lagrange.OneBot.Core.Message;

namespace Lagrange.OneBot.Core.Entity.Message;

public class OneBotSegment(string type, ISegment data)
{
[JsonPropertyName("type")] public string Type { get; set; } = type;

[JsonPropertyName("data")] public object Data { get; set; } = data;
}
14 changes: 4 additions & 10 deletions Lagrange.OneBot/Core/Entity/Meta/OneBotHeartBeat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@
namespace Lagrange.OneBot.Core.Entity.Meta;

[Serializable]
public class OneBotHeartBeat : OneBotMeta
public class OneBotHeartBeat(uint selfId, int interval, object status) : OneBotMeta(selfId, "heartbeat")
{
[JsonPropertyName("interval")] public int Interval { get; set; }

[JsonPropertyName("status")] public object Status { get; set; }

public OneBotHeartBeat(uint selfId, int interval, object status) : base(selfId, "heartbeat")
{
Interval = interval;
Status = status;
}
[JsonPropertyName("interval")] public int Interval { get; set; } = interval;

[JsonPropertyName("status")] public object Status { get; set; } = status;
}
9 changes: 2 additions & 7 deletions Lagrange.OneBot/Core/Entity/Meta/OneBotLifecycle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
namespace Lagrange.OneBot.Core.Entity.Meta;

[Serializable]
public class OneBotLifecycle : OneBotMeta
public class OneBotLifecycle(uint selfId, string subType) : OneBotMeta(selfId, "lifecycle")
{
[JsonPropertyName("sub_type")] public string SubType { get; set; }

public OneBotLifecycle(uint selfId, string subType) : base(selfId, "lifecycle")
{
SubType = subType;
}
[JsonPropertyName("sub_type")] public string SubType { get; set; } = subType;
}
9 changes: 2 additions & 7 deletions Lagrange.OneBot/Core/Entity/Meta/OneBotMeta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
namespace Lagrange.OneBot.Core.Entity.Meta;

[Serializable]
public abstract class OneBotMeta : OneBotEntityBase
public abstract class OneBotMeta(uint selfId, string metaEventType) : OneBotEntityBase(selfId, "meta_event")
{
[JsonPropertyName("meta_event_type")] public string MetaEventType { get; set; }

protected OneBotMeta(uint selfId, string metaEventType) : base(selfId, "meta_event")
{
MetaEventType = metaEventType;
}
[JsonPropertyName("meta_event_type")] public string MetaEventType { get; set; } = metaEventType;
}
29 changes: 10 additions & 19 deletions Lagrange.OneBot/Core/Entity/Meta/OneBotStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,15 @@
namespace Lagrange.OneBot.Core.Entity.Meta;

[Serializable]
public class OneBotStatus
public class OneBotStatus(bool online, bool good)
{
[JsonPropertyName("app_initialized")] public bool AppInitialized { get; set; }

[JsonPropertyName("app_enabled")] public bool AppEnabled { get; set; }

[JsonPropertyName("app_good")] public bool AppGood { get; set; }

[JsonPropertyName("online")] public bool Online { get; set; }

[JsonPropertyName("good")] public bool Good { get; set; }

public OneBotStatus(bool online, bool good)
{
AppInitialized = true;
AppEnabled = true;
AppGood = true;
Online = online;
Good = good;
}
[JsonPropertyName("app_initialized")] public bool AppInitialized { get; set; } = true;

[JsonPropertyName("app_enabled")] public bool AppEnabled { get; set; } = true;

[JsonPropertyName("app_good")] public bool AppGood { get; set; } = true;

[JsonPropertyName("online")] public bool Online { get; set; } = online;

[JsonPropertyName("good")] public bool Good { get; set; } = good;
}
19 changes: 6 additions & 13 deletions Lagrange.OneBot/Core/Entity/OneBotEntityBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@
namespace Lagrange.OneBot.Core.Entity;

[Serializable]
public abstract class OneBotEntityBase
public abstract class OneBotEntityBase(uint selfId, string postType)
{
[JsonPropertyName("time")] public long Time { get; set; }

[JsonPropertyName("self_id")] public uint SelfId { get; set; }

[JsonPropertyName("post_type")] public string PostType { get; set; }

protected OneBotEntityBase(uint selfId, string postType)
{
Time = DateTimeOffset.Now.ToUnixTimeSeconds();
SelfId = selfId;
PostType = postType;
}
[JsonPropertyName("time")] public long Time { get; set; } = DateTimeOffset.Now.ToUnixTimeSeconds();

[JsonPropertyName("self_id")] public uint SelfId { get; set; } = selfId;

[JsonPropertyName("post_type")] public string PostType { get; set; } = postType;
}
13 changes: 6 additions & 7 deletions Lagrange.OneBot/Core/Message/Entity/AtSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@
namespace Lagrange.OneBot.Core.Message.Entity;

[Serializable]
public partial class AtSegment
public partial class AtSegment(uint at)
{
public AtSegment(uint at) => At = at.ToString();

[JsonPropertyName("qq")] public string At { get; set; }
public AtSegment() : this(0) { }
[JsonPropertyName("qq")] public string At { get; set; } = at.ToString();
}

[SegmentSubscriber(typeof(MentionEntity), "at")]
public partial class AtSegment : ISegment
{
string ISegment.Type => "at";

public IMessageEntity ToEntity() => new MentionEntity("", uint.Parse(At));

public ISegment FromMessageEntity(IMessageEntity entity)
public ISegment FromEntity(IMessageEntity entity)
{
if (entity is not MentionEntity mentionEntity) throw new ArgumentException("Invalid entity type.");

Expand Down
13 changes: 6 additions & 7 deletions Lagrange.OneBot/Core/Message/Entity/FaceSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@
namespace Lagrange.OneBot.Core.Message.Entity;

[Serializable]
public partial class FaceSegment
public partial class FaceSegment(int id)
{
public FaceSegment(int id) => Id = id.ToString();

[JsonPropertyName("id")] public string Id { get; set; }
public FaceSegment() : this(0) { }
[JsonPropertyName("id")] public string Id { get; set; } = id.ToString();
}

[SegmentSubscriber(typeof(FaceSegment), "face")]
public partial class FaceSegment : ISegment
{
string ISegment.Type => "face";

public IMessageEntity ToEntity() => new FaceEntity(ushort.Parse(Id), false);

public ISegment FromMessageEntity(IMessageEntity entity)
public ISegment FromEntity(IMessageEntity entity)
{
if (entity is not FaceEntity faceEntity) throw new ArgumentException("Invalid entity type.");

Expand Down
26 changes: 26 additions & 0 deletions Lagrange.OneBot/Core/Message/Entity/ImageSegment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Text.Json.Serialization;
using Lagrange.Core.Message;
using Lagrange.Core.Message.Entity;

namespace Lagrange.OneBot.Core.Message.Entity;

[Serializable]
public partial class ImageSegment(string url)
{
public ImageSegment() : this("") { }

[JsonPropertyName("file")] public string Url { get; set; } = url;
}

[SegmentSubscriber(typeof(ImageEntity), "image")]
public partial class ImageSegment : ISegment
{
public IMessageEntity ToEntity() => new ImageEntity(url);

public ISegment FromEntity(IMessageEntity entity)
{
if (entity is not ImageEntity imageEntity) throw new ArgumentException("Invalid entity type.");

return new ImageSegment(imageEntity.ImageUrl);
}
}
Loading

0 comments on commit dc2589e

Please sign in to comment.