-
Notifications
You must be signed in to change notification settings - Fork 240
/
NotifyService.cs
164 lines (144 loc) · 7.37 KB
/
NotifyService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using Lagrange.Core;
using Lagrange.Core.Common.Entity;
using Lagrange.Core.Common.Interface.Api;
using Lagrange.Core.Event.EventArg;
using Lagrange.Core.Message.Entity;
using Lagrange.OneBot.Core.Entity.Notify;
using Lagrange.OneBot.Core.Network;
using Lagrange.OneBot.Database;
using Microsoft.Extensions.Logging;
namespace Lagrange.OneBot.Core.Notify;
public sealed class NotifyService(BotContext bot, ILogger<NotifyService> logger, LagrangeWebSvcCollection service)
{
public void RegisterEvents()
{
bot.Invoker.OnGroupMessageReceived += async (_, @event) =>
{
if (@event.Chain.GetEntity<FileEntity>() is { FileId: { } id } file)
{
var fileInfo = new OneBotFileInfo(id, file.FileName, (ulong)file.FileSize, file.FileUrl ?? "");
await service.SendJsonAsync(new OneBotGroupFile(bot.BotUin, @event.Chain.GroupUin ?? 0, @event.Chain.FriendUin, fileInfo));
}
};
bot.Invoker.OnFriendMessageReceived += async (_, @event) =>
{
if (@event.Chain.GetEntity<FileEntity>() is { FileUrl: { } url } file)
{
var fileInfo = new OneBotFileInfo(file.FileId ?? "", file.FileName, (ulong)file.FileSize, url);
await service.SendJsonAsync(new OneBotPrivateFile(bot.BotUin, @event.Chain.FriendUin, fileInfo));
}
};
bot.Invoker.OnGroupMuteEvent += async (_, @event) =>
{
logger.LogInformation(@event.ToString());
await service.SendJsonAsync(new OneBotGroupMute(bot.BotUin, @event.IsMuted ? "ban" : "lift_ban", @event.GroupUin, @event.OperatorUin ?? 0, 0, @event.IsMuted ? -1 : 0));
};
bot.Invoker.OnFriendRequestEvent += async (_, @event) =>
{
logger.LogInformation(@event.ToString());
await service.SendJsonAsync(new OneBotFriendRequestNotice(bot.BotUin, @event.SourceUin));
await service.SendJsonAsync(new OneBotFriendRequest(bot.BotUin, @event.SourceUin, @event.Message, @event.SourceUid));
};
bot.Invoker.OnGroupInvitationReceived += async (_, @event) =>
{
logger.LogInformation(@event.ToString());
var requests = await bot.FetchGroupRequests();
if (requests?.FirstOrDefault(x => @event.GroupUin == x.GroupUin && @event.InvitorUin == x.InvitorMemberUin) is { } request)
{
string flag = $"{request.Sequence}-{request.GroupUin}-{(uint)request.EventType}";
await service.SendJsonAsync(new OneBotGroupRequest(bot.BotUin, @event.InvitorUin, @event.GroupUin, "invite", request.Comment, flag));
}
};
bot.Invoker.OnGroupJoinRequestEvent += async (_, @event) =>
{
logger.LogInformation(@event.ToString());
var requests = await bot.FetchGroupRequests();
if (requests?.FirstOrDefault(x => @event.GroupUin == x.GroupUin && @event.TargetUin == x.TargetMemberUin) is { } request)
{
string flag = $"{request.Sequence}-{request.GroupUin}-{(uint)request.EventType}";
await service.SendJsonAsync(new OneBotGroupRequest(bot.BotUin, @event.TargetUin, @event.GroupUin, "add", request.Comment, flag));
}
};
bot.Invoker.OnGroupInvitationRequestEvent += async (_, @event) =>
{
logger.LogInformation(@event.ToString());
var requests = await bot.FetchGroupRequests();
if (requests?.FirstOrDefault(x => @event.GroupUin == x.GroupUin && @event.TargetUin == x.TargetMemberUin) is { } request)
{
string flag = $"{request.Sequence}-{request.GroupUin}-{(uint)request.EventType}";
await service.SendJsonAsync(new OneBotGroupRequest(bot.BotUin, @event.TargetUin, @event.GroupUin, "add", request.Comment, flag) { InvitorId = @event.InvitorUin });
}
};
bot.Invoker.OnGroupAdminChangedEvent += async (_, @event) =>
{
logger.LogInformation(@event.ToString());
await service.SendJsonAsync(new OneBotGroupAdmin(bot.BotUin, @event.IsPromote ? "set" : "unset", @event.GroupUin, @event.AdminUin));
};
bot.Invoker.OnGroupMemberIncreaseEvent += async (_, @event) =>
{
logger.LogInformation(@event.ToString());
string type = @event.Type.ToString().ToLower();
await service.SendJsonAsync(new OneBotMemberIncrease(bot.BotUin, type, @event.GroupUin, @event.InvitorUin ?? 0, @event.MemberUin));
};
bot.Invoker.OnGroupMemberDecreaseEvent += async (_, @event) =>
{
logger.LogInformation(@event.ToString());
BotGroupRequest? botGroupRequest = (await bot.ContextCollection.Business.OperationLogic.FetchGroupRequests())
?.AsParallel()
.FirstOrDefault(r =>
{
return @event.Type switch
{
GroupMemberDecreaseEvent.EventType.Kick => r.EventType == BotGroupRequest.Type.KickMember
&& r.TargetMemberUin == @event.MemberUin,
GroupMemberDecreaseEvent.EventType.KickMe => r.EventType == BotGroupRequest.Type.KickSelf,
_ => false
} && r.GroupUin == @event.GroupUin;
});
string type = @event.Type switch
{
GroupMemberDecreaseEvent.EventType.KickMe => "kick_me",
GroupMemberDecreaseEvent.EventType.Leave => "leave",
GroupMemberDecreaseEvent.EventType.Kick => "kick",
_ => @event.Type.ToString()
};
await service.SendJsonAsync(new OneBotMemberDecrease(bot.BotUin, type, @event.GroupUin, botGroupRequest?.InvitorMemberUin ?? 0, @event.MemberUin));
};
bot.Invoker.OnGroupMemberMuteEvent += async (_, @event) =>
{
logger.LogInformation(@event.ToString());
string type = @event.Duration == 0 ? "lift_ban" : "ban";
await service.SendJsonAsync(new OneBotGroupMute(bot.BotUin, type, @event.GroupUin, @event.OperatorUin ?? 0, @event.TargetUin, @event.Duration));
};
bot.Invoker.OnGroupRecallEvent += async (_, @event) =>
{
logger.LogInformation(@event.ToString());
await service.SendJsonAsync(new OneBotGroupRecall(bot.BotUin)
{
GroupId = @event.GroupUin,
UserId = @event.AuthorUin,
MessageId = MessageRecord.CalcMessageHash(@event.Random, @event.Sequence),
OperatorId = @event.OperatorUin
});
};
bot.Invoker.OnFriendRecallEvent += async (_, @event) =>
{
logger.LogInformation(@event.ToString());
await service.SendJsonAsync(new OneBotFriendRecall(bot.BotUin)
{
UserId = @event.FriendUin,
MessageId = MessageRecord.CalcMessageHash(@event.Random, @event.Sequence),
});
};
bot.Invoker.OnFriendPokeEvent += async (_, @event) =>
{
logger.LogInformation(@event.ToString());
await service.SendJsonAsync(new OneBotFriendPoke(bot.BotUin)
{
SenderId = @event.FriendUin,
UserId = @event.FriendUin,
TargetId = bot.BotUin
});
};
}
}