Skip to content

Commit

Permalink
[Core] Implemented SetStatus and SetCustomStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored and Linwenxuan committed Feb 14, 2024
1 parent a53a101 commit 34f2c25
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 1 deletion.
21 changes: 20 additions & 1 deletion Lagrange.Core/Common/Interface/Api/OperationExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static Task<List<string>> FetchCookies(this BotContext bot, List<string>
/// <param name="chain">the chain constructed by <see cref="MessageBuilder"/></param>
public static Task<MessageResult> SendMessage(this BotContext bot, MessageChain chain)
=> bot.ContextCollection.Business.OperationLogic.SendMessage(chain);

/// <summary>
/// Recall the group message from Bot itself by <see cref="MessageResult"/>
/// </summary>
Expand Down Expand Up @@ -77,6 +77,25 @@ public static Task<bool> RecallGroupMessage(this BotContext bot, MessageChain ch
public static Task<List<BotGroupRequest>?> FetchRequests(this BotContext bot)
=> bot.ContextCollection.Business.OperationLogic.FetchRequests();

/// <summary>
/// set status
/// </summary>
/// <param name="bot">target BotContext</param>
/// <param name="status">The status code</param>
/// <returns></returns>
public static Task<bool> SetStatus(this BotContext bot, uint status)
=> bot.ContextCollection.Business.OperationLogic.SetStatus(status);

Check failure on line 87 in Lagrange.Core/Common/Interface/Api/OperationExt.cs

View workflow job for this annotation

GitHub Actions / Build (win-x64)

'OperationLogic' does not contain a definition for 'SetStatus' and the best extension method overload 'OperationExt.SetStatus(BotContext, uint)' requires a receiver of type 'Lagrange.Core.BotContext'

Check failure on line 87 in Lagrange.Core/Common/Interface/Api/OperationExt.cs

View workflow job for this annotation

GitHub Actions / build

'OperationLogic' does not contain a definition for 'SetStatus' and the best extension method overload 'OperationExt.SetStatus(BotContext, uint)' requires a receiver of type 'BotContext'

Check failure on line 87 in Lagrange.Core/Common/Interface/Api/OperationExt.cs

View workflow job for this annotation

GitHub Actions / build

'OperationLogic' does not contain a definition for 'SetStatus' and the best extension method overload 'OperationExt.SetStatus(BotContext, uint)' requires a receiver of type 'BotContext'

Check failure on line 87 in Lagrange.Core/Common/Interface/Api/OperationExt.cs

View workflow job for this annotation

GitHub Actions / Build (linux-arm64)

'OperationLogic' does not contain a definition for 'SetStatus' and the best extension method overload 'OperationExt.SetStatus(BotContext, uint)' requires a receiver of type 'Lagrange.Core.BotContext'

Check failure on line 87 in Lagrange.Core/Common/Interface/Api/OperationExt.cs

View workflow job for this annotation

GitHub Actions / Build (linux-musl-arm64)

'OperationLogic' does not contain a definition for 'SetStatus' and the best extension method overload 'OperationExt.SetStatus(BotContext, uint)' requires a receiver of type 'Lagrange.Core.BotContext'

/// <summary>
/// set custom status
/// </summary>
/// <param name="bot">target BotContext</param>
/// <param name="faceId">faceId that is same as the <see cref="Lagrange.Core.Message.Entity.FaceEntity"/></param>
/// <param name="text">text that would shown</param>
/// <returns></returns>
public static Task<bool> SetCustomStatus(this BotContext bot, uint faceId, string text)
=> bot.ContextCollection.Business.OperationLogic.SetCustomStatus(faceId, text);

Check failure on line 97 in Lagrange.Core/Common/Interface/Api/OperationExt.cs

View workflow job for this annotation

GitHub Actions / Build (win-x64)

'OperationLogic' does not contain a definition for 'SetCustomStatus' and the best extension method overload 'OperationExt.SetCustomStatus(BotContext, uint, string)' requires a receiver of type 'Lagrange.Core.BotContext'

Check failure on line 97 in Lagrange.Core/Common/Interface/Api/OperationExt.cs

View workflow job for this annotation

GitHub Actions / build

'OperationLogic' does not contain a definition for 'SetCustomStatus' and the best extension method overload 'OperationExt.SetCustomStatus(BotContext, uint, string)' requires a receiver of type 'BotContext'

Check failure on line 97 in Lagrange.Core/Common/Interface/Api/OperationExt.cs

View workflow job for this annotation

GitHub Actions / build

'OperationLogic' does not contain a definition for 'SetCustomStatus' and the best extension method overload 'OperationExt.SetCustomStatus(BotContext, uint, string)' requires a receiver of type 'BotContext'

Check failure on line 97 in Lagrange.Core/Common/Interface/Api/OperationExt.cs

View workflow job for this annotation

GitHub Actions / Build (linux-arm64)

'OperationLogic' does not contain a definition for 'SetCustomStatus' and the best extension method overload 'OperationExt.SetCustomStatus(BotContext, uint, string)' requires a receiver of type 'Lagrange.Core.BotContext'

Check failure on line 97 in Lagrange.Core/Common/Interface/Api/OperationExt.cs

View workflow job for this annotation

GitHub Actions / Build (linux-musl-arm64)

'OperationLogic' does not contain a definition for 'SetCustomStatus' and the best extension method overload 'OperationExt.SetCustomStatus(BotContext, uint, string)' requires a receiver of type 'Lagrange.Core.BotContext'

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

Expand Down
20 changes: 20 additions & 0 deletions Lagrange.Core/Internal/Event/Action/SetCustomStatusEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Lagrange.Core.Internal.Event.Action;

internal class SetCustomStatusEvent : SetStatusEvent
{
public uint FaceId { get; }

public string Text { get; } = string.Empty;

private SetCustomStatusEvent(uint faceId, string text) : base(2000, 0)
{
FaceId = faceId;
Text = text;
}

private SetCustomStatusEvent(int resultCode) : base(resultCode) { }

public static SetCustomStatusEvent Create(uint faceId, string text) => new(faceId, text);

public new static SetCustomStatusEvent Result(int resultCode) => new(resultCode);
}
20 changes: 20 additions & 0 deletions Lagrange.Core/Internal/Event/Action/SetStatusEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Lagrange.Core.Internal.Event.Action;

internal class SetStatusEvent : ProtocolEvent
{
public uint Status { get; }

public uint ExtStatus { get; }

protected SetStatusEvent(uint status, uint extStatus) : base(true)
{
Status = status;
ExtStatus = extStatus;
}

protected SetStatusEvent(int resultCode) : base(resultCode) { }

public static SetStatusEvent Create(uint status, uint extStatus) => new(status, extStatus);

public static SetStatusEvent Result(int resultCode) => new(resultCode);
}
34 changes: 34 additions & 0 deletions Lagrange.Core/Internal/Packets/Action/SetStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using ProtoBuf;

namespace Lagrange.Core.Internal.Packets.Action;

/// <summary>
/// trpc.qq_new_tech.status_svc.StatusService.SetStatus
/// </summary>
[ProtoContract]
internal class SetStatus
{
[ProtoMember(1)] public uint Field1 { get; set; } // 10

[ProtoMember(2)] public uint Status { get; set; }

[ProtoMember(3)] public uint ExtStatus { get; set; }

[ProtoMember(4)] public SetStatusCustomExt? CustomExt { get; set; }
}

[ProtoContract]
internal class SetStatusCustomExt
{
[ProtoMember(1)] public uint FaceId { get; set; }

[ProtoMember(2)] public string? Text { get; set; }

[ProtoMember(3)] public uint Field3 { get; set; } // 1
}

[ProtoContract]
internal class SetStatusResponse
{
[ProtoMember(2)] public string Message { get; set; } = string.Empty;
}
49 changes: 49 additions & 0 deletions Lagrange.Core/Internal/Service/Action/SetStatusService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Lagrange.Core.Common;
using Lagrange.Core.Internal.Event;
using Lagrange.Core.Internal.Event.Action;
using Lagrange.Core.Internal.Packets.Action;
using Lagrange.Core.Utility.Binary;
using Lagrange.Core.Utility.Extension;
using ProtoBuf;

namespace Lagrange.Core.Internal.Service.Action;

[EventSubscribe(typeof(SetStatusEvent))]
[EventSubscribe(typeof(SetCustomStatusEvent))]
[Service("trpc.qq_new_tech.status_svc.StatusService.SetStatus")]
internal class SetStatusService : BaseService<SetStatusEvent>
{
protected override bool Build(SetStatusEvent input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device,
out BinaryPacket output, out List<BinaryPacket>? extraPackets)
{
var packet = new SetStatus
{
Field1 = 0,
Status = input.Status,
ExtStatus = input.ExtStatus
};

if (input is SetCustomStatusEvent custom)
{
packet.CustomExt = new SetStatusCustomExt
{
FaceId = custom.FaceId,
Text = custom.Text,
Field3 = 1
};
}

output = packet.Serialize();
extraPackets = null;
return true;
}

protected override bool Parse(byte[] input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device,
out SetStatusEvent output, out List<ProtocolEvent>? extraEvents)
{
var payload = Serializer.Deserialize<SetStatusResponse>(input.AsSpan());
extraEvents = null;
output = payload.Message == "set status success" ? SetStatusEvent.Result(0) : SetStatusEvent.Result(-1);
return true;
}
}

0 comments on commit 34f2c25

Please sign in to comment.