Skip to content
This repository was archived by the owner on Oct 12, 2025. It is now read-only.

Commit 88f1e64

Browse files
authored
[OneBot] Support get group requests (#787)
1 parent 43646ee commit 88f1e64

File tree

7 files changed

+89
-11
lines changed

7 files changed

+89
-11
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace Lagrange.Core.Common.Entity;
4+
5+
public class OperationResult<T>
6+
{
7+
public int Retcode { get; init; }
8+
9+
public string? Message { get; init; }
10+
11+
public T? Data { get; init; }
12+
13+
[MemberNotNullWhen(true, nameof(Data))]
14+
[MemberNotNullWhen(false, nameof(Message))]
15+
public bool IsSuccess => Retcode == 0;
16+
}

Lagrange.Core/Common/Interface/Api/OperationExt.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ public static Task<bool> RecallFriendMessage(this BotContext bot, MessageChain c
107107
public static Task<List<BotGroupRequest>?> FetchGroupRequests(this BotContext bot)
108108
=> bot.ContextCollection.Business.OperationLogic.FetchGroupRequests();
109109

110+
public static Task<OperationResult<List<BotGroupRequest>>> FetchGroupRequestsWithResults(this BotContext bot)
111+
=> bot.ContextCollection.Business.OperationLogic.FetchGroupRequestsWithResult();
112+
110113
/// <summary>
111114
///
112115
/// </summary>

Lagrange.Core/Internal/Context/Logic/Implementation/OperationLogic.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,15 @@ public async Task<bool> RecallFriendMessage(MessageChain chain)
352352
return events.Count != 0 && ((RecallFriendMessageEvent)events[0]).ResultCode == 0;
353353
}
354354

355-
public async Task<List<BotGroupRequest>?> FetchGroupRequests()
355+
public async Task<OperationResult<List<BotGroupRequest>>> FetchGroupRequestsWithResult()
356356
{
357357
var fetchRequestsEvent = FetchGroupRequestsEvent.Create();
358358
var events = await Collection.Business.SendEvent(fetchRequestsEvent);
359-
if (events.Count == 0) return null;
359+
if (events.Count == 0) return new OperationResult<List<BotGroupRequest>>
360+
{
361+
Retcode = -1,
362+
Message = "No Events"
363+
};
360364

361365
var resolved = events.Cast<FetchGroupRequestsEvent>().SelectMany(e => e.Events).ToList();
362366
var results = new List<BotGroupRequest>();
@@ -385,7 +389,11 @@ public async Task<bool> RecallFriendMessage(MessageChain chain)
385389
));
386390
}
387391

388-
return results;
392+
return new OperationResult<List<BotGroupRequest>>
393+
{
394+
Retcode = 0,
395+
Data = results
396+
};
389397

390398
async Task<uint> ResolveUid(string? uid)
391399
{
@@ -397,6 +405,8 @@ async Task<uint> ResolveUid(string? uid)
397405
}
398406
}
399407

408+
public async Task<List<BotGroupRequest>?> FetchGroupRequests() => (await FetchGroupRequestsWithResult()).Data;
409+
400410
public async Task<List<BotFriendRequest>?> FetchFriendRequests()
401411
{
402412
var fetchRequestsEvent = FetchFriendsRequestsEvent.Create();

Lagrange.Core/Internal/Event/System/FetchGroupRequestsEvent.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,22 @@ namespace Lagrange.Core.Internal.Event.System;
55
internal class FetchGroupRequestsEvent : ProtocolEvent
66
{
77
public List<RawEvent> Events { get; }
8-
8+
9+
public string? Message { get; }
10+
911
private FetchGroupRequestsEvent() : base(true) { }
1012

11-
private FetchGroupRequestsEvent(int resultCode, List<RawEvent> events) : base(resultCode)
13+
private FetchGroupRequestsEvent(int resultCode, string? message, List<RawEvent> events) : base(resultCode)
1214
{
15+
Message = message;
1316
Events = events;
1417
}
1518

1619
public static FetchGroupRequestsEvent Create() => new();
17-
18-
public static FetchGroupRequestsEvent Result(int resultCode, List<RawEvent> events) => new(resultCode, events);
20+
21+
public static FetchGroupRequestsEvent Result(List<RawEvent> events) => new(0, null, events);
22+
23+
public static FetchGroupRequestsEvent Result(int resultCode, string message) => new(resultCode, message, new List<RawEvent>());
1924

2025
public record RawEvent(
2126
uint GroupUin,

Lagrange.Core/Internal/Service/System/FetchFilteredGroupRequestsService.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ protected override bool Parse(Span<byte> input, BotKeystore keystore, BotAppInfo
4545
x.Comment,
4646
true
4747
)).ToList() ?? new List<FetchGroupRequestsEvent.RawEvent>();
48-
49-
output = FetchGroupRequestsEvent.Result((int)payload.ErrorCode, events);
48+
49+
output = payload.ErrorCode == 0
50+
? FetchGroupRequestsEvent.Result(events)
51+
: FetchGroupRequestsEvent.Result((int)payload.ErrorCode, payload.ErrorMsg);
5052
extraEvents = null;
5153
return true;
5254
}

Lagrange.Core/Internal/Service/System/FetchGroupRequestsService.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ protected override bool Parse(Span<byte> input, BotKeystore keystore, BotAppInfo
4545
x.Comment,
4646
false
4747
)).ToList() ?? new List<FetchGroupRequestsEvent.RawEvent>();
48-
49-
output = FetchGroupRequestsEvent.Result((int)payload.ErrorCode, events);
48+
49+
output = (payload.ErrorCode == 0)
50+
? FetchGroupRequestsEvent.Result(events)
51+
: FetchGroupRequestsEvent.Result((int)payload.ErrorCode, payload.ErrorMsg);
5052
extraEvents = null;
5153
return true;
5254
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Text.Json.Nodes;
2+
using Lagrange.Core;
3+
using Lagrange.Core.Common.Interface.Api;
4+
using Lagrange.OneBot.Core.Entity.Action;
5+
using Lagrange.OneBot.Core.Entity.Notify;
6+
7+
namespace Lagrange.OneBot.Core.Operation.Request;
8+
9+
[Operation("get_group_requests")]
10+
public class GetGroupRequestsOperation : IOperation
11+
{
12+
public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? payload)
13+
{
14+
var result = await context.FetchGroupRequestsWithResults();
15+
if (!result.IsSuccess) return new OneBotResult(null, result.Retcode, "failed");
16+
17+
var requests = result.Data.Select(r =>
18+
{
19+
string? type = r.EventType switch
20+
{
21+
Lagrange.Core.Common.Entity.BotGroupRequest.Type.GroupRequest => "add",
22+
Lagrange.Core.Common.Entity.BotGroupRequest.Type.SelfInvitation or
23+
Lagrange.Core.Common.Entity.BotGroupRequest.Type.GroupInvitation => "invite",
24+
_ => null
25+
};
26+
if (type == null) return null;
27+
28+
return new OneBotGroupRequest(
29+
context.BotUin,
30+
r.TargetMemberUin,
31+
r.GroupUin,
32+
type,
33+
r.Comment,
34+
$"{r.Sequence}-{r.GroupUin}-{(uint)r.EventType}-{Convert.ToInt32(r.IsFiltered)}"
35+
);
36+
})
37+
.Where(r => r != null);
38+
return new OneBotResult(requests, 0, "ok");
39+
}
40+
}

0 commit comments

Comments
 (0)