Skip to content

Commit

Permalink
[OneBot] Implemented set_qq_avatar and set_group_portrait
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan04 committed Oct 3, 2024
1 parent 64796b5 commit 8c160e6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Lagrange.OneBot/Core/Entity/Action/OneBotSetGroupPortrait.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 OneBotSetGroupPortrait
{
[JsonPropertyName("group_id")] public uint GroupId { get; set; }

[JsonPropertyName("file")] public string File { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Text.Json.Nodes;
using Lagrange.Core;
using Lagrange.Core.Common.Interface.Api;
using Lagrange.Core.Message.Entity;
using Lagrange.OneBot.Core.Entity.Action;
using Lagrange.OneBot.Message.Entity;

namespace Lagrange.OneBot.Core.Operation.Generic;

[Operation("set_qq_avatar")]
public class SetQQAvatar : IOperation
{
public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? payload)
{
if (payload?["file"]?.ToString() is { } portrait)
{
var image = CommonResolver.ResolveStream(portrait);
if (image == null) throw new Exception();

var imageEntity = new ImageEntity(image);
bool result = await context.SetAvatar(imageEntity);
return new OneBotResult(null, result ? 0 : 1, "");
}

throw new Exception();
}
}
28 changes: 28 additions & 0 deletions Lagrange.OneBot/Core/Operation/Group/SetGroupPortraitOperation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using Lagrange.Core;
using Lagrange.Core.Common.Interface.Api;
using Lagrange.Core.Message.Entity;
using Lagrange.OneBot.Core.Entity.Action;
using Lagrange.OneBot.Message.Entity;

namespace Lagrange.OneBot.Core.Operation.Group;

[Operation("set_group_portrait")]
public class SetGroupPortraitOperation : IOperation
{
public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? payload)
{
if (payload.Deserialize<OneBotSetGroupPortrait>() is { } portrait)
{
var image = CommonResolver.ResolveStream(portrait.File);
if (image == null) throw new Exception();

var imageEntity = new ImageEntity(image);
bool result = await context.GroupSetAvatar(portrait.GroupId, imageEntity);
return new OneBotResult(null, result ? 0 : 1, "");
}

throw new Exception();
}
}

0 comments on commit 8c160e6

Please sign in to comment.