-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityLifecycleHandler.cs
More file actions
72 lines (57 loc) · 3.33 KB
/
EntityLifecycleHandler.cs
File metadata and controls
72 lines (57 loc) · 3.33 KB
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
using BaseBotService.Commands.Interfaces;
using BaseBotService.Core.Messages;
using BaseBotService.Data.Interfaces;
using BaseBotService.Data.Models;
namespace BaseBotService.Interactions;
public class EntityLifecycleHandler : INotificationHandler<JoinedGuildNotification>, INotificationHandler<UserJoinedNotification>, INotificationHandler<LeftGuildNotification>, INotificationHandler<UserLeftNotification>
{
private readonly ILogger _logger;
private readonly IMemberRepository _memberRepository;
private readonly IGuildRepository _guildRepository;
private readonly IGuildMemberRepository _guildMemberRepository;
private readonly IEngagementService _engagementService;
public EntityLifecycleHandler(ILogger logger, IMemberRepository memberRepository, IGuildRepository guildRepository, IGuildMemberRepository guildMemberRepository, IEngagementService engagementService)
{
_logger = logger.ForContext<EntityLifecycleHandler>();
_memberRepository = memberRepository;
_guildRepository = guildRepository;
_guildMemberRepository = guildMemberRepository;
_engagementService = engagementService;
}
public Task Handle(LeftGuildNotification notification, CancellationToken cancellationToken)
{
_logger.Debug($"{nameof(EntityLifecycleHandler)} received {nameof(LeftGuildNotification)}");
bool success = _guildRepository.DeleteGuild(notification.Guild.Id);
_logger.Information($"Bot left guild {notification.Guild.Id}, deleted GuildHC entity: {success}.");
int amount = _guildMemberRepository.DeleteGuild(notification.Guild.Id);
_logger.Information($"Bot left guild {notification.Guild.Id}, deleted {amount} GuildMemberHC entities.");
return Task.CompletedTask;
}
public Task Handle(UserJoinedNotification notification, CancellationToken cancellationToken)
{
_logger.Debug($"{nameof(EntityLifecycleHandler)} received {nameof(UserJoinedNotification)}");
_ = _memberRepository.GetUser(notification.User.Id, true)!;
_engagementService.AddActivityTick(notification.User.Guild.Id, notification.User.Id);
_logger.Information($"User {notification.User.Id} joined {notification.User.Guild.Id}, created MemberHC (if necessary) and initialized first Activity-Tick.");
return Task.CompletedTask;
}
public Task Handle(JoinedGuildNotification notification, CancellationToken cancellationToken)
{
_logger.Debug($"{nameof(EntityLifecycleHandler)} received {nameof(JoinedGuildNotification)}");
_guildRepository.AddGuild(new GuildHC { GuildId = notification.Guild.Id });
_logger.Information($"Bot joined {notification.Guild.Id}, created GuildHC.");
return Task.CompletedTask;
}
public Task Handle(UserLeftNotification notification, CancellationToken cancellationToken)
{
_logger.Debug($"{nameof(EntityLifecycleHandler)} received {nameof(UserLeftNotification)}");
GuildMemberHC? member = _guildMemberRepository.GetUser(notification.Guild.Id, notification.User.Id);
if (member != null)
{
member.ActivityPoints = 0;
_guildMemberRepository.UpdateUser(member);
_logger.Information($"User {notification.User.Id} left {notification.Guild.Id}, reset ActivityPoints.");
}
return Task.CompletedTask;
}
}