public async Task <IActionResult> SetItem([FromRoute] ulong guildId, [FromBody] AutoModerationConfigForPutDto dto)
        {
            await RequirePermission(guildId, DiscordPermission.Admin);

            return(Ok(new AutoModerationConfigView(await AutoModerationConfigRepository.CreateDefault(_serviceProvider, (await GetIdentity()).GetCurrentUser())
                                                   .UpdateConfig(new AutoModerationConfig(dto, guildId)))));
        }
 public AutoModerationConfig(AutoModerationConfigForPutDto dto, ulong guildId)
 {
     GuildId                   = guildId;
     AutoModerationType        = dto.AutoModerationType;
     AutoModerationAction      = dto.AutoModerationAction;
     PunishmentType            = dto.PunishmentType;
     PunishmentDurationMinutes = dto.PunishmentDurationMinutes;
     IgnoreChannels            = dto.IgnoreChannels;
     IgnoreRoles               = dto.IgnoreRoles;
     TimeLimitMinutes          = dto.TimeLimitMinutes;
     Limit                       = dto.Limit;
     CustomWordFilter            = dto.CustomWordFilter;
     SendDmNotification          = dto.SendDmNotification;
     SendPublicNotification      = dto.SendPublicNotification;
     ChannelNotificationBehavior = dto.ChannelNotificationBehavior;
 }
Example #3
0
        public async Task <IActionResult> SetItem([FromRoute] string guildid, [FromBody] AutoModerationConfigForPutDto dto)
        {
            logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | Incoming request.");
            Identity currentIdentity = await identityManager.GetIdentity(HttpContext);

            User currentUser = await currentIdentity.GetCurrentDiscordUser();

            if (currentUser == null)
            {
                logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 401 Unauthorized.");
                return(Unauthorized());
            }
            if (!await currentIdentity.HasAdminRoleOnGuild(guildid, this.database) && !config.Value.SiteAdminDiscordUserIds.Contains(currentUser.Id))
            {
                logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 401 Unauthorized.");
                return(Unauthorized());
            }
            // ========================================================

            GuildConfig guildConfig = await database.SelectSpecificGuildConfig(guildid);

            if (guildConfig == null)
            {
                logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 400 Guild not registered.");
                return(BadRequest("Guild not registered."));
            }

            AutoModerationConfig currentConfig = await database.SelectModerationConfigForGuildAndType(guildid, dto.AutoModerationType);

            if (currentConfig == null)
            {
                currentConfig = new AutoModerationConfig();
            }

            if (!Enum.IsDefined(typeof(AutoModerationType), dto.AutoModerationType))
            {
                logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 400 Invalid moderation type.");
                return(BadRequest("Invalid moderation type"));
            }

            if (!Enum.IsDefined(typeof(AutoModerationAction), dto.AutoModerationAction))
            {
                logger.LogInformation($"{HttpContext.Request.Method} {HttpContext.Request.Path} | 400 Invalid action type.");
                return(BadRequest("Invalid action type"));
            }

            currentConfig.AutoModerationType   = dto.AutoModerationType;
            currentConfig.AutoModerationAction = dto.AutoModerationAction;
            currentConfig.GuildId                   = guildid;
            currentConfig.IgnoreChannels            = dto.IgnoreChannels.Distinct().ToArray();
            currentConfig.IgnoreRoles               = dto.IgnoreRoles.Distinct().ToArray();
            currentConfig.Limit                     = dto.Limit;
            currentConfig.TimeLimitMinutes          = dto.TimeLimitMinutes;
            currentConfig.PunishmentType            = dto.PunishmentType;
            currentConfig.PunishmentDurationMinutes = dto.PunishmentDurationMinutes;
            currentConfig.SendPublicNotification    = dto.SendPublicNotification;
            currentConfig.SendDmNotification        = dto.SendDmNotification;

            database.PutModerationConfig(currentConfig);
            await database.SaveChangesAsync();

            return(Ok(new { Id = currentConfig.Id, GuildId = currentConfig.GuildId, Type = currentConfig.AutoModerationType }));
        }