Skip to content

Commit

Permalink
Implemented GroupRename and GroupRemark
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored and Linwenxuan committed Oct 14, 2023
1 parent 4b3ca34 commit 3e1fefa
Show file tree
Hide file tree
Showing 13 changed files with 245 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Lagrange.Core/Common/Interface/Api/GroupExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public static Task<bool> SetGroupAdmin(this BotContext bot, uint groupUin, uint

public static Task<bool> RenameGroupMember(this BotContext bot, uint groupUin, uint targetUin, string targetName)
=> bot.ContextCollection.Business.OperationLogic.RenameGroupMember(groupUin, targetUin, targetName);

public static Task<bool> RenameGroup(this BotContext bot, uint groupUin, string targetName)
=> bot.ContextCollection.Business.OperationLogic.RenameGroup(groupUin, targetName);

public static Task<bool> RemarkGroup(this BotContext bot, uint groupUin, string targetRemark)
=> bot.ContextCollection.Business.OperationLogic.RenameGroup(groupUin, targetRemark);

public static Task<ulong> FetchGroupFSSpace(this BotContext bot, uint groupUin)
=> bot.ContextCollection.Business.OperationLogic.FetchGroupFSSpace(groupUin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ public async Task<bool> RenameGroupMember(uint groupUin, uint targetUin, string
return events.Count != 0 && ((GroupSetAdminEvent)events[0]).ResultCode == 0;
}

public async Task<bool> 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<bool> 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<ulong> FetchGroupFSSpace(uint groupUin)
{
var groupFSSpaceEvent = GroupFSSpaceEvent.Create(groupUin);
Expand Down
20 changes: 20 additions & 0 deletions Lagrange.Core/Internal/Event/Protocol/Action/GroupRemarkEvent.cs
Original file line number Diff line number Diff line change
@@ -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);
}
20 changes: 20 additions & 0 deletions Lagrange.Core/Internal/Event/Protocol/Action/GroupRenameEvent.cs
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using ProtoBuf;

namespace Lagrange.Core.Internal.Packets.Service.Oidb.Request;

#pragma warning disable CS8618
// ReSharper disable InconsistentNaming

/// <summary>
/// Group Remark
/// </summary>
[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; }
}
44 changes: 44 additions & 0 deletions Lagrange.Core/Internal/Service/Action/GroupRemarkService.cs
Original file line number Diff line number Diff line change
@@ -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<GroupRemarkEvent>
{
protected override bool Build(GroupRemarkEvent input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device,
out BinaryPacket output, out List<BinaryPacket>? extraPackets)
{
var packet = new OidbSvcTrpcTcpBase<OidbSvcTrpcTcp0xF16_1>(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<ProtocolEvent>? extraEvents)
{
var payload = Serializer.Deserialize<OidbSvcTrpcTcpResponse<byte[]>>(input.AsSpan());

output = GroupRemarkEvent.Result((int)payload.ErrorCode);
extraEvents = null;
return true;
}
}
41 changes: 41 additions & 0 deletions Lagrange.Core/Internal/Service/Action/GroupRenameService.cs
Original file line number Diff line number Diff line change
@@ -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<GroupRenameEvent>
{
protected override bool Build(GroupRenameEvent input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device,
out BinaryPacket output, out List<BinaryPacket>? extraPackets)
{
var packet = new OidbSvcTrpcTcpBase<OidbSvcTrpcTcp0x89A_15>(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<ProtocolEvent>? extraEvents)
{
var payload = Serializer.Deserialize<OidbSvcTrpcTcpResponse<byte[]>>(input.AsSpan());

output = GroupRenameEvent.Result((int)payload.ErrorCode);
extraEvents = null;
return true;
}
}
13 changes: 13 additions & 0 deletions Lagrange.OneBot/Core/Entity/Action/OneBotSetGroupCard.cs
Original file line number Diff line number Diff line change
@@ -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; } = "";
}
11 changes: 11 additions & 0 deletions Lagrange.OneBot/Core/Entity/Action/OneBotSetGroupName.cs
Original file line number Diff line number Diff line change
@@ -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; } = "";
}
24 changes: 24 additions & 0 deletions Lagrange.OneBot/Core/Operation/Group/SetGroupCardOperation.cs
Original file line number Diff line number Diff line change
@@ -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<OneBotResult> HandleOperation(string echo, BotContext context, JsonObject? payload)
{
var message = payload.Deserialize<OneBotSetGroupCard>();

if (message != null)
{
bool _ = await context.RenameGroupMember(message.GroupId, message.UserId, message.Card);
return new OneBotResult(null, 0, "ok", echo);
}

throw new Exception();
}
}
24 changes: 24 additions & 0 deletions Lagrange.OneBot/Core/Operation/Group/SetGroupNameOperation.cs
Original file line number Diff line number Diff line change
@@ -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<OneBotResult> HandleOperation(string echo, BotContext context, JsonObject? payload)
{
var message = payload.Deserialize<OneBotSetGroupName>();

if (message != null)
{
bool _ = await context.RenameGroup(message.GroupId, message.GroupName);
return new OneBotResult(null, 0, "ok", echo);
}

throw new Exception();
}
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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] | 🔴 |
Expand Down

0 comments on commit 3e1fefa

Please sign in to comment.