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}.");
                }
            }
        }
Beispiel #2
0
        public async Task CheckDueScheduledMessages()
        {
            using var scope = _serviceProvider.CreateScope();
            var repo     = ScheduledMessageRepository.CreateWithBotIdentity(scope.ServiceProvider);
            var messages = await repo.GetDueMessages();

            foreach (ScheduledMessage message in messages)
            {
                _logger.LogInformation($"Handling scheduled message {message.Id} for {message.GuildId}/{message.ChannelId} by {message.CreatorId}/{message.LastEditedById}.");
                IGuild       guild;
                ITextChannel channel;
                try
                {
                    guild = _discordAPI.FetchGuildInfo(message.GuildId, CacheBehavior.OnlyCache);
                    if (guild == null)
                    {
                        throw new Exception();
                    }
                } catch (Exception)
                {
                    _logger.LogInformation($"Failed scheduled message {message.Id}. Reason unknown.");
                    await repo.SetMessageAsFailed(message.Id, ScheduledMessageFailureReason.Unknown);

                    continue;
                }

                try
                {
                    channel = await guild.GetTextChannelAsync(message.ChannelId);

                    if (channel == null)
                    {
                        throw new Exception();
                    }
                }
                catch (Exception)
                {
                    _logger.LogInformation($"Failed scheduled message {message.Id}. Reason channel not found.");
                    await repo.SetMessageAsFailed(message.Id, ScheduledMessageFailureReason.ChannelNotFound);

                    continue;
                }

                try
                {
                    await channel.SendMessageAsync(message.Content);

                    await repo.SetMessageAsSent(message.Id);

                    _logger.LogInformation($"Sent scheduled message {message.Id} for {message.GuildId}/{message.ChannelId} by {message.CreatorId}/{message.LastEditedById}.");
                }
                catch (HttpException e)
                {
                    if (e.HttpCode == HttpStatusCode.Unauthorized || e.HttpCode == HttpStatusCode.Forbidden)
                    {
                        _logger.LogInformation($"Failed scheduled message {message.Id}. Reason insufficient permission.");
                        await repo.SetMessageAsFailed(message.Id, ScheduledMessageFailureReason.InsufficientPermission);
                    }
                    else
                    {
                        _logger.LogInformation($"Failed scheduled message {message.Id}. Reason unknown");
                        await repo.SetMessageAsFailed(message.Id, ScheduledMessageFailureReason.Unknown);
                    }
                }
                catch (Exception)
                {
                    _logger.LogInformation($"Failed scheduled message {message.Id}. Reason unknown");
                    await repo.SetMessageAsFailed(message.Id, ScheduledMessageFailureReason.Unknown);
                }
            }
        }