Skip to content

Commit

Permalink
我疑似有待能抽象
Browse files Browse the repository at this point in the history
  • Loading branch information
sisi0318 committed Jan 12, 2025
1 parent af5f4ef commit bcdcd74
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Lagrange.OneBot/Core/Entity/Message/OneBotSendMessageWithGroup.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.Message;

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

[JsonPropertyName("target_group_id")] public uint TargetGroupId { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using Lagrange.Core;
using Lagrange.Core.Message;
using Lagrange.Core.Common.Interface.Api;
using Lagrange.OneBot.Core.Entity.Action;
using Lagrange.OneBot.Core.Entity.Message;
using Lagrange.OneBot.Core.Operation.Converters;
using Lagrange.OneBot.Message;
using Lagrange.OneBot.Database;
using Lagrange.OneBot.Core.Entity.Action.Response;
namespace Lagrange.OneBot.Core.Operation.Message;

using LiteDB;

[Operation("send_msg_with_group")]
public class SendMessageWithGroupOperation(LiteDatabase database) : IOperation
{
public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? payload)
{
if (payload.Deserialize<OneBotSendMessageWithGroup>(SerializerOptions.DefaultOptions) is { } history)
{
var record = database.GetCollection<MessageRecord>().FindById(history.MessageId);
var chain = (MessageChain)record;
if (chain.GroupUin == 0)
{
return new OneBotResult("messageid not group", -1, "failed");
}
uint groupUin = chain.GroupUin ?? 0;

if (await context.GetGroupMessageWithPushMsgBody(groupUin, (uint)chain.Sequence, (uint)chain.Sequence) is { } results)
{
var newMessageChain = new MessageChain(history.TargetGroupId);
var sendMsgRes = await context.SendMessage(newMessageChain, results.Item2?[0] ?? throw new Exception("No PushMsgBody found"));

if (sendMsgRes.Result != 0) return new OneBotResult(null, (int)sendMsgRes.Result, "failed");
if (sendMsgRes.Sequence == null || sendMsgRes.Sequence == 0) return new OneBotResult(null, 9000, "failed");
int hash = MessageRecord.CalcMessageHash(newMessageChain.MessageId, sendMsgRes.Sequence ?? 0);
return new OneBotResult(new OneBotMessageResponse(hash, (int)sendMsgRes.Sequence.GetValueOrDefault()), (int)sendMsgRes.Result, "ok");
}
}
throw new Exception();
}
}

0 comments on commit bcdcd74

Please sign in to comment.