Пример #1
0
        public async Task <IRole> GetOrCreateDesignatedMuteRoleAsync(IGuild guild, ulong currentUserId)
        {
            using (var transaction = await DesignatedRoleMappingRepository.BeginCreateTransactionAsync())
            {
                var mapping = (await DesignatedRoleMappingRepository.SearchBriefsAsync(new DesignatedRoleMappingSearchCriteria()
                {
                    GuildId = guild.Id,
                    Type = DesignatedRoleType.ModerationMute,
                    IsDeleted = false
                })).FirstOrDefault();

                if (!(mapping is null))
                {
                    return(guild.Roles.First(x => x.Id == mapping.Role.Id));
                }

                var role = guild.Roles.FirstOrDefault(x => x.Name == MuteRoleName)
                           ?? await guild.CreateRoleAsync(MuteRoleName);

                await DesignatedRoleMappingRepository.CreateAsync(new DesignatedRoleMappingCreationData()
                {
                    GuildId     = guild.Id,
                    RoleId      = role.Id,
                    Type        = DesignatedRoleType.ModerationMute,
                    CreatedById = currentUserId
                });

                transaction.Commit();
                return(role);
            }
        }
Пример #2
0
        /// <inheritdoc />
        public async Task AddDesignatedRoleAsync(ulong guildId, ulong roleId, DesignatedRoleType type)
        {
            AuthorizationService.RequireAuthenticatedUser();
            AuthorizationService.RequireClaims(AuthorizationClaim.DesignatedRoleMappingCreate);

            using (var transaction = await DesignatedRoleMappingRepository.BeginCreateTransactionAsync())
            {
                if (await DesignatedRoleMappingRepository.AnyAsync(new DesignatedRoleMappingSearchCriteria()
                {
                    GuildId = guildId,
                    RoleId = roleId,
                    Type = type,
                    IsDeleted = false
                }))
                {
                    throw new InvalidOperationException($"Role {roleId} already has a {type} designation");
                }

                await DesignatedRoleMappingRepository.CreateAsync(new DesignatedRoleMappingCreationData()
                {
                    GuildId     = guildId,
                    RoleId      = roleId,
                    Type        = type,
                    CreatedById = AuthorizationService.CurrentUserId.Value
                });

                transaction.Commit();
            }
        }