public static async Task <EmbedBuilder> CreateInternalAutomodEmbed(this AutoModerationEvent autoModerationEvent, GuildConfig guildConfig, IUser user, ITextChannel channel, IServiceProvider provider, PunishmentType?punishmentType = null) { var translator = provider.GetService <Translator>(); await translator.SetContext(autoModerationEvent.GuildId); EmbedBuilder embed = CreateBasicEmbed(RestAction.Created, provider); embed.Title = translator.T().Automoderation(); embed.Description = translator.T().NotificationAutomoderationInternal(user); embed.AddField( translator.T().Channel(), channel.Mention, true ); embed.AddField( translator.T().Message(), $"[{autoModerationEvent.MessageId}](https://discord.com/channels/{autoModerationEvent.GuildId}/{channel.Id}/{autoModerationEvent.MessageId})", true ); embed.AddField( translator.T().Type(), translator.T().Enum(autoModerationEvent.AutoModerationType), false ); embed.AddField( translator.T().MessageContent(), autoModerationEvent.MessageContent, false ); embed.AddField( translator.T().Action(), translator.T().Enum(autoModerationEvent.AutoModerationAction), false ); if ((autoModerationEvent.AutoModerationAction == AutoModerationAction.CaseCreated || autoModerationEvent.AutoModerationAction == AutoModerationAction.ContentDeletedAndCaseCreated) && punishmentType != null) { embed.AddField( translator.T().Punishment(), translator.T().Enum(punishmentType.Value), false ); } embed.WithFooter($"GuildId: {guildConfig.GuildId}"); return(embed); }
public async Task SaveModerationEvent(AutoModerationEvent modEvent) { await context.AutoModerationEvents.AddAsync(modEvent); }
private async Task AnnounceAutomoderation(AutoModerationEvent modEvent, AutoModerationConfig moderationConfig, GuildConfig guildConfig, ITextChannel channel, IUser author) { using var scope = _serviceProvider.CreateScope(); var translator = scope.ServiceProvider.GetRequiredService <Translator>(); translator.SetContext(guildConfig); if (!string.IsNullOrEmpty(guildConfig.ModInternalNotificationWebhook)) { _logger.LogInformation($"Sending internal webhook for automod event {modEvent.GuildId}/{modEvent.Id} to {guildConfig.ModInternalNotificationWebhook}."); try { EmbedBuilder embed = await modEvent.CreateInternalAutomodEmbed(guildConfig, author, channel, scope.ServiceProvider, moderationConfig.PunishmentType); await _discordAPI.ExecuteWebhook(guildConfig.ModInternalNotificationWebhook, embed.Build()); } catch (Exception e) { _logger.LogError(e, $"Error while announcing automod event {modEvent.GuildId}/{modEvent.Id} to {guildConfig.ModInternalNotificationWebhook}."); } } if (moderationConfig.SendDmNotification) { _logger.LogInformation($"Sending dm notification for autmod event {modEvent.GuildId}/{modEvent.Id} to {author.Id}."); try { string reason = translator.T().Enum(modEvent.AutoModerationType); string action = translator.T().Enum(modEvent.AutoModerationAction); await _discordAPI.SendDmMessage(author.Id, translator.T().NotificationAutomoderationDM(author, channel, reason, action)); } catch (Exception e) { _logger.LogError(e, $"Error while announcing automod event {modEvent.GuildId}/{modEvent.Id} in dm to {author.Id}."); } } if ((modEvent.AutoModerationAction == AutoModerationAction.ContentDeleted || modEvent.AutoModerationAction == AutoModerationAction.ContentDeletedAndCaseCreated) && moderationConfig.ChannelNotificationBehavior != AutoModerationChannelNotificationBehavior.NoNotification) { _logger.LogInformation($"Sending channel notification to {modEvent.GuildId}/{modEvent.Id} {channel.GuildId}/{channel.Id}."); try { string reason = translator.T().Enum(modEvent.AutoModerationType); IMessage msg = await channel.SendMessageAsync(translator.T().NotificationAutomoderationChannel(author, reason)); if (moderationConfig.ChannelNotificationBehavior == AutoModerationChannelNotificationBehavior.SendNotificationAndDelete) { Task task = new(async() => { await Task.Delay(TimeSpan.FromSeconds(5)); try { _logger.LogInformation($"Deleting channel automod event notification {channel.GuildId}/{channel.Id}/{msg.Id}."); await msg.DeleteAsync(); } catch (UnauthorizedException) { } catch (Exception e) { _logger.LogError(e, $"Error while deleting message {channel.GuildId}/{channel.Id}/{msg.Id} for automod event {modEvent.GuildId}/{modEvent.Id}."); } }); task.Start(); } } catch (Exception e) { _logger.LogError(e, $"Error while announcing automod event {modEvent.GuildId}/{modEvent.Id} in channel {channel.Id}."); } } }
public async Task <IActionResult> CreateItem([FromRoute] string guildid, [FromBody] AutoModerationEventForCreateDto dto) { logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | Incoming request."); if (string.IsNullOrEmpty(this.config.Value.DiscordBotToken)) { logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 401 Authorization header not defined."); return(Unauthorized()); } string auth = String.Empty; try { auth = Request.Headers["Authorization"]; } catch (Exception e) { logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 401 Authorization header not defined.", e); return(Unauthorized()); } if (this.config.Value.DiscordBotToken == auth) { AutoModerationConfig modConfig = await this.database.SelectModerationConfigForGuildAndType(guildid, dto.AutoModerationType); if (modConfig == null) { logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 400 No config found for this type."); return(BadRequest("No config found for this type")); } AutoModerationEvent modEvent = new AutoModerationEvent(); if (modConfig.AutoModerationAction == AutoModerationAction.CaseCreated || modConfig.AutoModerationAction == AutoModerationAction.ContentDeletedAndCaseCreated) { ModCase newModCase = new ModCase(); User discordBot = await discord.FetchCurrentBotInfo(); newModCase.CaseId = await database.GetHighestCaseIdForGuild(guildid) + 1; newModCase.GuildId = guildid; newModCase.UserId = dto.UserId; newModCase.Username = dto.Username; newModCase.Discriminator = dto.Discriminator; newModCase.Nickname = dto.Nickname; newModCase.ModId = discordBot.Id; newModCase.CreatedAt = DateTime.UtcNow; newModCase.LastEditedAt = newModCase.CreatedAt; newModCase.LastEditedByModId = discordBot.Id; newModCase.OccuredAt = newModCase.CreatedAt; newModCase.Title = $"AutoModeration: {dto.AutoModerationType.ToString()}"; newModCase.Description = $"User triggered AutoModeration.\nEvent: {dto.AutoModerationType.ToString()}.\nAction: {modConfig.AutoModerationAction.ToString()}\nMessageId: {dto.MessageId}.\nMessage content: {dto.MessageContent}."; newModCase.Labels = new List <string>() { "automoderation", dto.AutoModerationType.ToString() }.ToArray(); newModCase.Valid = true; if (modConfig.PunishmentType != null && modConfig.PunishmentType != PunishmentType.None) { newModCase.PunishmentType = modConfig.PunishmentType.Value; newModCase.PunishmentActive = true; if (modConfig.PunishmentDurationMinutes == null) { newModCase.Punishment = newModCase.PunishmentType.ToString(); newModCase.PunishedUntil = null; } else { newModCase.Punishment = "Temp" + newModCase.PunishmentType.ToString(); newModCase.PunishedUntil = DateTime.UtcNow.AddMinutes(modConfig.PunishmentDurationMinutes.Value); } try { logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | Handling punishment."); await punishmentHandler.ExecutePunishment(newModCase, database); } catch (Exception e) { logger.LogError(e, "Failed to handle punishment for modcase."); } } else { newModCase.Punishment = "Warn"; newModCase.PunishmentType = PunishmentType.None; newModCase.PunishedUntil = null; newModCase.PunishmentActive = false; } await database.SaveModCase(newModCase); await database.SaveChangesAsync(); modEvent.AssociatedCaseId = newModCase.CaseId; logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | Sending notification."); try { await discordAnnouncer.AnnounceModCase(newModCase, RestAction.Created, discordBot, modConfig.SendPublicNotification); } catch (Exception e) { logger.LogError(e, "Failed to announce modcase."); } } modEvent.GuildId = guildid; modEvent.CreatedAt = DateTime.UtcNow; modEvent.UserId = dto.UserId; modEvent.Username = dto.Username; modEvent.Nickname = dto.Nickname; modEvent.Discriminator = dto.Discriminator; modEvent.MessageContent = dto.MessageContent; modEvent.MessageId = dto.MessageId; modEvent.AutoModerationType = dto.AutoModerationType; modEvent.AutoModerationAction = modConfig.AutoModerationAction; await database.SaveModerationEvent(modEvent); await database.SaveChangesAsync(); return(Ok(new { Id = modEvent.Id })); } else { logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 401 Unauthorized."); return(Unauthorized()); } }