Skip to content

Commit cb965a9

Browse files
kengwangCopilot
andauthored
[Core & Milky] Implement group_member_increase event (#71)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent dd58423 commit cb965a9

6 files changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace Lagrange.Core.Events.EventArgs;
2+
3+
public class BotGroupMemberIncreaseEvent(long groupUin, long memberUin, long invitorUin, uint type, long? operatorUin) : EventBase
4+
{
5+
public long GroupUin { get; } = groupUin;
6+
public long MemberUin { get; } = memberUin;
7+
public long InvitorUin { get; } = invitorUin;
8+
public uint Type { get; } = type;
9+
public long? OperatorUin { get; } = operatorUin;
10+
11+
12+
public override string ToEventMessage()
13+
{
14+
return $"{nameof(BotGroupMemberIncreaseEvent)}: GroupUin={GroupUin}, MemberUin={MemberUin}, InvitorUin={InvitorUin}, Type={Type}, OperatorUin={OperatorUin}";
15+
}
16+
}

Lagrange.Core/Internal/Logic/PushLogic.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text;
12
using System.Text.Json;
23
using System.Web;
34
using Lagrange.Core.Common;
@@ -54,6 +55,29 @@ public async ValueTask Incoming(ProtocolEvent e)
5455
context.EventInvoker.PostEvent(new BotMessageEvent(message, messageEvent.Raw));
5556
break;
5657
}
58+
case Type.GroupMemberIncreaseNotice when messageEvent.MsgPush.CommonMessage.MessageBody.MsgContent is { } content:
59+
{
60+
var increase = ProtoHelper.Deserialize<GroupChange>(content.Span);
61+
var response = await context.EventContext.SendEvent<FetchGroupNotificationsEventResp>(
62+
new FetchGroupNotificationsEventReq(20)
63+
);
64+
var inviteNotifications = response
65+
.GroupNotifications
66+
.OfType<BotGroupInviteNotification>();
67+
var notification = inviteNotifications.FirstOrDefault(notification =>
68+
increase.GroupUin == notification.GroupUin &&
69+
increase.MemberUid == notification.TargetUid &&
70+
notification.State == BotGroupNotificationState.Accept
71+
);
72+
var operatorUin = notification?.OperatorUin;
73+
context.EventInvoker.PostEvent(new BotGroupMemberIncreaseEvent(
74+
increase.GroupUin,
75+
context.CacheContext.ResolveUin(increase.MemberUid),
76+
context.CacheContext.ResolveUin(Encoding.UTF8.GetString(increase.Operator.AsSpan())),
77+
increase.IncreaseType,
78+
operatorUin));
79+
break;
80+
}
5781
case Type.GroupMemberDecreaseNotice when messageEvent.MsgPush.CommonMessage.MessageBody.MsgContent is { } content:
5882
{
5983
var decrease = ProtoHelper.Deserialize<GroupChange>(content.Span);
@@ -266,6 +290,7 @@ public async ValueTask Incoming(ProtocolEvent e)
266290

267291
private enum Type
268292
{
293+
GroupMemberIncreaseNotice = 33,
269294
GroupMemberDecreaseNotice = 34,
270295
GroupMessage = 82,
271296
GroupJoinNotification = 84,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace Lagrange.Milky.Entity.Event;
4+
5+
public class GroupMemberIncreaseEvent(long time, long selfId, GroupMemberIncreaseEventData data) : EventBase<GroupMemberIncreaseEventData>(time, selfId, "group_member_increase", data) { }
6+
7+
public class GroupMemberIncreaseEventData(long groupId, long userId, long? operatorId, long? invitorId = null)
8+
{
9+
[JsonPropertyName("group_id")]
10+
public long GroupId { get; } = groupId;
11+
[JsonPropertyName("user_id")]
12+
public long UserId { get; } = userId;
13+
[JsonPropertyName("operator_id")]
14+
public long? OperatorId { get; } = operatorId;
15+
[JsonPropertyName("invitor_id")]
16+
public long? InvitorId { get; } = invitorId;
17+
}

Lagrange.Milky/Event/EventService.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public Task StartAsync(CancellationToken token)
2929
_bot.EventInvoker.RegisterEvent<LgrEvents.BotOfflineEvent>(HandleOfflineEvent);
3030
_bot.EventInvoker.RegisterEvent<LgrEvents.BotMessageEvent>(HandleMessageEvent);
3131
_bot.EventInvoker.RegisterEvent<LgrEvents.BotGroupNudgeEvent>(HandleGroupNudgeEvent);
32+
_bot.EventInvoker.RegisterEvent<LgrEvents.BotGroupMemberIncreaseEvent>(HandleGroupMemberIncreaseEvent);
3233
_bot.EventInvoker.RegisterEvent<LgrEvents.BotGroupMemberDecreaseEvent>(HandleGroupMemberDecreaseEvent);
3334
_bot.EventInvoker.RegisterEvent<LgrEvents.BotFriendRequestEvent>(HandleFriendRequestEvent);
3435

@@ -124,6 +125,32 @@ private void HandleGroupNudgeEvent(BotContext bot, LgrEvents.BotGroupNudgeEvent
124125
}
125126
}
126127

128+
private void HandleGroupMemberIncreaseEvent(BotContext bot, LgrEvents.BotGroupMemberIncreaseEvent @event)
129+
{
130+
try
131+
{
132+
_logger.LogGroupMemberIncreaseEvent(
133+
@event.GroupUin,
134+
@event.MemberUin,
135+
@event.OperatorUin,
136+
@event.InvitorUin
137+
);
138+
var result = _convert.GroupMemberIncreaseEvent(@event);
139+
byte[] bytes = JsonUtility.SerializeToUtf8Bytes(result.GetType(), result);
140+
using (_lock.UsingReadLock())
141+
{
142+
foreach (var handler in _handlers)
143+
{
144+
handler(bytes);
145+
}
146+
}
147+
}
148+
catch (Exception e)
149+
{
150+
_logger.LogHandleEventException(nameof(LgrEvents.BotGroupMemberIncreaseEvent), e);
151+
}
152+
}
153+
127154
private void HandleGroupMemberDecreaseEvent(BotContext bot, LgrEvents.BotGroupMemberDecreaseEvent @event)
128155
{
129156
try
@@ -226,6 +253,9 @@ public static partial class EventServiceLoggerExtension
226253
[LoggerMessage(EventId = 6, Level = LogLevel.Debug, Message = "BotGroupInviteEvent {{ request: {request}, user: {user}, group: {group} }}")]
227254
public static partial void LogGroupInvitationEvent(this ILogger<EventService> logger, long request, long user, long group);
228255

256+
[LoggerMessage(EventId = 7, Level = LogLevel.Debug, Message = "BotGroupMemberIncreaseEvent {{ group: {group}, user: {user}, operator: {operator}, invitor: {invitor} }}")]
257+
public static partial void LogGroupMemberIncreaseEvent(this ILogger<EventService> logger, long group, long user, long? @operator, long? invitor);
258+
229259
[LoggerMessage(EventId = 999, Level = LogLevel.Error, Message = "Handle {event} exception")]
230260
public static partial void LogHandleEventException(this ILogger<EventService> logger, string @event, Exception e);
231261
}

Lagrange.Milky/Utility/EntityConvert.Event.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ public partial class EntityConvert
2424
new GroupNudgeEventData(@event.GroupUin, @event.OperatorUin, @event.TargetUin, @event.Action, @event.Suffix, @event.ActionImageUrl)
2525
);
2626

27+
public GroupMemberIncreaseEvent GroupMemberIncreaseEvent(LgrEventArgs.BotGroupMemberIncreaseEvent @event) => new(
28+
@event.EventTime.ToUnixTimeSeconds(),
29+
_bot.BotUin,
30+
new GroupMemberIncreaseEventData(
31+
@event.GroupUin,
32+
@event.MemberUin,
33+
@event.OperatorUin,
34+
@event.InvitorUin
35+
)
36+
);
37+
2738
public GroupMemberDecreaseEvent GroupMemberDecreaseEvent(LgrEventArgs.BotGroupMemberDecreaseEvent @event) => new(
2839
@event.EventTime.ToUnixTimeSeconds(),
2940
_bot.BotUin,

Lagrange.Milky/Utility/JsonUtility.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ public static partial class JsonUtility
117117
[JsonSerializable(typeof(MessageReceiveEvent))]
118118
// group_nudge
119119
[JsonSerializable(typeof(GroupNudgeEvent))]
120+
// group_member_increase
121+
[JsonSerializable(typeof(GroupMemberIncreaseEvent))]
120122
// group_member_decrease
121123
[JsonSerializable(typeof(GroupMemberDecreaseEvent))]
122124
// friend_request

0 commit comments

Comments
 (0)