public static void ShouldMatchTestData(this GuildRoleBrief brief) { brief.ShouldNotBeNull(); brief.Id.ShouldBeOneOf(GuildRoles.Entities.Select(x => x.RoleId).ToArray()); var entity = GuildRoles.Entities.First(x => x.RoleId == brief.Id); brief.Name.ShouldBe(entity.Name); }
/// <summary> /// Creates an <see cref="InsufficientRankResult"/> using the given instances of <see cref="GuildRoleBrief"/> /// </summary> /// <param name="requiredRole">The role that was required by the request</param> /// <param name="currentRole">The role that the user had</param> public InsufficientRankResult(GuildRoleBrief requiredRole, GuildRoleBrief currentRole) { if (requiredRole == null || currentRole == null) { IsSuccess = false; Error = "Either you or the target user are missing roles that are configured as \"Rank\"s. Please check your Modix config."; return; } RequiredRoleId = requiredRole.Id; RequiredRoleName = requiredRole.Name; CurrentRoleId = currentRole.Id; CurrentRoleName = currentRole.Name; IsSuccess = false; Error = ToString(); }
private bool TryGetNextRankRoleForUser( GuildRoleBrief[] rankRoles, IGuildUser subject, out GuildRoleBrief nextRankRole, out string message) { var userRankRoles = rankRoles.Where(r => subject.RoleIds.Contains(r.Id)); var userCurrentRankRole = userRankRoles.OrderByDescending(r => r.Position).FirstOrDefault(); if (userCurrentRankRole is null) { nextRankRole = rankRoles.OrderBy(r => r.Position).FirstOrDefault(); if (nextRankRole is null) { message = "There are no rank roles defined."; return(false); } else { message = null; return(true); } } else { nextRankRole = rankRoles.OrderBy(x => x.Position).FirstOrDefault(r => r.Position > userCurrentRankRole.Position); if (nextRankRole is null) { message = $"There are no rank roles available for {subject.GetFullUsername()} to be promoted to."; return(false); } else { message = null; return(true); } } }
private async Task PerformCommonCreateCampaignValidationsAsync(IGuildUser subject, GuildRoleBrief targetRankRole, IEnumerable <GuildRoleBrief> rankRoles) { if (await PromotionCampaignRepository.AnyAsync(new PromotionCampaignSearchCriteria() { GuildId = AuthorizationService.CurrentGuildId.Value, SubjectId = subject.Id, TargetRoleId = targetRankRole.Id, IsClosed = false })) { throw new InvalidOperationException($"An active campaign already exists for {subject.GetFullUsername()} to be promoted to {targetRankRole.Name}"); } // JoinedAt is null, when it cannot be obtained if (subject.JoinedAt.HasValue) { if (subject.JoinedAt.Value.DateTime > (DateTimeOffset.Now - TimeSpan.FromDays(20))) { throw new InvalidOperationException($"{subject.GetFullUsername()} has joined less than 20 days prior"); } } if (!await CheckIfUserIsRankOrHigherAsync(rankRoles, AuthorizationService.CurrentUserId.Value, targetRankRole.Id)) { throw new InvalidOperationException($"Creating a promotion campaign requires a rank at least as high as the proposed target rank"); } }
private async Task PerformCommonCreateCampaignValidationsAsync(IGuildUser subject, GuildRoleBrief targetRankRole, IEnumerable <GuildRoleBrief> rankRoles) { if (await PromotionCampaignRepository.AnyAsync(new PromotionCampaignSearchCriteria() { GuildId = AuthorizationService.CurrentGuildId.Value, SubjectId = subject.Id, TargetRoleId = targetRankRole.Id, IsClosed = false })) { throw new InvalidOperationException($"An active campaign already exists for {subject.GetDisplayNameWithDiscriminator()} to be promoted to {targetRankRole.Name}"); } if (!await CheckIfUserIsRankOrHigherAsync(rankRoles, AuthorizationService.CurrentUserId.Value, targetRankRole.Id)) { throw new InvalidOperationException($"Creating a promotion campaign requires a rank at least as high as the proposed target rank"); } }
private async Task <bool> CanUserMaintainTagOwnedByRoleAsync(IGuildUser currentUser, GuildRoleBrief ownerRole) { Debug.Assert(!(ownerRole is null)); var rankRoles = await GetRankRolesAsync(currentUser.GuildId); // If the owner role is no longer ranked, everything outranks it. if (!rankRoles.Any(x => x.Id == ownerRole.Id)) { return(true); } var currentUserRankRoles = rankRoles.Where(r => currentUser.RoleIds.Contains(r.Id)); var currentUserMaxRank = currentUserRankRoles.Any() ? currentUserRankRoles.Select(x => x.Position).Max() : int.MinValue; // Only allow maintenance if the user has sufficient rank. return(currentUserMaxRank >= ownerRole.Position); }