From 3e1fefabecb8846d60cda68e886e69ad14b29974 Mon Sep 17 00:00:00 2001 From: Linwenxuan <116782992+Linwenxuan05@users.noreply.github.com> Date: Sat, 14 Oct 2023 20:50:55 +0800 Subject: [PATCH] Implemented GroupRename and GroupRemark --- .../Common/Interface/Api/GroupExt.cs | 6 +++ .../Logic/Implementation/OperationLogic.cs | 15 +++++++ .../Event/Protocol/Action/GroupRemarkEvent.cs | 20 +++++++++ .../Event/Protocol/Action/GroupRenameEvent.cs | 20 +++++++++ .../Oidb/Request/OidbSvcTrpcTcp0x89A_15.cs | 1 + .../Oidb/Request/OidbSvcTrpcTcp0xF16_1.cs | 24 ++++++++++ .../Service/Action/GroupRemarkService.cs | 44 +++++++++++++++++++ .../Service/Action/GroupRenameService.cs | 41 +++++++++++++++++ .../Core/Entity/Action/OneBotSetGroupCard.cs | 13 ++++++ .../Core/Entity/Action/OneBotSetGroupName.cs | 11 +++++ .../Operation/Group/SetGroupCardOperation.cs | 24 ++++++++++ .../Operation/Group/SetGroupNameOperation.cs | 24 ++++++++++ README.md | 4 +- 13 files changed, 245 insertions(+), 2 deletions(-) create mode 100644 Lagrange.Core/Internal/Event/Protocol/Action/GroupRemarkEvent.cs create mode 100644 Lagrange.Core/Internal/Event/Protocol/Action/GroupRenameEvent.cs create mode 100644 Lagrange.Core/Internal/Packets/Service/Oidb/Request/OidbSvcTrpcTcp0xF16_1.cs create mode 100644 Lagrange.Core/Internal/Service/Action/GroupRemarkService.cs create mode 100644 Lagrange.Core/Internal/Service/Action/GroupRenameService.cs create mode 100644 Lagrange.OneBot/Core/Entity/Action/OneBotSetGroupCard.cs create mode 100644 Lagrange.OneBot/Core/Entity/Action/OneBotSetGroupName.cs create mode 100644 Lagrange.OneBot/Core/Operation/Group/SetGroupCardOperation.cs create mode 100644 Lagrange.OneBot/Core/Operation/Group/SetGroupNameOperation.cs diff --git a/Lagrange.Core/Common/Interface/Api/GroupExt.cs b/Lagrange.Core/Common/Interface/Api/GroupExt.cs index c272c89ac..8cf13a148 100644 --- a/Lagrange.Core/Common/Interface/Api/GroupExt.cs +++ b/Lagrange.Core/Common/Interface/Api/GroupExt.cs @@ -18,6 +18,12 @@ public static Task SetGroupAdmin(this BotContext bot, uint groupUin, uint public static Task RenameGroupMember(this BotContext bot, uint groupUin, uint targetUin, string targetName) => bot.ContextCollection.Business.OperationLogic.RenameGroupMember(groupUin, targetUin, targetName); + + public static Task RenameGroup(this BotContext bot, uint groupUin, string targetName) + => bot.ContextCollection.Business.OperationLogic.RenameGroup(groupUin, targetName); + + public static Task RemarkGroup(this BotContext bot, uint groupUin, string targetRemark) + => bot.ContextCollection.Business.OperationLogic.RenameGroup(groupUin, targetRemark); public static Task FetchGroupFSSpace(this BotContext bot, uint groupUin) => bot.ContextCollection.Business.OperationLogic.FetchGroupFSSpace(groupUin); diff --git a/Lagrange.Core/Internal/Context/Logic/Implementation/OperationLogic.cs b/Lagrange.Core/Internal/Context/Logic/Implementation/OperationLogic.cs index 9f8b78633..5b6744bce 100644 --- a/Lagrange.Core/Internal/Context/Logic/Implementation/OperationLogic.cs +++ b/Lagrange.Core/Internal/Context/Logic/Implementation/OperationLogic.cs @@ -88,6 +88,21 @@ public async Task RenameGroupMember(uint groupUin, uint targetUin, string return events.Count != 0 && ((GroupSetAdminEvent)events[0]).ResultCode == 0; } + public async Task RenameGroup(uint groupUin, string targetName) + { + var renameGroupEvent = GroupRenameEvent.Create(groupUin, targetName); + var events = await Collection.Business.SendEvent(renameGroupEvent); + return events.Count != 0 && ((GroupRenameEvent)events[0]).ResultCode == 0; + } + + public async Task RemarkGroup(uint groupUin, string targetRemark) + { + var renameGroupEvent = GroupRemarkEvent.Create(groupUin, targetRemark); + var events = await Collection.Business.SendEvent(renameGroupEvent); + return events.Count != 0 && ((GroupRemarkEvent)events[0]).ResultCode == 0; + } + + public async Task FetchGroupFSSpace(uint groupUin) { var groupFSSpaceEvent = GroupFSSpaceEvent.Create(groupUin); diff --git a/Lagrange.Core/Internal/Event/Protocol/Action/GroupRemarkEvent.cs b/Lagrange.Core/Internal/Event/Protocol/Action/GroupRemarkEvent.cs new file mode 100644 index 000000000..823927cf2 --- /dev/null +++ b/Lagrange.Core/Internal/Event/Protocol/Action/GroupRemarkEvent.cs @@ -0,0 +1,20 @@ +namespace Lagrange.Core.Internal.Event.Protocol.Action; + +internal class GroupRemarkEvent : ProtocolEvent +{ + public uint GroupUin { get; set; } + + public string TargetRemark { get; set; } = ""; + + private GroupRemarkEvent(uint groupUin, string targetRemark) : base(true) + { + GroupUin = groupUin; + TargetRemark = targetRemark; + } + + private GroupRemarkEvent(int resultCode) : base(resultCode) { } + + public static GroupRemarkEvent Create(uint groupUin, string targetRemark) => new(groupUin, targetRemark); + + public static GroupRemarkEvent Result(int resultCode) => new(resultCode); +} \ No newline at end of file diff --git a/Lagrange.Core/Internal/Event/Protocol/Action/GroupRenameEvent.cs b/Lagrange.Core/Internal/Event/Protocol/Action/GroupRenameEvent.cs new file mode 100644 index 000000000..96fd4be4c --- /dev/null +++ b/Lagrange.Core/Internal/Event/Protocol/Action/GroupRenameEvent.cs @@ -0,0 +1,20 @@ +namespace Lagrange.Core.Internal.Event.Protocol.Action; + +internal class GroupRenameEvent : ProtocolEvent +{ + public uint GroupUin { get; set; } + + public string TargetName { get; set; } = ""; + + private GroupRenameEvent(uint groupUin, string targetName) : base(true) + { + GroupUin = groupUin; + TargetName = targetName; + } + + private GroupRenameEvent(int resultCode) : base(resultCode) { } + + public static GroupRenameEvent Create(uint groupUin, string targetName) => new(groupUin, targetName); + + public static GroupRenameEvent Result(int resultCode) => new(resultCode); +} \ No newline at end of file diff --git a/Lagrange.Core/Internal/Packets/Service/Oidb/Request/OidbSvcTrpcTcp0x89A_15.cs b/Lagrange.Core/Internal/Packets/Service/Oidb/Request/OidbSvcTrpcTcp0x89A_15.cs index 499ed5a1e..7f34916fc 100644 --- a/Lagrange.Core/Internal/Packets/Service/Oidb/Request/OidbSvcTrpcTcp0x89A_15.cs +++ b/Lagrange.Core/Internal/Packets/Service/Oidb/Request/OidbSvcTrpcTcp0x89A_15.cs @@ -17,6 +17,7 @@ internal class OidbSvcTrpcTcp0x89A_15 [ProtoMember(2)] public OidbSvcTrpcTcp0x89A_15Body Body { get; set; } } +[ProtoContract] internal class OidbSvcTrpcTcp0x89A_15Body { [ProtoMember(3)] public string TargetName { get; set; } diff --git a/Lagrange.Core/Internal/Packets/Service/Oidb/Request/OidbSvcTrpcTcp0xF16_1.cs b/Lagrange.Core/Internal/Packets/Service/Oidb/Request/OidbSvcTrpcTcp0xF16_1.cs new file mode 100644 index 000000000..e08692047 --- /dev/null +++ b/Lagrange.Core/Internal/Packets/Service/Oidb/Request/OidbSvcTrpcTcp0xF16_1.cs @@ -0,0 +1,24 @@ +using ProtoBuf; + +namespace Lagrange.Core.Internal.Packets.Service.Oidb.Request; + +#pragma warning disable CS8618 +// ReSharper disable InconsistentNaming + +/// +/// Group Remark +/// +[ProtoContract] +[OidbSvcTrpcTcp(0xF16, 1)] +internal class OidbSvcTrpcTcp0xF16_1 +{ + [ProtoMember(1)] public OidbSvcTrpcTcp0xF16_1Body Body { get; set; } +} + +[ProtoContract] +internal class OidbSvcTrpcTcp0xF16_1Body +{ + [ProtoMember(1)] public uint GroupUin { get; set; } + + [ProtoMember(3)] public string TargetRemark { get; set; } +} \ No newline at end of file diff --git a/Lagrange.Core/Internal/Service/Action/GroupRemarkService.cs b/Lagrange.Core/Internal/Service/Action/GroupRemarkService.cs new file mode 100644 index 000000000..bbeb12435 --- /dev/null +++ b/Lagrange.Core/Internal/Service/Action/GroupRemarkService.cs @@ -0,0 +1,44 @@ +using Lagrange.Core.Common; +using Lagrange.Core.Internal.Event.Protocol; +using Lagrange.Core.Internal.Event.Protocol.Action; +using Lagrange.Core.Internal.Packets.Service.Oidb; +using Lagrange.Core.Internal.Packets.Service.Oidb.Request; +using Lagrange.Core.Utility.Binary; +using ProtoBuf; + +namespace Lagrange.Core.Internal.Service.Action; + +[EventSubscribe(typeof(GroupRemarkEvent))] +[Service("OidbSvcTrpcTcp.0xf16_1")] +internal class GroupRemarkService : BaseService +{ + protected override bool Build(GroupRemarkEvent input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device, + out BinaryPacket output, out List? extraPackets) + { + var packet = new OidbSvcTrpcTcpBase(new OidbSvcTrpcTcp0xF16_1 + { + Body = new OidbSvcTrpcTcp0xF16_1Body + { + GroupUin = input.GroupUin, + TargetRemark = input.TargetRemark + } + }); + + var stream = new MemoryStream(); + Serializer.Serialize(stream, packet); + output = new BinaryPacket(stream); + + extraPackets = null; + return true; + } + + protected override bool Parse(byte[] input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device, + out GroupRemarkEvent output, out List? extraEvents) + { + var payload = Serializer.Deserialize>(input.AsSpan()); + + output = GroupRemarkEvent.Result((int)payload.ErrorCode); + extraEvents = null; + return true; + } +} \ No newline at end of file diff --git a/Lagrange.Core/Internal/Service/Action/GroupRenameService.cs b/Lagrange.Core/Internal/Service/Action/GroupRenameService.cs new file mode 100644 index 000000000..7c598c47f --- /dev/null +++ b/Lagrange.Core/Internal/Service/Action/GroupRenameService.cs @@ -0,0 +1,41 @@ +using Lagrange.Core.Common; +using Lagrange.Core.Internal.Event.Protocol; +using Lagrange.Core.Internal.Event.Protocol.Action; +using Lagrange.Core.Internal.Packets.Service.Oidb; +using Lagrange.Core.Internal.Packets.Service.Oidb.Request; +using Lagrange.Core.Utility.Binary; +using ProtoBuf; + +namespace Lagrange.Core.Internal.Service.Action; + +[EventSubscribe(typeof(GroupRenameEvent))] +[Service("OidbSvcTrpcTcp.0x89a_15")] +internal class GroupRenameService : BaseService +{ + protected override bool Build(GroupRenameEvent input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device, + out BinaryPacket output, out List? extraPackets) + { + var packet = new OidbSvcTrpcTcpBase(new OidbSvcTrpcTcp0x89A_15 + { + GroupUin = input.GroupUin, + Body = new OidbSvcTrpcTcp0x89A_15Body { TargetName = input.TargetName } + }); + + var stream = new MemoryStream(); + Serializer.Serialize(stream, packet); + output = new BinaryPacket(stream); + + extraPackets = null; + return true; + } + + protected override bool Parse(byte[] input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device, + out GroupRenameEvent output, out List? extraEvents) + { + var payload = Serializer.Deserialize>(input.AsSpan()); + + output = GroupRenameEvent.Result((int)payload.ErrorCode); + extraEvents = null; + return true; + } +} \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Entity/Action/OneBotSetGroupCard.cs b/Lagrange.OneBot/Core/Entity/Action/OneBotSetGroupCard.cs new file mode 100644 index 000000000..2b688abac --- /dev/null +++ b/Lagrange.OneBot/Core/Entity/Action/OneBotSetGroupCard.cs @@ -0,0 +1,13 @@ +using System.Text.Json.Serialization; + +namespace Lagrange.OneBot.Core.Entity.Action; + +[Serializable] +public class OneBotSetGroupCard +{ + [JsonPropertyName("user_id")] public uint UserId { get; set; } + + [JsonPropertyName("group_id")] public uint GroupId { get; set; } + + [JsonPropertyName("card")] public string Card { get; set; } = ""; +} \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Entity/Action/OneBotSetGroupName.cs b/Lagrange.OneBot/Core/Entity/Action/OneBotSetGroupName.cs new file mode 100644 index 000000000..be7a376ff --- /dev/null +++ b/Lagrange.OneBot/Core/Entity/Action/OneBotSetGroupName.cs @@ -0,0 +1,11 @@ +using System.Text.Json.Serialization; + +namespace Lagrange.OneBot.Core.Entity.Action; + +[Serializable] +public class OneBotSetGroupName +{ + [JsonPropertyName("group_id")] public uint GroupId { get; set; } + + [JsonPropertyName("group_name")] public string GroupName { get; set; } = ""; +} \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Operation/Group/SetGroupCardOperation.cs b/Lagrange.OneBot/Core/Operation/Group/SetGroupCardOperation.cs new file mode 100644 index 000000000..a3f2c5266 --- /dev/null +++ b/Lagrange.OneBot/Core/Operation/Group/SetGroupCardOperation.cs @@ -0,0 +1,24 @@ +using System.Text.Json; +using System.Text.Json.Nodes; +using Lagrange.Core; +using Lagrange.Core.Common.Interface.Api; +using Lagrange.OneBot.Core.Entity.Action; + +namespace Lagrange.OneBot.Core.Operation.Group; + +[Operation("set_group_card")] +public class SetGroupCardOperation : IOperation +{ + public async Task HandleOperation(string echo, BotContext context, JsonObject? payload) + { + var message = payload.Deserialize(); + + if (message != null) + { + bool _ = await context.RenameGroupMember(message.GroupId, message.UserId, message.Card); + return new OneBotResult(null, 0, "ok", echo); + } + + throw new Exception(); + } +} \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Operation/Group/SetGroupNameOperation.cs b/Lagrange.OneBot/Core/Operation/Group/SetGroupNameOperation.cs new file mode 100644 index 000000000..288240bc8 --- /dev/null +++ b/Lagrange.OneBot/Core/Operation/Group/SetGroupNameOperation.cs @@ -0,0 +1,24 @@ +using System.Text.Json; +using System.Text.Json.Nodes; +using Lagrange.Core; +using Lagrange.Core.Common.Interface.Api; +using Lagrange.OneBot.Core.Entity.Action; + +namespace Lagrange.OneBot.Core.Operation.Group; + +[Operation("set_group_name")] +public class SetGroupNameOperation : IOperation +{ + public async Task HandleOperation(string echo, BotContext context, JsonObject? payload) + { + var message = payload.Deserialize(); + + if (message != null) + { + bool _ = await context.RenameGroup(message.GroupId, message.GroupName); + return new OneBotResult(null, 0, "ok", echo); + } + + throw new Exception(); + } +} \ No newline at end of file diff --git a/README.md b/README.md index f677195a0..e4e8aaa33 100644 --- a/README.md +++ b/README.md @@ -114,8 +114,8 @@ Please use Lagrange.Core responsibly and in accordance with the law. | [/set_group_whole_ban] | 🟢 | | [/set_group_admin] | 🟢 | | [/set_group_anonymous] | 🔴 | -| [/set_group_card] | 🔴 | -| [/set_group_name] | 🔴 | +| [/set_group_card] | 🟢 | +| [/set_group_name] | 🟢 | | [/set_group_leave] | 🔴 | | [/set_group_special_title] | 🔴 | | [/set_friend_add_request] | 🔴 |