private Task GuildMemberUpdatedHandler(Cacheable <SocketGuildUser, ulong> oldUsrCached, SocketGuildUser newUsr) { using var scope = _serviceProvider.CreateScope(); // Refresh identity memberships IdentityManager identityManager = scope.ServiceProvider.GetRequiredService <IdentityManager>(); foreach (Identity identity in identityManager.GetCurrentIdentities()) { if (identity.GetCurrentUser().Id == newUsr.Id) { identity.UpdateGuildMembership(newUsr); } } // Refresh member cache DiscordAPIInterface discordAPI = scope.ServiceProvider.GetRequiredService <DiscordAPIInterface>(); discordAPI.AddOrUpdateCache(CacheKey.GuildMember(newUsr.Id, newUsr.Id), new CacheApiResponse(newUsr)); return(Task.CompletedTask); }
private async Task GuildMemberAddedHandler(SocketGuildUser member) { using var scope = _serviceProvider.CreateScope(); var translator = scope.ServiceProvider.GetRequiredService <Translator>(); await translator.SetContext(member.Guild.Id); // Refresh identity memberships IdentityManager identityManager = scope.ServiceProvider.GetRequiredService <IdentityManager>(); foreach (Identity identity in identityManager.GetCurrentIdentities()) { if (identity.GetCurrentUser().Id == member.Id) { identity.AddGuildMembership(member); } } // Refresh member cache DiscordAPIInterface discordAPI = scope.ServiceProvider.GetRequiredService <DiscordAPIInterface>(); discordAPI.AddOrUpdateCache(CacheKey.GuildMember(member.Guild.Id, member.Id), new CacheApiResponse(member)); GuildConfig guildConfig; try { guildConfig = await GuildConfigRepository.CreateDefault(scope.ServiceProvider).GetGuildConfig(member.Guild.Id); } catch (ResourceNotFoundException) { return; } try { Punishments handler = scope.ServiceProvider.GetRequiredService <Punishments>(); await handler.HandleMemberJoin(member); } catch (Exception ex) { _logger.LogError(ex, "Failed to handle punishment on member join."); } if (member.IsBot) { return; } List <TrackedInvite> newInvites = await FetchInvites(member.Guild); TrackedInvite usedInvite = null; try { usedInvite = InviteTracker.GetUsedInvite(member.Guild.Id, newInvites); } catch (Exception ex) { _logger.LogError(ex, "Failed to get used invite."); } InviteTracker.AddInvites(member.Guild.Id, newInvites); if (usedInvite != null) { UserInvite invite = new() { GuildId = member.Guild.Id, JoinedUserId = member.Id, JoinedAt = DateTime.UtcNow, InviteIssuerId = usedInvite.CreatorId, InviteCreatedAt = usedInvite.CreatedAt, TargetChannelId = usedInvite.TargetChannelId, UsedInvite = $"https://discord.gg/{usedInvite.Code}" }; _logger.LogInformation($"User {member.Username}#{member.Discriminator} joined guild {member.Guild.Name} with ID: {member.Guild.Id} using invite {usedInvite.Code}"); if (guildConfig.ExecuteWhoisOnJoin && !string.IsNullOrEmpty(guildConfig.ModInternalNotificationWebhook)) { string message; if (invite.InviteIssuerId != 0 && invite.InviteCreatedAt != null) { message = translator.T().NotificationAutoWhoisJoinWithAndFrom(member, invite.InviteIssuerId, invite.InviteCreatedAt.Value, member.CreatedAt.DateTime, invite.UsedInvite); } else { message = translator.T().NotificationAutoWhoisJoinWith(member, member.CreatedAt.DateTime, invite.UsedInvite); } await discordAPI.ExecuteWebhook(guildConfig.ModInternalNotificationWebhook, null, message, AllowedMentions.None); } await InviteRepository.CreateDefault(scope.ServiceProvider).CreateInvite(invite); } }