private async Task AnnounceModCase(ModCase modCase, IUser actor, bool announcePublic, bool announceDm, RestAction action)
        {
            using var scope = _serviceProvider.CreateScope();

            _logger.LogInformation($"Announcing modcase {modCase.Id} in guild {modCase.GuildId}.");

            var translator = scope.ServiceProvider.GetRequiredService <Translator>();

            IUser caseUser = await _discordAPI.FetchUserInfo(modCase.UserId, CacheBehavior.Default);

            GuildConfig guildConfig = await GuildConfigRepository.CreateDefault(scope.ServiceProvider).GetGuildConfig(modCase.GuildId);

            translator.SetContext(guildConfig);

            if (announceDm && action != RestAction.Deleted)
            {
                _logger.LogInformation($"Sending dm notification to {modCase.UserId} for case {modCase.GuildId}/{modCase.CaseId}");

                try
                {
                    IGuild guild   = _discordAPI.FetchGuildInfo(modCase.GuildId, CacheBehavior.Default);
                    string message = string.Empty;
                    switch (modCase.PunishmentType)
                    {
                    case PunishmentType.Mute:
                        if (modCase.PunishedUntil.HasValue)
                        {
                            message = translator.T().NotificationModcaseDMMuteTemp(modCase, guild, _config.GetBaseUrl());
                        }
                        else
                        {
                            message = translator.T().NotificationModcaseDMMutePerm(guild, _config.GetBaseUrl());
                        }
                        break;

                    case PunishmentType.Kick:
                        message = translator.T().NotificationModcaseDMKick(guild, _config.GetBaseUrl());
                        break;

                    case PunishmentType.Ban:
                        if (modCase.PunishedUntil.HasValue)
                        {
                            message = translator.T().NotificationModcaseDMBanTemp(modCase, guild, _config.GetBaseUrl());
                        }
                        else
                        {
                            message = translator.T().NotificationModcaseDMBanPerm(guild, _config.GetBaseUrl());
                        }
                        break;

                    default:
                        message = translator.T().NotificationModcaseDMWarn(guild, _config.GetBaseUrl());
                        break;
                    }
                    await _discordAPI.SendDmMessage(modCase.UserId, message);
                }
                catch (Exception e)
                {
                    _logger.LogError(e, $"Error while announcing modcase {modCase.GuildId}/{modCase.CaseId} in DMs to {modCase.UserId}.");
                }
            }

            if (!string.IsNullOrEmpty(guildConfig.ModPublicNotificationWebhook) && announcePublic)
            {
                _logger.LogInformation($"Sending public webhook for modcase {modCase.GuildId}/{modCase.CaseId} to {guildConfig.ModPublicNotificationWebhook}.");

                try
                {
                    EmbedBuilder embed = await modCase.CreateModcaseEmbed(action, actor, scope.ServiceProvider, caseUser, false);

                    await _discordAPI.ExecuteWebhook(guildConfig.ModPublicNotificationWebhook, embed.Build(), $"<@{modCase.UserId}>");
                }
                catch (Exception e)
                {
                    _logger.LogError(e, $"Error while announcing modcase {modCase.GuildId}/{modCase.CaseId} public to {guildConfig.ModPublicNotificationWebhook}.");
                }
            }

            if (!string.IsNullOrEmpty(guildConfig.ModInternalNotificationWebhook))
            {
                _logger.LogInformation($"Sending internal webhook for modcase {modCase.GuildId}/{modCase.CaseId} to {guildConfig.ModInternalNotificationWebhook}.");

                try
                {
                    EmbedBuilder embed = await modCase.CreateModcaseEmbed(action, actor, scope.ServiceProvider, caseUser, true);

                    await _discordAPI.ExecuteWebhook(guildConfig.ModInternalNotificationWebhook, embed.Build(), $"<@{modCase.UserId}>");
                }
                catch (Exception e)
                {
                    _logger.LogError(e, $"Error while announcing modcase {modCase.GuildId}/{modCase.CaseId} internal to {guildConfig.ModInternalNotificationWebhook}.");
                }
            }
        }
Exemple #2
0
        public async Task <List <ulong> > CacheAllKnownUsers(List <ulong> handledUsers)
        {
            _logger.LogInformation("Cacher | Cache all known users.");
            using (var scope = _serviceProvider.CreateScope())
            {
                Database database = scope.ServiceProvider.GetRequiredService <Database>();

                foreach (var modCase in await database.SelectLatestModCases(DateTime.UtcNow.AddYears(-3), 750))
                {
                    if (!handledUsers.Contains(modCase.UserId))
                    {
                        await _discordAPI.FetchUserInfo(modCase.UserId, CacheBehavior.IgnoreCache);

                        handledUsers.Add(modCase.UserId);
                    }
                    if (!handledUsers.Contains(modCase.ModId))
                    {
                        await _discordAPI.FetchUserInfo(modCase.ModId, CacheBehavior.IgnoreCache);

                        handledUsers.Add(modCase.ModId);
                    }
                    if (!handledUsers.Contains(modCase.LastEditedByModId))
                    {
                        await _discordAPI.FetchUserInfo(modCase.LastEditedByModId, CacheBehavior.IgnoreCache);

                        handledUsers.Add(modCase.LastEditedByModId);
                    }
                }

                foreach (var userNote in await database.SelectLatestUserNotes(DateTime.UtcNow.AddYears(-3), 100))
                {
                    if (!handledUsers.Contains(userNote.UserId))
                    {
                        await _discordAPI.FetchUserInfo(userNote.UserId, CacheBehavior.IgnoreCache);

                        handledUsers.Add(userNote.UserId);
                    }
                    if (!handledUsers.Contains(userNote.CreatorId))
                    {
                        await _discordAPI.FetchUserInfo(userNote.CreatorId, CacheBehavior.IgnoreCache);

                        handledUsers.Add(userNote.CreatorId);
                    }
                }

                foreach (var userMapping in await database.SelectLatestUserMappings(DateTime.UtcNow.AddYears(-3), 100))
                {
                    if (!handledUsers.Contains(userMapping.UserA))
                    {
                        await _discordAPI.FetchUserInfo(userMapping.UserA, CacheBehavior.IgnoreCache);

                        handledUsers.Add(userMapping.UserA);
                    }
                    if (!handledUsers.Contains(userMapping.UserB))
                    {
                        await _discordAPI.FetchUserInfo(userMapping.UserB, CacheBehavior.IgnoreCache);

                        handledUsers.Add(userMapping.UserB);
                    }
                    if (!handledUsers.Contains(userMapping.CreatorUserId))
                    {
                        await _discordAPI.FetchUserInfo(userMapping.CreatorUserId, CacheBehavior.IgnoreCache);

                        handledUsers.Add(userMapping.CreatorUserId);
                    }
                }
            }
            return(handledUsers);
        }